Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres store fixes #461

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading