Skip to content

Commit

Permalink
Support postgres as a katib db (#1921)
Browse files Browse the repository at this point in the history
* implement postgres for katib db

* fix yaml lint

* apply go mod tidy

* Update manifests/v1beta1/components/postgres/postgres.yaml

Co-authored-by: Yuki Iwai <[email protected]>

* refactoring by reviews

- split openconnection to common packages
- add unit test for postgres db

* change to install only mysql by default

* remove useless import

* add postgres kustomization and e2e test for it

* change mysql installation files to be variable

* fix shell scripts

* fix lint

* fix image name

* set default value on github action workflow

* make postgres deployment to use pvc

* temporarily comments

* uncomment invalid experiments

* test with for loop

* sleep until controller created well

* add some comments

* Update pkg/db/v1beta1/postgres/postgres.go

Co-authored-by: Yuki Iwai <[email protected]>

* Update pkg/db/v1beta1/postgres/postgres_test.go

Co-authored-by: Yuki Iwai <[email protected]>

* refactor by reviews

Co-authored-by: Yuki Iwai <[email protected]>
  • Loading branch information
anencore94 and tenzen-y authored Aug 19, 2022
1 parent 6fa3913 commit cc888af
Show file tree
Hide file tree
Showing 26 changed files with 662 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/darts-cifar10-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
experiments: ${{ matrix.experiments }}
# Comma Delimited
trial-images: darts-cnn-cifar10-cpu
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -33,3 +34,4 @@ jobs:
kubernetes-version: ["v1.21.13", "v1.22.10", "v1.23.7"]
# Comma Delimited
experiments: ["darts-cpu"]
database-type: ["mysql", "postgres"]
2 changes: 2 additions & 0 deletions .github/workflows/enas-cifar10-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
experiments: ${{ matrix.experiments }}
# Comma Delimited
trial-images: enas-cnn-cifar10-cpu
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -33,3 +34,4 @@ jobs:
kubernetes-version: ["v1.21.13", "v1.22.10", "v1.23.7"]
# Comma Delimited
experiments: ["enas-cpu"]
database-type: ["mysql", "postgres"]
2 changes: 2 additions & 0 deletions .github/workflows/mxnet-mnist-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
experiments: ${{ matrix.experiments }}
# Comma Delimited
trial-images: mxnet-mnist
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -39,3 +40,4 @@ jobs:
# others
- "grid,bayesian-optimization,tpe"
- "multivariate-tpe,cma-es,hyperband"
database-type: ["mysql", "postgres"]
2 changes: 2 additions & 0 deletions .github/workflows/pytorch-mnist-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
training-operator: true
# Comma Delimited
trial-images: pytorch-mnist-cpu
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -36,3 +37,4 @@ jobs:
experiments:
- "file-metrics-collector,pytorchjob-mnist"
- "median-stop-with-json-format,file-metrics-collector-with-json-format"
database-type: ["mysql", "postgres"]
2 changes: 2 additions & 0 deletions .github/workflows/simple-pbt-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
experiments: ${{ matrix.experiments }}
# Comma Delimited
trial-images: simple-pbt
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -34,3 +35,4 @@ jobs:
kubernetes-version: ["v1.21.12", "v1.22.9", "v1.23.6"]
# Comma Delimited
experiments: ["simple-pbt"]
database-type: ["mysql", "postgres"]
7 changes: 6 additions & 1 deletion .github/workflows/template-e2e-test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ inputs:
training-operator:
required: false
type: boolean
default: false
trial-images:
required: true
type: string
katib-ui:
required: true
type: boolean
default: false
database-type:
required: false
type: string
default: mysql

runs:
using: composite
Expand All @@ -24,7 +29,7 @@ runs:

- name: Set Up Katib
shell: bash
run: ./test/e2e/v1beta1/scripts/gh-actions/setup-katib.sh ${{ inputs.katib-ui }} ${{ inputs.training-operator }}
run: ./test/e2e/v1beta1/scripts/gh-actions/setup-katib.sh ${{ inputs.katib-ui }} ${{ inputs.training-operator }} ${{ inputs.database-type }}

- name: Run E2E Experiment
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tf-mnist-with-summaries-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
training-operator: true
# Comma Delimited
trial-images: tf-mnist-with-summaries
database-type: ${{ matrix.database-type }}

strategy:
fail-fast: false
Expand All @@ -34,3 +35,4 @@ jobs:
kubernetes-version: ["v1.21.13", "v1.22.10", "v1.23.7"]
# Comma Delimited
experiments: ["tfjob-mnist-with-summaries"]
database-type: ["mysql", "postgres"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ update:

# Deploy Katib v1beta1 manifests using Kustomize into a k8s cluster.
deploy:
bash scripts/v1beta1/deploy.sh
bash scripts/v1beta1/deploy.sh $(WITH_DATABASE_TYPE)

# Undeploy Katib v1beta1 manifests using Kustomize from a k8s cluster
undeploy:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20211222182933-7c19fa370dbd
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hpcloud/tail v1.0.1-0.20180514194441-a1dbeea552b7
github.com/lib/pq v1.10.6
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a
github.com/onsi/gomega v1.17.0
github.com/prometheus/client_golang v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
Expand Down
9 changes: 9 additions & 0 deletions manifests/v1beta1/components/postgres/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- postgres.yaml
- pvc.yaml
- secret.yaml
- service.yaml
42 changes: 42 additions & 0 deletions manifests/v1beta1/components/postgres/postgres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: katib-postgres
namespace: kubeflow
labels:
katib.kubeflow.org/component: postgres
spec:
replicas: 1
selector:
matchLabels:
katib.kubeflow.org/component: postgres
strategy:
type: Recreate
template:
metadata:
labels:
katib.kubeflow.org/component: postgres
annotations:
sidecar.istio.io/inject: "false"
spec:
containers:
- name: katib-postgres
image: postgres:14.5-alpine
envFrom:
- secretRef:
name: katib-postgres-secrets
env:
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
ports:
- name: postgres
containerPort: 5432
protocol: TCP
volumeMounts:
- name: katib-postgres
mountPath: /var/lib/postgresql/data
volumes:
- name: katib-postgres
persistentVolumeClaim:
claimName: katib-postgres
12 changes: 12 additions & 0 deletions manifests/v1beta1/components/postgres/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: katib-postgres
namespace: kubeflow
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
10 changes: 10 additions & 0 deletions manifests/v1beta1/components/postgres/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: katib-postgres-secrets
data:
POSTGRES_USER: a2F0aWI= # katib
POSTGRES_PASSWORD: a2F0aWI= # katib
POSTGRES_DB: a2F0aWI= # katib
16 changes: 16 additions & 0 deletions manifests/v1beta1/components/postgres/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: katib-postgres
namespace: kubeflow
labels:
katib.kubeflow.org/component: postgres
spec:
type: ClusterIP
ports:
- port: 5432
protocol: TCP
name: dbapi
selector:
katib.kubeflow.org/component: postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: kubeflow
resources:
# Namespace.
- ../../components/namespace/
# Katib controller.
- ../../components/controller/
# Katib CRDs.
- ../../components/crd/
# Katib DB manager.
- ../../components/db-manager/
# Katib DB postgres.
- ../../components/postgres/
# Katib UI.
- ../../components/ui/
# Katib Cert Generator
- ../../components/cert-generator/
# Katib webhooks.
- ../../components/webhook/
images:
- name: docker.io/kubeflowkatib/katib-controller
newName: docker.io/kubeflowkatib/katib-controller
newTag: latest
- name: docker.io/kubeflowkatib/katib-db-manager
newName: docker.io/kubeflowkatib/katib-db-manager
newTag: latest
- name: docker.io/kubeflowkatib/katib-ui
newName: docker.io/kubeflowkatib/katib-ui
newTag: latest
- name: docker.io/kubeflowkatib/cert-generator
newName: docker.io/kubeflowkatib/cert-generator
newTag: latest
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: katib-db-manager
path: ./patches/db-manager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- op: replace
path: /spec/template/spec/containers/0/env
value:
- name: DB_NAME
value: "postgres"
- name: DB_PASSWORD
value: "katib"
48 changes: 48 additions & 0 deletions pkg/db/v1beta1/common/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2022 The Kubeflow Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
"database/sql"
"fmt"
"time"

"k8s.io/klog"
)

func OpenSQLConn(driverName string, dataSourceName string, interval time.Duration,
timeout time.Duration) (*sql.DB, error) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

timeoutC := time.After(timeout)
for {
select {
case <-ticker.C:
if db, err := sql.Open(driverName, dataSourceName); err == nil {
if err = db.Ping(); err == nil {
return db, nil
}
klog.Errorf("Ping to Katib db failed: %v", err)
} else {
klog.Errorf("Open sql connection failed: %v", err)
}
case <-timeoutC:
return nil, fmt.Errorf("Timeout waiting for DB conn successfully opened.")
}
}
}
22 changes: 18 additions & 4 deletions pkg/db/v1beta1/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ limitations under the License.

package common

import "time"

const (
DBUserEnvName = "DB_USER"
ConnectInterval = 5 * time.Second
ConnectTimeout = 60 * time.Second

DBNameEnvName = "DB_NAME"
DBUserEnvName = "DB_USER"
DBNameEnvName = "DB_NAME"
DBPasswordEnvName = "DB_PASSWORD"

MySqlDBNameEnvValue = "mysql"

DBPasswordEnvName = "DB_PASSWORD"

MySQLDBHostEnvName = "KATIB_MYSQL_DB_HOST"
MySQLDBPortEnvName = "KATIB_MYSQL_DB_PORT"
MySQLDatabase = "KATIB_MYSQL_DB_DATABASE"
Expand All @@ -33,4 +36,15 @@ const (
DefaultMySQLDatabase = "katib"
DefaultMySQLHost = "katib-mysql"
DefaultMySQLPort = "3306"

PostgresSQLDBNameEnvValue = "postgres"

PostgreSQLDBHostEnvName = "KATIB_POSTGRESQL_DB_HOST"
PostgreSQLDBPortEnvName = "KATIB_POSTGRESQL_DB_PORT"
PostgreSQLDatabase = "KATIB_POSTGRESQL_DB_DATABASE"

DefaultPostgreSQLUser = "katib"
DefaultPostgreSQLDatabase = "katib"
DefaultPostgreSQLHost = "katib-postgres"
DefaultPostgreSQLPort = "5432"
)
6 changes: 6 additions & 0 deletions pkg/db/v1beta1/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ import (

"github.com/kubeflow/katib/pkg/db/v1beta1/common"
"github.com/kubeflow/katib/pkg/db/v1beta1/mysql"
"github.com/kubeflow/katib/pkg/db/v1beta1/postgres"
"k8s.io/klog"
)

func NewKatibDBInterface(dbName string) (common.KatibDBInterface, error) {

if dbName == common.MySqlDBNameEnvValue {
klog.Info("Using MySQL")
return mysql.NewDBInterface()
} else if dbName == common.PostgresSQLDBNameEnvValue {
klog.Info("Using Postgres")
return postgres.NewDBInterface()
}
return nil, errors.New("Invalid DB Name")
}
Loading

0 comments on commit cc888af

Please sign in to comment.