Skip to content

Commit

Permalink
support system function
Browse files Browse the repository at this point in the history
  • Loading branch information
bjeevan-ib committed Oct 10, 2023
1 parent c2bc2b8 commit eae146c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ deploy: .id docker-push build-chart
--create-namespace \
-f helm/db-controller/minikube.yaml \
--set dbController.class=`cat .id` \
--set image.tag="${TAG}" \
--set db.identifier.prefix=`cat .id` ${HELM_SETFLAGS}
--set image.tag="${TAG}" ${HELM_SETFLAGS}

undeploy: .id
helm delete --namespace `cat .id` `cat .id`-db-ctrl
10 changes: 10 additions & 0 deletions controllers/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ func (r *DatabaseClaimReconciler) updateStatus(ctx context.Context, dbClaim *per
if err != nil {
return r.manageError(ctx, dbClaim, err)
}
dbClaim.Status.ActiveDB = *dbClaim.Status.NewDB.DeepCopy()
dbClaim.Status.NewDB = persistancev1.Status{ConnectionInfo: &persistancev1.DatabaseClaimConnectionInfo{}}
}

return r.reconcileMigrateToNewDB(ctx, dbClaim)
Expand Down Expand Up @@ -540,6 +542,10 @@ func (r *DatabaseClaimReconciler) reconcileUseExistingDB(ctx context.Context, db
return err
}
}
err = dbClient.ManageSystemFunctions(dbName, r.getSystemFunctions())
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -1072,6 +1078,10 @@ func (r *DatabaseClaimReconciler) getDbSubnetGroupNameRef() string {
return r.Config.GetString("dbSubnetGroupNameRef")
}

func (r *DatabaseClaimReconciler) getSystemFunctions() map[string]string {
return r.Config.GetStringMapString("system-functions")
}

func (r *DatabaseClaimReconciler) getDynamicHostWaitTime() time.Duration {
t := r.Config.GetInt("dynamicHostWaitTimeMin")
if t > maxWaitTime {
Expand Down
7 changes: 7 additions & 0 deletions helm/db-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ secrets:
enabled: false

env: local
realm: "us"
lifecycle: "dev"
db:
identifier:
prefix: "{{ .Values.env }}"
Expand Down Expand Up @@ -158,3 +160,8 @@ controllerConfig:
enablePerfInsight: true
# Possible values for enableCloudwatchLogsExport are all, none, postgresql and upgrade.
enableCloudwatchLogsExport: "none"
system-functions:
ib_realm: "{{ tpl .Values.realm . }}"
ib_env: "{{ .Values.env }}"
ib_lifecycle: "{{ .Values.lifecycle }}"

38 changes: 38 additions & 0 deletions pkg/dbclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,44 @@ func (pc *client) CreateDefaultExtentions(dbName string) error {
return err
}

func (pc *client) ManageSystemFunctions(dbName string, functions map[string]string) error {
db, err := pc.getDB(dbName)
if err != nil {
pc.log.Error(err, "could not connect to db", "database", dbName)
return err
}
pc.log.Info("ManageSystemFunctions - connected to " + dbName)
defer db.Close()
var currVal string
var create bool
for f, v := range functions {
create = false
err := db.QueryRow(fmt.Sprintf("SELECT %s()", f)).Scan(&currVal)
//check if error contains "does not exist"
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
pc.log.Info("function does not exist - create it", "function", f)
create = true
} else {
return err
}
} else {
if currVal != v {
pc.log.Info("function value is not correct - update it", "function", f)
create = true
}
}
if create {
_, err = db.Exec(fmt.Sprintf("CREATE OR REPLACE FUNCTION %s() RETURNS text AS $$SELECT text '%s'$$ LANGUAGE sql IMMUTABLE PARALLEL SAFE;", f, v))
if err != nil {
pc.log.Error(err, "could not create function", "database_name", dbName, "function", f, "value", v)
return err
}
}
}
return nil
}

func (pc *client) CreateGroup(dbName, rolename string) (bool, error) {
start := time.Now()
var exists bool
Expand Down
32 changes: 32 additions & 0 deletions pkg/dbclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,38 @@ func TestPostgresClientOperations(t *testing.T) {
if _, err := db.Exec("CREATE EXTENSION IF NOT EXISTS citext"); err != nil {
t.Errorf("citext is not created: %s", err)
}
t.Logf("create system functions")
functions := map[string]string{
"ib_realm": "eu",
"ib_env": "box-3",
"ib_lifecycle": "dev",
}
err = pc.ManageSystemFunctions(tt.args.dbName, functions)
if (err != nil) != tt.wantErr {
t.Errorf("\t%s ManageSystemFunctions() error = %v, wantErr %v", failed, err, tt.wantErr)
} else {
t.Logf("\t%s create system functions passed", succeed)
}
t.Logf("re-create same system functions")
err = pc.ManageSystemFunctions(tt.args.dbName, functions)
if (err != nil) != tt.wantErr {
t.Errorf("\t%s re-create ManageSystemFunctions() error = %v, wantErr %v", failed, err, tt.wantErr)
} else {
t.Logf("\t%s create same system functions passed", succeed)
}
t.Logf("re-create different system functions")
functions = map[string]string{
"ib_realm": "us",
"ib_env": "box-4",
"ib_lifecycle": "stage",
}
err = pc.ManageSystemFunctions(tt.args.dbName, functions)
if (err != nil) != tt.wantErr {
t.Errorf("\t%s re-create ManageSystemFunctions() error = %v, wantErr %v", failed, err, tt.wantErr)
} else {
t.Logf("\t%s create different system functions passed", succeed)
}

})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/dbclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Client interface {
ManageReplicationRole(username string, enableReplicationRole bool) error
ManageSuperUserRole(username string, enableSuperUser bool) error
ManageCreateRole(username string, enableCreateRole bool) error
ManageSystemFunctions(dbName string, functions map[string]string) error

DBCloser
}
Expand Down

0 comments on commit eae146c

Please sign in to comment.