Skip to content

Commit

Permalink
Merge pull request #461 from kube-tarian/postgres-store-fixes
Browse files Browse the repository at this point in the history
postgres store fixes
  • Loading branch information
share2kanna authored May 3, 2024
2 parents 9a82e89 + 07d2638 commit bd0deb0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
23 changes: 3 additions & 20 deletions capten/deployment-worker/internal/activities/plugin_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
pluginconfigstore "github.com/kube-tarian/kad/capten/common-pkg/pluginconfig-store"
vaultcred "github.com/kube-tarian/kad/capten/common-pkg/vault-cred"
"github.com/kube-tarian/kad/capten/deployment-worker/internal/captensdk"
"github.com/kube-tarian/kad/capten/deployment-worker/internal/dbstorepreactions/postgresstore"
"github.com/kube-tarian/kad/capten/model"
v1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -90,30 +91,12 @@ func (p *PluginActivities) PluginDeployPreActionPostgresStoreActivity(ctx contex
}, err
}

// Call capten-sdk DB setup
sdkDBClient := db.NewDBClientWithConfig(&db.DBConfig{
DbOemName: db.POSTGRES,
PluginName: req.PluginName,
DbName: req.DefaultNamespace + "-" + req.PluginName,
DbServiceUserName: req.PluginName,
})

vaultPath, err := sdkDBClient.SetupDatabase()
if err != nil {
return &model.ResponsePayload{
Status: "FAILED",
Message: json.RawMessage(fmt.Sprintf("{ \"reason\": \"setup database: %s\"}", err.Error())),
}, err
}

pluginInitConfigmapName := req.PluginName + pluginConfigmapNameTemplate
err = p.createUpdateConfigmap(ctx, req.DefaultNamespace, pluginInitConfigmapName, map[string]string{
"vault-path": vaultPath,
})
err = postgresstore.SetupPostgresDatabase(logger, req.PluginName, req.DefaultNamespace, pluginInitConfigmapName, p.k8sClient)
if err != nil {
return &model.ResponsePayload{
Status: "FAILED",
Message: json.RawMessage(fmt.Sprintf("{ \"reason\": \"update configmap: %s\"}", err.Error())),
Message: json.RawMessage(fmt.Sprintf("{ \"reason\": \" %s\"}", err.Error())),
}, err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package postgresstore

import (
"context"
"fmt"

"github.com/intelops/go-common/credentials"
"github.com/intelops/go-common/logging"
"github.com/kelseyhightower/envconfig"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
dbinit "github.com/kube-tarian/kad/capten/common-pkg/postgres/db-init"
"github.com/kube-tarian/kad/capten/deployment-worker/internal/k8sops"
)

const (
dbNameTemplate = "%s-db"
)

func SetupPostgresDatabase(log logging.Logger, pluginName, namespace, cmName string, k8sClient *k8s.K8SClient) error {
conf, err := readConfig()
if err != nil {
log.Error(err.Error())
return err
}

vaultPath := fmt.Sprintf("%s/%s/%s", credentials.CertCredentialType, pluginName, conf.EntityName)

conf.DBName = fmt.Sprintf(dbNameTemplate, pluginName)
conf.DBServiceUsername = pluginName
conf.Password = dbinit.GenerateRandomPassword(12)

err = dbinit.CreatedDatabaseWithConfig(log, conf)
if err != nil {
log.Error(err.Error())
return err
}

// Insert into vault path plugin/<plugin-name>/<svc-entity> => plugin/test/postgres
cred := credentials.PrepareServiceCredentialMap(credentials.ServiceCredential{
UserName: conf.DBServiceUsername,
Password: conf.Password,
AdditionalData: map[string]string{
"db-url": conf.DBAddress,
"db-name": conf.DBName,
"service-user": pluginName,
},
})

k8sops.CreateUpdateConfigmap(context.TODO(), log, namespace, cmName, map[string]string{
"vault-path": vaultPath,
}, k8sClient)
if err != nil {
return err
}

return credential.PutPluginCredential(context.TODO(), pluginName, conf.EntityName, cred)
}

// Read the Postgres DB configuration
func readConfig() (*dbinit.Config, error) {
var baseConfig dbinit.BaseConfig
if err := envconfig.Process("", &baseConfig); err != nil {
return nil, err
}
return &dbinit.Config{
BaseConfig: baseConfig,
}, nil
}

0 comments on commit bd0deb0

Please sign in to comment.