Skip to content

Commit

Permalink
Merge pull request #460 from kube-tarian/postgres-migrate
Browse files Browse the repository at this point in the history
migrate database from cassandra to postgres
vramk23 authored May 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents ab4353d + ed2a8f9 commit a56409c
Showing 170 changed files with 3,782 additions and 6,568 deletions.
27 changes: 14 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -5,15 +5,16 @@ DEPLOYMENT_WORKER_APP_NAME := deployment-worker
CONFIG_WORKER_APP_NAME := config-worker
BUILD := 0.1.1

.PHONY: gen-protoc
gen-protoc:
mkdir -p server/pkg/pb/serverpb
mkdir -p capten/agent/internal/pb/captenpluginspb
mkdir -p capten/common-pkg/pb/captenpluginspb
mkdir -p server/pkg/pb/agentpb
mkdir -p server/pkg/pb/captenpluginspb
mkdir -p server/pkg/pb/pluginstorepb
mkdir -p capten/common-pkg/vault-cred/vaultcredpb
mkdir -p capten/common-pkg/agentpb
mkdir -p capten/common-pkg/cluster-plugins/clusterpluginspb
mkdir -p capten/common-pkg/pb/vaultcredpb
mkdir -p capten/common-pkg/pb/agentpb
mkdir -p capten/common-pkg/pb/clusterpluginspb
mkdir -p server/pkg/pb/clusterpluginspb

cd proto && protoc --go_out=../server/pkg/pb/serverpb/ --go_opt=paths=source_relative \
@@ -23,29 +24,29 @@ gen-protoc:
cd proto && protoc --go_out=../server/pkg/pb/agentpb --go_opt=paths=source_relative \
--go-grpc_out=../server/pkg/pb/agentpb --go-grpc_opt=paths=source_relative \
./agent.proto

cd proto && protoc --go_out=../capten/common-pkg/agentpb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/agentpb --go-grpc_opt=paths=source_relative \
cd proto && protoc --go_out=../capten/common-pkg/pb/agentpb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/agentpb --go-grpc_opt=paths=source_relative \
./agent.proto

cd proto && protoc --go_out=../server/pkg/pb/captenpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../server/pkg/pb/captenpluginspb --go-grpc_opt=paths=source_relative \
./capten_plugins.proto

cd proto && protoc --go_out=../capten/agent/internal/pb/captenpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../capten/agent/internal/pb/captenpluginspb --go-grpc_opt=paths=source_relative \
cd proto && protoc --go_out=../capten/common-pkg/pb/captenpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/captenpluginspb --go-grpc_opt=paths=source_relative \
./capten_plugins.proto

cd proto && protoc --go_out=../capten/common-pkg/vault-cred/vaultcredpb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/vault-cred/vaultcredpb --go-grpc_opt=paths=source_relative \
cd proto && protoc --go_out=../capten/common-pkg/pb/vaultcredpb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/vaultcredpb --go-grpc_opt=paths=source_relative \
./vault_cred.proto

cd proto && protoc --go_out=../server/pkg/pb/pluginstorepb --go_opt=paths=source_relative \
--go-grpc_out=../server/pkg/pb/pluginstorepb --go-grpc_opt=paths=source_relative \
./plugin_store.proto

cd proto && protoc --go_out=../capten/common-pkg/cluster-plugins/clusterpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/cluster-plugins/clusterpluginspb --go-grpc_opt=paths=source_relative \
cd proto && protoc --go_out=../capten/common-pkg/pb/clusterpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../capten/common-pkg/pb/clusterpluginspb --go-grpc_opt=paths=source_relative \
./cluster_plugins.proto
cd proto && protoc --go_out=../server/pkg/pb/clusterpluginspb --go_opt=paths=source_relative \
--go-grpc_out=../server/pkg/pb/clusterpluginspb --go-grpc_opt=paths=source_relative \
13 changes: 5 additions & 8 deletions capten/agent/internal/api/agent.go
Original file line number Diff line number Diff line change
@@ -5,13 +5,12 @@ import (
"fmt"

"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/config"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/agent/internal/temporalclient"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/cluster-plugins/clusterpluginspb"
pluginconfigtore "github.com/kube-tarian/kad/capten/common-pkg/pluginconfig-store"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
)

var _ agentpb.AgentServer = &Agent{}
@@ -22,13 +21,12 @@ type Agent struct {
clusterpluginspb.UnimplementedClusterPluginsServer
tc *temporalclient.Client
as *captenstore.Store
pas *pluginconfigtore.Store
log logging.Logger
cfg *config.SericeConfig
createPr bool
}

func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Store, pas *pluginconfigtore.Store) (*Agent, error) {
func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Store) (*Agent, error) {
var tc *temporalclient.Client
var err error

@@ -40,7 +38,6 @@ func NewAgent(log logging.Logger, cfg *config.SericeConfig, as *captenstore.Stor
agent := &Agent{
tc: tc,
as: as,
pas: pas,
cfg: cfg,
log: log,
}
2 changes: 1 addition & 1 deletion capten/agent/internal/api/agent_cluster_apps.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
)

func (a *Agent) SyncApp(ctx context.Context, request *agentpb.SyncAppRequest) (
4 changes: 2 additions & 2 deletions capten/agent/internal/api/agent_cluster_apps_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
"testing"

"github.com/kube-tarian/kad/capten/agent/internal/config"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/integrator/common-pkg/logging"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v2"
@@ -27,7 +27,7 @@ func TestAgentTestSuite(t *testing.T) {
t.Fatal(err)
}*/

agent, err := NewAgent(agentSuite.logger, &config.SericeConfig{}, nil, nil)
agent, err := NewAgent(agentSuite.logger, &config.SericeConfig{}, nil)
if err != nil {
t.Fatal(err)
}
2 changes: 1 addition & 1 deletion capten/agent/internal/api/app_config_sso.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"context"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
2 changes: 1 addition & 1 deletion capten/agent/internal/api/app_values_util.go
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ import (
"strings"

"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
2 changes: 1 addition & 1 deletion capten/agent/internal/api/app_values_util_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
"testing"

"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
2 changes: 1 addition & 1 deletion capten/agent/internal/api/capten_plugin_apis.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ package api
import (
"context"

"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
)

func (a *Agent) GetCaptenPlugins(ctx context.Context, request *captenpluginspb.GetCaptenPluginsRequest) (
42 changes: 18 additions & 24 deletions capten/agent/internal/api/cluster_plugin_apis.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,14 @@ import (
"html/template"

"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/cluster-plugins/clusterpluginspb"
pluginconfigstore "github.com/kube-tarian/kad/capten/common-pkg/pluginconfig-store"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
"github.com/kube-tarian/kad/capten/model"
"gopkg.in/yaml.v2"
)

func (a *Agent) GetClusterPlugins(ctx context.Context, request *clusterpluginspb.GetClusterPluginsRequest) (
*clusterpluginspb.GetClusterPluginsResponse, error) {
pluginConfigList, err := a.pas.GetAllPlugins()
pluginConfigList, err := a.as.GetAllClusterPluginConfigs()
if err != nil {
return &clusterpluginspb.GetClusterPluginsResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
@@ -44,31 +43,26 @@ func (a *Agent) DeployClusterPlugin(ctx context.Context, request *clusterplugins
*clusterpluginspb.DeployClusterPluginResponse, error) {
a.log.Infof("Recieved Plugin Deploy request for plugin %s, version %+v", request.Plugin.PluginName, request.Plugin.Version)

pluginConfig := &pluginconfigstore.PluginConfig{
Plugin: *request.Plugin,
}

values, err := replaceTemplateValuesInByteData(pluginConfig.Plugin.Values, pluginConfig.Plugin.OverrideValues)
values, err := replaceTemplateValuesInByteData(request.Plugin.Values, request.Plugin.OverrideValues)
if err != nil {
a.log.Errorf("failed to derive template values for plugin %s, %v", pluginConfig.PluginName, err)
a.log.Errorf("failed to derive template values for plugin %s, %v", request.Plugin.PluginName, err)
return &clusterpluginspb.DeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to prepare plugin values",
}, nil
}

pluginConfig.Plugin.Values = values
pluginConfig.InstallStatus = string(model.AppIntallingStatus)
if err := a.pas.UpsertPluginConfig(pluginConfig); err != nil {
a.log.Errorf("failed to update plugin config data for plugin %s, %v", pluginConfig.PluginName, err)
request.Plugin.Values = values
request.Plugin.InstallStatus = string(model.AppIntallingStatus)
if err := a.as.UpsertClusterPluginConfig(request.Plugin); err != nil {
a.log.Errorf("failed to update plugin config data for plugin %s, %v", request.Plugin.PluginName, err)
return &clusterpluginspb.DeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
StatusMessage: "failed to update plugin config data",
}, nil
}

// deployReq := prepareAppDeployRequestFromPlugin(plugin)
go a.deployPluginWithWorkflow(request.Plugin, pluginConfig)
go a.deployPluginWithWorkflow(request.Plugin)

a.log.Infof("Triggerred plugin [%s] install", request.Plugin.PluginName)
return &clusterpluginspb.DeployClusterPluginResponse{
@@ -89,7 +83,7 @@ func (a *Agent) UnDeployClusterPlugin(ctx context.Context, request *clusterplugi
}, nil
}

pluginConfigdata, err := a.pas.GetPluginConfig(request.PluginName)
pluginConfigdata, err := a.as.GetClusterPluginConfig(request.PluginName)
if err != nil {
a.log.Errorf("failed to fetch plugin config record %s, %v", request.PluginName, err)
return &clusterpluginspb.UnDeployClusterPluginResponse{
@@ -99,7 +93,7 @@ func (a *Agent) UnDeployClusterPlugin(ctx context.Context, request *clusterplugi
}

pluginConfigdata.InstallStatus = string(model.AppUnInstallingStatus)
if err := a.pas.UpsertPluginConfig(pluginConfigdata); err != nil {
if err := a.as.UpsertClusterPluginConfig(pluginConfigdata); err != nil {
a.log.Errorf("failed to update plugin config status with UnInstalling for plugin %s, %v", pluginConfigdata.PluginName, err)
return &clusterpluginspb.UnDeployClusterPluginResponse{
Status: clusterpluginspb.StatusCode_INTERNRAL_ERROR,
@@ -116,7 +110,7 @@ func (a *Agent) UnDeployClusterPlugin(ctx context.Context, request *clusterplugi
}, nil
}

func (a *Agent) deployPluginWithWorkflow(plugin *clusterpluginspb.Plugin, pluginConfig *pluginconfigstore.PluginConfig) {
func (a *Agent) deployPluginWithWorkflow(plugin *clusterpluginspb.Plugin) {
wd := workers.NewDeployment(a.tc, a.log)
_, err := wd.SendEventV2(context.TODO(), wd.GetPluginWorkflowName(), string(model.AppInstallAction), plugin)
if err != nil {
@@ -125,23 +119,23 @@ func (a *Agent) deployPluginWithWorkflow(plugin *clusterpluginspb.Plugin, plugin
// a.log.Errorf("failed to update plugin config for plugin %s, %v", pluginConfig.PluginName, err)
// return
// }
a.log.Errorf("sendEventV2 failed, plugin: %s, reason: %v", pluginConfig.PluginName, err)
a.log.Errorf("sendEventV2 failed, plugin: %s, reason: %v", plugin.PluginName, err)
return
}
// TODO: workflow will update the final status
// Write a periodic scheduler which will go through all apps not in installed status and check the status till either success or failed.
// Make SendEventV2 asynchrounous so that periodic scheduler will take care of monitoring.
}

func (a *Agent) unInstallPluginWithWorkflow(request *clusterpluginspb.UnDeployClusterPluginRequest, pluginConfig *pluginconfigstore.PluginConfig) {
func (a *Agent) unInstallPluginWithWorkflow(request *clusterpluginspb.UnDeployClusterPluginRequest, plugin *clusterpluginspb.Plugin) {
wd := workers.NewDeployment(a.tc, a.log)
_, err := wd.SendDeleteEvent(context.TODO(), wd.GetPluginWorkflowName(), string(model.AppUnInstallAction), request)
if err != nil {
a.log.Errorf("failed to send delete event to workflow for plugin %s, %v", pluginConfig.PluginName, err)
a.log.Errorf("failed to send delete event to workflow for plugin %s, %v", request.PluginName, err)

pluginConfig.InstallStatus = string(model.AppUnUninstallFailedStatus)
if err := a.pas.UpsertPluginConfig(pluginConfig); err != nil {
a.log.Errorf("failed to update plugin config status with Installed for plugin %s, %v", pluginConfig.PluginName, err)
plugin.InstallStatus = string(model.AppUnUninstallFailedStatus)
if err := a.as.UpsertClusterPluginConfig(plugin); err != nil {
a.log.Errorf("failed to update plugin config status with Installed for plugin %s, %v", request.PluginName, err)
}
}
}
2 changes: 1 addition & 1 deletion capten/agent/internal/api/container_registry.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import (

"github.com/google/uuid"
"github.com/intelops/go-common/credentials"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
)

const containerRegEntityName = "container-registry"
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_argocd_apis.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
"strings"

"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/plugins/argocd"
)

2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_cloud_provider_apis.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (

"github.com/google/uuid"
"github.com/intelops/go-common/credentials"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
)

const cloudProviderEntityName = "cloud-provider"
9 changes: 2 additions & 7 deletions capten/agent/internal/api/plugin_crossplane_project_apis.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/model"
)

@@ -168,7 +168,6 @@ func (a *Agent) configureCrossplaneGitRepo(req *model.CrossplaneProject, provide
&model.ConfigureParameters{Resource: crossplaneConfigUseCase, Action: model.CrossPlaneProjectSync}, ci)
if err != nil {
req.Status = string(model.CrossplaneProjectConfigurationFailed)
req.WorkflowId = "NA"
if err := a.as.UpsertCrossplaneProject(req); err != nil {
return fmt.Errorf("failed to update Cluster Gitopts Project, %v", err)
}
@@ -178,8 +177,6 @@ func (a *Agent) configureCrossplaneGitRepo(req *model.CrossplaneProject, provide
a.log.Infof("Crossplane Git project %s config workflow %s created", req.GitProjectUrl, wkfId)

req.Status = string(model.CrossplaneProjectConfigured)
req.WorkflowId = wkfId
req.WorkflowStatus = string(model.WorkFlowStatusStarted)
if err := a.as.UpsertCrossplaneProject(req); err != nil {
a.log.Errorf("failed to update Cluster Gitopts Project, %v", err)
return nil
@@ -193,10 +190,9 @@ func (a *Agent) configureCrossplaneGitRepo(req *model.CrossplaneProject, provide
func (a *Agent) monitorCrossplaneWorkflow(req *model.CrossplaneProject, wkfId string) {
// during system reboot start monitoring, add it in map or somewhere.
wd := workers.NewConfig(a.tc, a.log)
wkfResp, err := wd.GetWorkflowInformation(context.TODO(), wkfId)
_, err := wd.GetWorkflowInformation(context.TODO(), wkfId)
if err != nil {
req.Status = string(model.CrossplaneProjectConfigurationFailed)
req.WorkflowStatus = string(model.WorkFlowStatusFailed)
if err := a.as.UpsertCrossplaneProject(req); err != nil {
a.log.Errorf("failed to update Cluster Gitopts Project, %v", err)
return
@@ -206,7 +202,6 @@ func (a *Agent) monitorCrossplaneWorkflow(req *model.CrossplaneProject, wkfId st
}

req.Status = string(model.CrossplaneProjectConfigured)
req.WorkflowStatus = wkfResp.Status
if err := a.as.UpsertCrossplaneProject(req); err != nil {
a.log.Errorf("failed to update Cluster Gitopts Project, %v", err)
return
4 changes: 2 additions & 2 deletions capten/agent/internal/api/plugin_crossplane_provider_apis.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import (
"context"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/model"
)

@@ -47,7 +47,7 @@ func (a *Agent) AddCrossplanProvider(ctx context.Context, request *captenplugins
Status: string(model.CrossPlaneProviderOutofSynch),
}

if err := a.as.InsertCrossplaneProvider(&provider); err != nil {
if err := a.as.UpsertCrossplaneProvider(&provider); err != nil {
a.log.Errorf("failed to store crossplane provider to DB, %v", err)
return &captenpluginspb.AddCrossplanProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_git_apis.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (

"github.com/google/uuid"
"github.com/intelops/go-common/credentials"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
)

const gitProjectEntityName = "git-project"
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_managed_clusters_apis.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import (
"fmt"

"github.com/intelops/go-common/credentials"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
)

const ManagedClusterEntityName = "managedcluster"
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_tekton_apis.go
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ package api
import (
"context"

"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/agent/internal/workers"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/model"
)

2 changes: 1 addition & 1 deletion capten/agent/internal/api/store_cred_apis.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
vaultcred "github.com/kube-tarian/kad/capten/common-pkg/vault-cred"

v1 "k8s.io/api/core/v1"
35 changes: 11 additions & 24 deletions capten/agent/internal/app/app.go
Original file line number Diff line number Diff line change
@@ -10,17 +10,14 @@ import (
"github.com/intelops/go-common/logging"
ginapiserver "github.com/kube-tarian/kad/capten/agent/gin-api-server"
agentapi "github.com/kube-tarian/kad/capten/agent/internal/api"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/config"
"github.com/kube-tarian/kad/capten/agent/internal/crossplane"
"github.com/kube-tarian/kad/capten/agent/internal/job"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/agent/internal/util"
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
dbinit "github.com/kube-tarian/kad/capten/common-pkg/cassandra/db-init"
dbmigrate "github.com/kube-tarian/kad/capten/common-pkg/cassandra/db-migrate"
"github.com/kube-tarian/kad/capten/common-pkg/cluster-plugins/clusterpluginspb"
pluginconfigtore "github.com/kube-tarian/kad/capten/common-pkg/pluginconfig-store"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
dbinit "github.com/kube-tarian/kad/capten/common-pkg/postgres/db-init"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
@@ -39,7 +36,7 @@ func Start() {
log.Fatalf("service config reading failed, %v", err)
}

if err := configureDB(); err != nil {
if err := configurePostgresDB(); err != nil {
log.Fatalf("%v", err)
}

@@ -49,13 +46,7 @@ func Start() {
return
}

pas, err := pluginconfigtore.NewStore(log)
if err != nil {
log.Errorf("failed to initialize plugin app store, %v", err)
return
}

rpcapi, err := agentapi.NewAgent(log, cfg, as, pas)
rpcapi, err := agentapi.NewAgent(log, cfg, as)
if err != nil {
log.Fatalf("Agent initialization failed, %v", err)
return
@@ -116,17 +107,13 @@ func Start() {
log.Debugf("Exiting Agent")
}

func configureDB() error {
if err := util.SyncCassandraAdminSecret(log); err != nil {
return errors.WithMessage(err, "error in update cassandra secret to vault")
}

func configurePostgresDB() error {
if err := dbinit.CreatedDatabase(log); err != nil {
return errors.WithMessage(err, "error creating database")
return errors.WithMessage(err, "error in creating postgres database")
}

if err := dbmigrate.RunMigrations(log, dbmigrate.UP); err != nil {
return errors.WithMessage(err, "error in migrating cassandra DB")
if err := dbinit.RunMigrations(dbinit.UP); err != nil {
return errors.WithMessage(err, "error in migrating postgres database")
}
return nil
}
346 changes: 0 additions & 346 deletions capten/agent/internal/capten-store/app_config_store.go

This file was deleted.

140 changes: 0 additions & 140 deletions capten/agent/internal/capten-store/argocd_projects.go

This file was deleted.

152 changes: 0 additions & 152 deletions capten/agent/internal/capten-store/cloud_provider.go

This file was deleted.

124 changes: 0 additions & 124 deletions capten/agent/internal/capten-store/container_registry.go

This file was deleted.

145 changes: 0 additions & 145 deletions capten/agent/internal/capten-store/crossplane_project.go

This file was deleted.

113 changes: 0 additions & 113 deletions capten/agent/internal/capten-store/crossplane_store.go

This file was deleted.

142 changes: 0 additions & 142 deletions capten/agent/internal/capten-store/git_projects.go

This file was deleted.

138 changes: 0 additions & 138 deletions capten/agent/internal/capten-store/managed_cluster.go

This file was deleted.

37 changes: 0 additions & 37 deletions capten/agent/internal/capten-store/store.go

This file was deleted.

146 changes: 0 additions & 146 deletions capten/agent/internal/capten-store/tekton_projects.go

This file was deleted.

4 changes: 2 additions & 2 deletions capten/agent/internal/crossplane/cluster_claims.go
Original file line number Diff line number Diff line change
@@ -9,11 +9,11 @@ import (

"github.com/google/uuid"
"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/temporalclient"
"github.com/kube-tarian/kad/capten/agent/internal/workers"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"

"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"

"github.com/kube-tarian/kad/capten/common-pkg/k8s"
managedcluster "github.com/kube-tarian/kad/capten/common-pkg/managed-cluster"
4 changes: 2 additions & 2 deletions capten/agent/internal/crossplane/package_providers.go
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@ import (
"sync"

"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/temporalclient"
"github.com/kube-tarian/kad/capten/agent/internal/workers"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"

"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/model"
"k8s.io/apimachinery/pkg/runtime/schema"
)
2 changes: 1 addition & 1 deletion capten/agent/internal/crossplane/watchers.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import (
"time"

"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
)

2 changes: 1 addition & 1 deletion capten/agent/internal/job/crossplane_resources_sync.go
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ package job

import (
"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/crossplane"
captenstore "github.com/kube-tarian/kad/capten/common-pkg/capten-store"
)

type CrossplaneResourcesSync struct {
Original file line number Diff line number Diff line change
@@ -10,17 +10,17 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
)

type SyncSecretConfig struct {
DBAdminCredIdentifier string `envconfig:"DB_ADMIN_CRED_IDENTIFIER" default:"cassandra-admin"`
EntityName string `envconfig:"DB_ENTITY_NAME" required:"true"`
SecretName string `envconfig:"CASSANDRA_SECRET_NAME" required:"true"`
type SyncPostgresSecretConfig struct {
DBAdminCredIdentifier string `envconfig:"PG_DB_ADMIN_CRED_IDENTIFIER" default:"postgres-admin"`
EntityName string `envconfig:"PG_DB_ENTITY_NAME" required:"true"`
SecretName string `envconfig:"PG_DB_SECRET_NAME" required:"true"`
Namespace string `envconfig:"POD_NAMESPACE" required:"true"`
}

func SyncCassandraAdminSecret(log logging.Logger) error {
conf := &SyncSecretConfig{}
func SyncPostgresAdminSecret(log logging.Logger) error {
conf := &SyncPostgresSecretConfig{}
if err := envconfig.Process("", conf); err != nil {
return fmt.Errorf("cassandra config read failed, %v", err)
return fmt.Errorf("postgres config read failed, %v", err)
}

k8sClient, err := k8s.NewK8SClient(log)
168 changes: 168 additions & 0 deletions capten/common-pkg/capten-store/app_config_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package captenstore

import (
"encoding/base64"
"fmt"
"time"

"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/agentpb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

func (a *Store) UpsertAppConfig(appData *agentpb.SyncAppData) error {
if len(appData.Config.ReleaseName) == 0 {
return fmt.Errorf("app release name empty")
}

appConfig := &ClusterAppConfig{}
recordFound := true
err := a.dbClient.Find(appConfig, ClusterAppConfig{ReleaseName: appData.Config.ReleaseName})
if err != nil {
if gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return prepareError(err, appData.Config.ReleaseName, "Fetch")
}
err = nil
recordFound = false
} else if appConfig.ReleaseName == "" {
recordFound = false
}

appConfig.ReleaseName = appData.Config.ReleaseName
appConfig.PluginName = appData.Config.PluginName
appConfig.PluginStoreType = int(appData.Config.PluginStoreType)
appConfig.Category = appData.Config.Category
appConfig.Description = appData.Config.Description
appConfig.Icon = appData.Config.Icon
appConfig.AppName = appData.Config.ChartName
appConfig.RepoURL = appData.Config.RepoURL
appConfig.Namespace = appData.Config.Namespace
appConfig.PrivilegedNamespace = appData.Config.PrivilegedNamespace
appConfig.APIEndpoint = appData.Config.ApiEndpoint
appConfig.UIEndpoint = appData.Config.UiEndpoint
appConfig.UIModuleEndpoint = appData.Config.UiModuleEndpoint
appConfig.Version = appData.Config.Version
appConfig.OverrideValues = base64.StdEncoding.EncodeToString(appData.Values.OverrideValues)
appConfig.TemplateValues = base64.StdEncoding.EncodeToString(appData.Values.TemplateValues)
appConfig.LaunchUIValues = base64.StdEncoding.EncodeToString(appData.Values.LaunchUIValues)
appConfig.InstallStatus = appData.Config.InstallStatus
appConfig.DefaultApp = appData.Config.DefualtApp
appConfig.LastUpdateTime = time.Now()

if !recordFound {
err = a.dbClient.Create(appConfig)
} else {
err = a.dbClient.Update(appConfig, ClusterAppConfig{ReleaseName: appData.Config.ReleaseName})
}
return err
}

func (a *Store) GetAppConfig(appReleaseName string) (*agentpb.SyncAppData, error) {
appConfig := &ClusterAppConfig{}
err := a.dbClient.Find(appConfig, ClusterAppConfig{ReleaseName: appReleaseName})
if err != nil {
err = prepareError(err, appReleaseName, "Fetch")
return nil, err
}

overrideValues, err := base64.StdEncoding.DecodeString(appConfig.OverrideValues)
if err != nil {
return nil, err
}
launchUIValues, err := base64.StdEncoding.DecodeString(appConfig.LaunchUIValues)
if err != nil {
return nil, err
}
templateValues, err := base64.StdEncoding.DecodeString(appConfig.TemplateValues)
if err != nil {
return nil, err
}

return &agentpb.SyncAppData{
Config: &agentpb.AppConfig{
ReleaseName: appConfig.ReleaseName,
PluginName: appConfig.PluginName,
PluginStoreType: agentpb.PluginStoreType(appConfig.PluginStoreType),
Category: appConfig.Category,
Description: appConfig.Description,
Icon: appConfig.Icon,
ChartName: appConfig.AppName,
RepoURL: appConfig.RepoURL,
Namespace: appConfig.Namespace,
PrivilegedNamespace: appConfig.PrivilegedNamespace,
ApiEndpoint: appConfig.APIEndpoint,
UiEndpoint: appConfig.UIEndpoint,
UiModuleEndpoint: appConfig.UIModuleEndpoint,
Version: appConfig.Version,
InstallStatus: appConfig.InstallStatus,
DefualtApp: appConfig.DefaultApp,
LastUpdateTime: appConfig.LastUpdateTime.Format(time.RFC3339),
},
Values: &agentpb.AppValues{
OverrideValues: overrideValues,
LaunchUIValues: launchUIValues,
TemplateValues: templateValues,
},
}, nil
}

func (a *Store) GetAllApps() ([]*agentpb.SyncAppData, error) {
var appConfigs []ClusterAppConfig
err := a.dbClient.Find(&appConfigs, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("Unable to fetch apps: %v", err.Error())
}

var appData []*agentpb.SyncAppData
for _, ac := range appConfigs {
overrideValues, err := base64.StdEncoding.DecodeString(ac.OverrideValues)
if err != nil {
return nil, err
}
launchUIValues, err := base64.StdEncoding.DecodeString(ac.LaunchUIValues)
if err != nil {
return nil, err
}
templateValues, err := base64.StdEncoding.DecodeString(ac.TemplateValues)
if err != nil {
return nil, err
}

appData = append(appData, &agentpb.SyncAppData{
Config: &agentpb.AppConfig{
ReleaseName: ac.ReleaseName,
PluginName: ac.PluginName,
PluginStoreType: agentpb.PluginStoreType(ac.PluginStoreType),
Category: ac.Category,
Description: ac.Description,
Icon: ac.Icon,
ChartName: ac.AppName,
RepoURL: ac.RepoURL,
Namespace: ac.Namespace,
PrivilegedNamespace: ac.PrivilegedNamespace,
ApiEndpoint: ac.APIEndpoint,
UiEndpoint: ac.UIEndpoint,
UiModuleEndpoint: ac.UIModuleEndpoint,
Version: ac.Version,
InstallStatus: ac.InstallStatus,
DefualtApp: ac.DefaultApp,
LastUpdateTime: ac.LastUpdateTime.Format(time.RFC3339),
},
Values: &agentpb.AppValues{
OverrideValues: overrideValues,
LaunchUIValues: launchUIValues,
TemplateValues: templateValues,
},
})
}

return appData, nil
}

func (a *Store) DeleteAppConfigByReleaseName(releaseName string) error {
err := a.dbClient.Delete(ClusterAppConfig{}, ClusterAppConfig{ReleaseName: releaseName})
if err != nil {
err = prepareError(err, releaseName, "Delete")
}
return err
}
117 changes: 117 additions & 0 deletions capten/common-pkg/capten-store/cloud_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"gorm.io/gorm"
)

func (a *Store) UpsertCloudProvider(config *captenpluginspb.CloudProvider) error {
if config.Id == "" {
provider := CloudProvider{
ID: uuid.New(),
CloudType: config.CloudType,
Labels: config.Labels,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&provider)
}

provider := CloudProvider{
ID: uuid.MustParse(config.Id),
CloudType: config.CloudType,
Labels: config.Labels,
LastUpdateTime: time.Now()}

return a.dbClient.Update(provider, CloudProvider{ID: provider.ID})
}

func (a *Store) GetCloudProviderForID(id string) (*captenpluginspb.CloudProvider, error) {
provider := CloudProvider{}
err := a.dbClient.Find(&provider, CloudProvider{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}

result := &captenpluginspb.CloudProvider{
Id: provider.ID.String(),
CloudType: provider.CloudType,
Labels: provider.Labels,
LastUpdateTime: provider.LastUpdateTime.String(),
}

return result, err
}

func (a *Store) GetCloudProviders() ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Find(&providers, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

cloudProviders := make([]*captenpluginspb.CloudProvider, 0)
for _, provider := range providers {
cloudProviders = append(cloudProviders, &captenpluginspb.CloudProvider{
Id: provider.ID.String(),
CloudType: provider.CloudType,
Labels: provider.Labels,
LastUpdateTime: provider.LastUpdateTime.String(),
})
}
return cloudProviders, err
}

func (a *Store) GetCloudProvidersByLabelsAndCloudType(searchLabels []string, cloudType string) ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Session().Where("cloud_type = ?", cloudType).Where("labels @> ?", fmt.Sprintf("{%s}", searchLabels[0])).Find(&providers).Error
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if gorm.ErrRecordNotFound != err {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}
err = nil
}

cloudProviders := make([]*captenpluginspb.CloudProvider, 0)
for _, provider := range providers {
cloudProviders = append(cloudProviders, &captenpluginspb.CloudProvider{
Id: provider.ID.String(),
CloudType: provider.CloudType,
Labels: provider.Labels,
LastUpdateTime: provider.LastUpdateTime.String(),
})
}
return cloudProviders, err
}

func (a *Store) GetCloudProvidersByLabels(searchLabels []string) ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Find(&providers, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

cloudProviders := make([]*captenpluginspb.CloudProvider, 0)
for _, provider := range providers {
cloudProviders = append(cloudProviders, &captenpluginspb.CloudProvider{
Id: provider.ID.String(),
CloudType: provider.CloudType,
Labels: provider.Labels,
LastUpdateTime: provider.LastUpdateTime.String(),
})
}
return cloudProviders, err
}

func (a *Store) DeleteCloudProviderById(id string) error {
err := a.dbClient.Delete(CloudProvider{}, CloudProvider{ID: uuid.MustParse(id)})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}
140 changes: 140 additions & 0 deletions capten/common-pkg/capten-store/cluster_plugin_config_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package captenstore

import (
"encoding/base64"
"fmt"
"time"

"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func (a *Store) UpsertClusterPluginConfig(pluginConfig *clusterpluginspb.Plugin) error {
if len(pluginConfig.PluginName) == 0 {
return fmt.Errorf("plugin name empty")
}

plugin := &ClusterPluginConfig{}
recordFound := true
err := a.dbClient.Find(plugin, ClusterPluginConfig{PluginName: pluginConfig.PluginName})
if err != nil {
if gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return prepareError(err, pluginConfig.PluginName, "Fetch")
}
err = nil
recordFound = false
fmt.Println("not found")
} else if plugin.PluginName == "" {
recordFound = false
}

plugin.PluginName = pluginConfig.PluginName
plugin.PluginStoreType = int(pluginConfig.StoreType)
plugin.Category = pluginConfig.Category
plugin.Capabilities = pluginConfig.Capabilities
plugin.Description = pluginConfig.Description
plugin.Icon = pluginConfig.Icon
plugin.ChartName = pluginConfig.ChartName
plugin.ChartRepo = pluginConfig.ChartRepo
plugin.Namespace = pluginConfig.DefaultNamespace
plugin.PrivilegedNamespace = pluginConfig.PrivilegedNamespace
plugin.APIEndpoint = pluginConfig.ApiEndpoint
plugin.UIEndpoint = pluginConfig.UiEndpoint
plugin.UIModuleEndpoint = pluginConfig.UiEndpoint
plugin.Version = pluginConfig.Version
plugin.Values = base64.StdEncoding.EncodeToString(pluginConfig.Values)
plugin.OverrideValues = base64.StdEncoding.EncodeToString(pluginConfig.OverrideValues)
plugin.InstallStatus = pluginConfig.InstallStatus
plugin.LastUpdateTime = time.Now()

if !recordFound {
err = a.dbClient.Create(plugin)
fmt.Println("created")
} else {
err = a.dbClient.Update(plugin, ClusterPluginConfig{PluginName: pluginConfig.PluginName})
fmt.Println("updated")
}
return err
}

func (a *Store) DeleteClusterPluginConfig(pluginName string) error {
err := a.dbClient.Delete(ClusterPluginConfig{}, ClusterPluginConfig{PluginName: pluginName})
if err != nil {
err = prepareError(err, pluginName, "Delete")
}
return err
}

func (a *Store) GetClusterPluginConfig(pluginName string) (*clusterpluginspb.Plugin, error) {
var pluginConfig ClusterPluginConfig
err := a.dbClient.Find(&pluginConfig, "plugin_name = ?", pluginName)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}

values, _ := base64.StdEncoding.DecodeString(pluginConfig.Values)
overrideValues, _ := base64.StdEncoding.DecodeString(pluginConfig.OverrideValues)

p := &clusterpluginspb.Plugin{
PluginName: pluginConfig.PluginName,
StoreType: clusterpluginspb.StoreType(pluginConfig.PluginStoreType),
Category: pluginConfig.Category,
Capabilities: pluginConfig.Capabilities,
Description: pluginConfig.Description,
Icon: pluginConfig.Icon,
ChartName: pluginConfig.ChartName,
ChartRepo: pluginConfig.ChartRepo,
DefaultNamespace: pluginConfig.Namespace,
PrivilegedNamespace: pluginConfig.PrivilegedNamespace,
ApiEndpoint: pluginConfig.APIEndpoint,
UiEndpoint: pluginConfig.UIEndpoint,
Version: pluginConfig.Version,
Values: values,
OverrideValues: overrideValues,
InstallStatus: pluginConfig.InstallStatus,
}

return p, nil
}

func (a *Store) GetAllClusterPluginConfigs() ([]*clusterpluginspb.Plugin, error) {
var plugins []ClusterPluginConfig
err := a.dbClient.Find(&plugins, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch plugins: %v", err.Error())
}

var pluginConfigs []*clusterpluginspb.Plugin
for _, p := range plugins {
values, _ := base64.StdEncoding.DecodeString(p.Values)
overrideValues, _ := base64.StdEncoding.DecodeString(p.OverrideValues)

pluginConfig := &clusterpluginspb.Plugin{
PluginName: p.PluginName,
StoreType: clusterpluginspb.StoreType(p.PluginStoreType),
Category: p.Category,
Capabilities: p.Capabilities,
Description: p.Description,
Icon: p.Icon,
ChartName: p.ChartName,
ChartRepo: p.ChartRepo,
DefaultNamespace: p.Namespace,
PrivilegedNamespace: p.PrivilegedNamespace,
ApiEndpoint: p.APIEndpoint,
UiEndpoint: p.UIEndpoint,
Version: p.Version,
Values: values,
OverrideValues: overrideValues,
InstallStatus: p.InstallStatus,
}
pluginConfigs = append(pluginConfigs, pluginConfig)
}

return pluginConfigs, nil
}
96 changes: 96 additions & 0 deletions capten/common-pkg/capten-store/container_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

func (a *Store) UpsertContainerRegistry(config *captenpluginspb.ContainerRegistry) error {
if config.Id == "" {
registry := ContainerRegistry{
ID: uuid.New(),
RegistryURL: config.RegistryUrl,
RegistryType: config.RegistryType,
Labels: config.Labels,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&registry)
}

registry := ContainerRegistry{RegistryURL: config.RegistryUrl,
ID: uuid.MustParse(config.Id),
RegistryType: config.RegistryType,
Labels: config.Labels,
LastUpdateTime: time.Now()}
return a.dbClient.Update(&registry, ContainerRegistry{ID: registry.ID})
}

func (a *Store) GetContainerRegistryForID(id string) (*captenpluginspb.ContainerRegistry, error) {
registry := ContainerRegistry{}
err := a.dbClient.Find(&registry, ContainerRegistry{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}

result := &captenpluginspb.ContainerRegistry{
Id: registry.ID.String(),
RegistryUrl: registry.RegistryURL,
RegistryType: registry.RegistryType,
Labels: registry.Labels,
LastUpdateTime: registry.LastUpdateTime.String(),
}
return result, err
}

func (a *Store) GetContainerRegistries() ([]*captenpluginspb.ContainerRegistry, error) {
registries := []ContainerRegistry{}
err := a.dbClient.Find(&registries, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch registries: %v", err.Error())
}

result := make([]*captenpluginspb.ContainerRegistry, 0)
for _, registry := range registries {
result = append(result, &captenpluginspb.ContainerRegistry{
Id: registry.ID.String(),
RegistryUrl: registry.RegistryURL,
RegistryType: registry.RegistryType,
Labels: registry.Labels,
LastUpdateTime: registry.LastUpdateTime.String(),
})
}
return result, err
}

func (a *Store) GetContainerRegistriesByLabels(searchLabels []string) ([]*captenpluginspb.ContainerRegistry, error) {
registries := []ContainerRegistry{}
err := a.dbClient.Find(&registries, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch registries: %v", err.Error())
}

result := make([]*captenpluginspb.ContainerRegistry, 0)
for _, registry := range registries {
result = append(result, &captenpluginspb.ContainerRegistry{
Id: registry.ID.String(),
RegistryUrl: registry.RegistryURL,
RegistryType: registry.RegistryType,
Labels: registry.Labels,
LastUpdateTime: registry.LastUpdateTime.String(),
})
}
return result, err
}

func (a *Store) DeleteContainerRegistryById(id string) error {
err := a.dbClient.Delete(ContainerRegistry{}, ContainerRegistry{ID: uuid.MustParse(id)})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}
123 changes: 123 additions & 0 deletions capten/common-pkg/capten-store/crossplane_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func (a *Store) UpsertCrossplaneProject(crossplaneProject *model.CrossplaneProject) error {
gitProjectUUID, err := uuid.Parse(crossplaneProject.GitProjectId)
if err != nil {
return err
}

if crossplaneProject.Id == "" {
project := CrossplaneProject{
ID: 1,
GitProjectID: gitProjectUUID,
GitProjectURL: crossplaneProject.GitProjectUrl,
Status: crossplaneProject.Status,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&project)
}

project := CrossplaneProject{
GitProjectID: gitProjectUUID,
GitProjectURL: crossplaneProject.GitProjectUrl,
Status: crossplaneProject.Status,
LastUpdateTime: time.Now()}
return a.dbClient.Update(project, CrossplaneProject{ID: 1})
}

func (a *Store) DeleteCrossplaneProject(id string) error {
err := a.dbClient.Delete(CrossplaneProject{}, CrossplaneProject{ID: 1})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}

func (a *Store) GetCrossplaneProjectForID(id string) (*model.CrossplaneProject, error) {
project := CrossplaneProject{}
err := a.dbClient.Find(&project, CrossplaneProject{ID: 1})
if err != nil {
return nil, err
} else if project.ID == 0 {
return nil, gorm.ErrRecordNotFound
}

crossplaneProject := &model.CrossplaneProject{
Id: "1",
GitProjectId: project.GitProjectID.String(),
GitProjectUrl: project.GitProjectURL,
Status: project.Status,
LastUpdateTime: project.LastUpdateTime.String(),
}
return crossplaneProject, err
}

func (a *Store) GetCrossplaneProject() (*model.CrossplaneProject, error) {
return a.updateCrossplaneProject()
}

func (a *Store) updateCrossplaneProject() (*model.CrossplaneProject, error) {
allCrossplaneGitProjects, err := a.GetGitProjectsByLabels([]string{"crossplane"})
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

if len(allCrossplaneGitProjects) == 0 {
return nil, fmt.Errorf("no git project found with crossplane tag")
}
crosplaneGitProject := allCrossplaneGitProjects[0]
gitProjectUUID, err := uuid.Parse(crosplaneGitProject.Id)
if err != nil {
return nil, err
}

crossplaneProject, err := a.GetCrossplaneProjectForID("0")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
project := CrossplaneProject{
ID: 1,
GitProjectID: gitProjectUUID,
GitProjectURL: crosplaneGitProject.ProjectUrl,
LastUpdateTime: time.Now(),
}
err = a.dbClient.Create(&project)
if err != nil {
return nil, err
}
return a.GetCrossplaneProjectForID("1")
} else {
return nil, err
}

}

if crossplaneProject.GitProjectId == crosplaneGitProject.Id &&
crossplaneProject.GitProjectUrl == crosplaneGitProject.ProjectUrl {
return crossplaneProject, nil
}

project := CrossplaneProject{GitProjectID: gitProjectUUID,
GitProjectURL: crosplaneGitProject.ProjectUrl,
Status: crossplaneProject.Status,
LastUpdateTime: time.Now()}

err = a.dbClient.Update(&project, CrossplaneProject{ID: 1})
if err != nil {
return nil, err
}

// project already registered, return that
return a.GetCrossplaneProjectForID("1")
}
104 changes: 104 additions & 0 deletions capten/common-pkg/capten-store/crossplane_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/kube-tarian/kad/capten/model"
)

func (a *Store) UpsertCrossplaneProvider(crossplaneProvider *model.CrossplaneProvider) error {
if crossplaneProvider.Id == "" {
provider := CrossplaneProvider{
ID: uuid.New(),
ProviderName: crossplaneProvider.ProviderName,
CloudProviderID: crossplaneProvider.CloudProviderId,
CloudType: crossplaneProvider.CloudType,
Status: crossplaneProvider.Status,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&provider)
}

provider := CrossplaneProvider{CloudType: crossplaneProvider.CloudType,
CloudProviderID: crossplaneProvider.CloudProviderId,
Status: crossplaneProvider.Status,
LastUpdateTime: time.Now()}

return a.dbClient.Update(provider, CrossplaneProvider{ID: uuid.MustParse(crossplaneProvider.Id)})
}

func (a *Store) DeleteCrossplaneProviderById(id string) error {
err := a.dbClient.Delete(CrossplaneProvider{}, CrossplaneProvider{ID: uuid.MustParse(id)})
return err
}

func (a *Store) GetCrossplaneProviders() ([]*captenpluginspb.CrossplaneProvider, error) {
providers := []CrossplaneProvider{}
err := a.dbClient.Find(&providers, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

crossplaneProviders := []*captenpluginspb.CrossplaneProvider{}
for _, provider := range providers {
crossplaneProviders = append(crossplaneProviders, &captenpluginspb.CrossplaneProvider{
Id: provider.ID.String(),
CloudProviderId: provider.CloudProviderID,
CloudType: provider.CloudType,
Status: provider.Status,
})
}
return crossplaneProviders, nil
}

func (a *Store) UpdateCrossplaneProvider(provider *model.CrossplaneProvider) error {
crossplaneProvider := CrossplaneProvider{CloudType: provider.CloudType,
CloudProviderID: provider.CloudProviderId,
Status: provider.Status,
LastUpdateTime: time.Now()}

return a.dbClient.Update(crossplaneProvider, CrossplaneProvider{ID: uuid.MustParse(provider.Id)})
}

func (a *Store) GetCrossplanProviderByCloudType(cloudType string) (*captenpluginspb.CrossplaneProvider, error) {
providers := []CrossplaneProvider{}
err := a.dbClient.Find(&providers, "cloud_type = ?", cloudType)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

if len(providers) == 1 {
provider := providers[0]
crossplaneProvider := &captenpluginspb.CrossplaneProvider{
Id: provider.ID.String(),
ProviderName: provider.ProviderName,
CloudProviderId: provider.CloudProviderID,
CloudType: provider.CloudType,
Status: provider.Status,
}
return crossplaneProvider, err
}
return nil, gerrors.New(postgresdb.ObjectNotExist, "Crossplane provider not found")
}

func (a *Store) GetCrossplanProviderById(id string) (*captenpluginspb.CrossplaneProvider, error) {
provider := CrossplaneProvider{}
err := a.dbClient.Find(&provider, CrossplaneProvider{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}

crossplaneProvider := &captenpluginspb.CrossplaneProvider{
Id: provider.ID.String(),
ProviderName: provider.ProviderName,
CloudProviderId: provider.CloudProviderID,
CloudType: provider.CloudType,
Status: provider.Status,
}
return crossplaneProvider, err
}
92 changes: 92 additions & 0 deletions capten/common-pkg/capten-store/git_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

func (a *Store) UpsertGitProject(config *captenpluginspb.GitProject) error {
if config.Id == "" {
project := GitProject{
ID: uuid.New(),
ProjectURL: config.ProjectUrl,
Labels: config.Labels,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&project)
}

project := GitProject{
ProjectURL: config.ProjectUrl,
Labels: config.Labels,
LastUpdateTime: time.Now()}
return a.dbClient.Update(project, GitProject{ID: uuid.MustParse(config.Id)})
}

func (a *Store) DeleteGitProjectById(id string) error {
err := a.dbClient.Delete(GitProject{}, GitProject{ID: uuid.MustParse(id)})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}

func (a *Store) GetGitProjectForID(id string) (*captenpluginspb.GitProject, error) {
project := GitProject{}
err := a.dbClient.Find(&project, GitProject{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}

gitProject := &captenpluginspb.GitProject{
Id: project.ID.String(),
ProjectUrl: project.ProjectURL,
Labels: project.Labels,
LastUpdateTime: project.LastUpdateTime.String(),
}
return gitProject, err
}

func (a *Store) GetGitProjects() ([]*captenpluginspb.GitProject, error) {
projects := []GitProject{}
err := a.dbClient.Find(&projects, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

gitProjects := make([]*captenpluginspb.GitProject, 0)
for _, project := range projects {
gitProjects = append(gitProjects, &captenpluginspb.GitProject{
Id: project.ID.String(),
ProjectUrl: project.ProjectURL,
Labels: project.Labels,
LastUpdateTime: project.LastUpdateTime.String(),
})
}
return gitProjects, err
}

func (a *Store) GetGitProjectsByLabels(searchLabels []string) ([]*captenpluginspb.GitProject, error) {
projects := []GitProject{}
err := a.dbClient.Find(&projects, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

gitProjects := make([]*captenpluginspb.GitProject, 0)
for _, project := range projects {
gitProjects = append(gitProjects, &captenpluginspb.GitProject{
Id: project.ID.String(),
ProjectUrl: project.ProjectURL,
Labels: project.Labels,
LastUpdateTime: project.LastUpdateTime.String(),
})
}

return gitProjects, err
}
744 changes: 744 additions & 0 deletions capten/common-pkg/capten-store/integration_store_test.go

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions capten/common-pkg/capten-store/managed_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"gorm.io/gorm"
)

func (a *Store) UpsertManagedCluster(managedCluster *captenpluginspb.ManagedCluster) error {
if managedCluster.Id == "" {
cluster := ManagedCluster{
ID: uuid.New(),
ClusterName: managedCluster.ClusterName,
ClusterEndpoint: managedCluster.ClusterEndpoint,
ClusterDeployStatus: managedCluster.ClusterDeployStatus,
AppDeployStatus: managedCluster.AppDeployStatus,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&cluster)
}

cluster := ManagedCluster{ClusterName: managedCluster.ClusterName,
ClusterEndpoint: managedCluster.ClusterEndpoint,
ClusterDeployStatus: managedCluster.ClusterDeployStatus,
AppDeployStatus: managedCluster.AppDeployStatus,
LastUpdateTime: time.Now()}
return a.dbClient.Update(&cluster, ManagedCluster{ID: uuid.MustParse(managedCluster.Id)})
}

func (a *Store) DeleteManagedClusterById(id string) error {
err := a.dbClient.Delete(ManagedCluster{}, ManagedCluster{ID: uuid.MustParse(id)})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}

func (a *Store) GetManagedClusterForID(id string) (*captenpluginspb.ManagedCluster, error) {
cluster := ManagedCluster{}
err := a.dbClient.Find(&cluster, ManagedCluster{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
} else if cluster.ID == uuid.Nil {
fmt.Printf("cluster %s, %v", id, cluster)
return nil, gorm.ErrRecordNotFound
}

result := &captenpluginspb.ManagedCluster{
Id: cluster.ID.String(),
ClusterName: cluster.ClusterName,
ClusterEndpoint: cluster.ClusterEndpoint,
ClusterDeployStatus: cluster.ClusterDeployStatus,
AppDeployStatus: cluster.AppDeployStatus,
LastUpdateTime: cluster.LastUpdateTime.String(),
}
return result, err
}

func (a *Store) GetManagedClusters() ([]*captenpluginspb.ManagedCluster, error) {
clusters := []ManagedCluster{}
err := a.dbClient.Find(&clusters, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch clusters: %v", err.Error())
}

result := []*captenpluginspb.ManagedCluster{}
for _, cluster := range clusters {
result = append(result, &captenpluginspb.ManagedCluster{
Id: cluster.ID.String(),
ClusterName: cluster.ClusterName,
ClusterEndpoint: cluster.ClusterEndpoint,
ClusterDeployStatus: cluster.ClusterDeployStatus,
AppDeployStatus: cluster.AppDeployStatus,
LastUpdateTime: cluster.LastUpdateTime.String(),
})
}

return result, err
}
189 changes: 189 additions & 0 deletions capten/common-pkg/capten-store/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package captenstore

import (
"strings"
"time"

"database/sql/driver"

"github.com/google/uuid"
)

// StringArray represents a string array to handle TEXT[] data type
type StringArray []string

// GormDataType returns the data type of the field
func (StringArray) GormDataType() string {
return "text[]"
}

// Value gets the value to store in the database
func (a StringArray) Value() (driver.Value, error) {
var arr = "{" + a[0]
for _, v := range a[1:] {
arr += "," + v
}
arr += "}"
return arr, nil
}

func (a StringArray) String() string {
var arr = "{\"" + a[0]
for _, v := range a[1:] {
arr += "\",\"" + v
}
arr += "\"}"
return arr
}

// Scan reads the value from the database
func (a *StringArray) Scan(value interface{}) error {
if value == nil {
return nil
}

input := strings.Trim(value.(string), "{}")
parts := strings.Split(input, ",")
for i, part := range parts {
parts[i] = strings.TrimSpace(part)
}
*a = parts
return nil
}

type GitProject struct {
ID uuid.UUID `json:"id" gorm:"column:id;primaryKey"`
ProjectURL string `json:"project_url" gorm:"column:project_url"`
Labels StringArray `json:"labels" gorm:"column:labels;type:text[]"`
LastUpdateTime time.Time `json:"last_update_time column:last_update_time"`
}

func (GitProject) TableName() string {
return "git_project"
}

type CloudProvider struct {
ID uuid.UUID `json:"id" gorm:"column:id;type:uuid"`
CloudType string `json:"cloud_type" gorm:"column:cloud_type"`
Labels StringArray `json:"labels" gorm:"column:labels;type:text[]"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (CloudProvider) TableName() string {
return "cloud_provider"
}

type ContainerRegistry struct {
ID uuid.UUID `json:"id" gorm:"column:id;primaryKey"`
RegistryURL string `json:"registry_url" gorm:"column:registry_url"`
RegistryType string `json:"registry_type" gorm:"column:registry_type"`
Labels StringArray `json:"labels" gorm:"column:labels;type:text[]"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (ContainerRegistry) TableName() string {
return "container_registry"
}

type ClusterAppConfig struct {
ReleaseName string `json:"release_name" gorm:"column:release_name;primaryKey"`
AppName string `json:"app_name" gorm:"column:app_name"`
PluginName string `json:"plugin_name" gorm:"column:plugin_name"`
PluginStoreType int `json:"plugin_store_type" gorm:"column:plugin_store_type"`
Category string `json:"category" gorm:"column:category"`
Description string `json:"description" gorm:"column:description"`
RepoURL string `json:"repo_url" gorm:"column:repo_url"`
Version string `json:"version" gorm:"column:version"`
Namespace string `json:"namespace" gorm:"column:namespace"`
UIEndpoint string `json:"ui_endpoint" gorm:"column:ui_endpoint"`
UIModuleEndpoint string `json:"ui_module_endpoint" gorm:"column:ui_module_endpoint"`
APIEndpoint string `json:"api_endpoint" gorm:"column:api_endpoint"`
DefaultApp bool `json:"default_app" gorm:"column:default_app"`
PrivilegedNamespace bool `json:"privileged_namespace" gorm:"column:privileged_namespace"`
InstallStatus string `json:"install_status" gorm:"column:install_status"`
Icon []byte `json:"icon" gorm:"column:icon"`
OverrideValues string `json:"override_values" gorm:"column:override_values"`
LaunchUIValues string `json:"launch_ui_values" gorm:"column:launch_ui_values"`
TemplateValues string `json:"template_values" gorm:"column:template_values"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (ClusterAppConfig) TableName() string {
return "cluster_app_config"
}

type ClusterPluginConfig struct {
PluginName string `json:"plugin_name" gorm:"column:plugin_name;primaryKey"`
PluginStoreType int `json:"plugin_store_type" gorm:"column:plugin_store_type"`
Capabilities StringArray `json:"capabilities" gorm:"column:capabilities;type:text[]"`
Category string `json:"category" gorm:"column:category"`
Description string `json:"description" gorm:"column:description"`
ChartName string `json:"chart_name" gorm:"column:chart_name"`
ChartRepo string `json:"chart_repo" gorm:"column:chart_repo"`
Version string `json:"version" gorm:"column:version"`
Namespace string `json:"namespace" gorm:"column:namespace"`
UIEndpoint string `json:"ui_endpoint" gorm:"column:ui_endpoint"`
APIEndpoint string `json:"api_endpoint" gorm:"column:api_endpoint"`
UIModuleEndpoint string `json:"ui_module_endpoint" gorm:"column:ui_module_endpoint"`
DefaultApp bool `json:"default_app" gorm:"column:default_app"`
PrivilegedNamespace bool `json:"privileged_namespace" gorm:"column:privileged_namespace"`
InstallStatus string `json:"install_status" gorm:"column:install_status"`
Icon []byte `json:"icon" gorm:"column:icon"`
OverrideValues string `json:"override_values" gorm:"column:override_values"`
Values string `json:"values" gorm:"column:values"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (ClusterPluginConfig) TableName() string {
return "cluster_plugin_config"
}

type ManagedCluster struct {
ID uuid.UUID `json:"id" gorm:"column:id;primaryKey"`
ClusterName string `json:"cluster_name" gorm:"column:cluster_name"`
ClusterEndpoint string `json:"cluster_endpoint" gorm:"column:cluster_endpoint"`
ClusterDeployStatus string `json:"cluster_deploy_status" gorm:"column:cluster_deploy_status"`
AppDeployStatus string `json:"app_deploy_status" gorm:"column:app_deploy_status"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (ManagedCluster) TableName() string {
return "managed_clusters"
}

type TektonProject struct {
ID int `json:"id" gorm:"column:id;primaryKey"`
GitProjectID uuid.UUID `json:"git_project_id" gorm:"column:git_project_id"`
GitProjectURL string `json:"git_project_url" gorm:"column:git_project_url"`
Status string `json:"status" gorm:"column:status"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (TektonProject) TableName() string {
return "tekton_project"
}

type CrossplaneProvider struct {
ID uuid.UUID `json:"id" gorm:"column:id;primaryKey"`
CloudProviderID string `json:"cloud_provider_id" gorm:"column:cloud_provider_id"`
ProviderName string `json:"provider_name" gorm:"column:provider_name"`
CloudType string `json:"cloud_type" gorm:"column:cloud_type"`
Status string `json:"status" gorm:"column:status"`
LastUpdateTime time.Time `json:"last_update_time" gorm:"column:last_update_time"`
}

func (CrossplaneProvider) TableName() string {
return "crossplane_provider"
}

type CrossplaneProject struct {
ID int `json:"id"`
GitProjectID uuid.UUID `json:"git_project_id"`
GitProjectURL string `json:"git_project_url"`
Status string `json:"status"`
LastUpdateTime time.Time `json:"last_update_time"`
}

func (CrossplaneProject) TableName() string {
return "crossplane_project"
}
35 changes: 35 additions & 0 deletions capten/common-pkg/capten-store/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package captenstore

import (
"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

type Store struct {
dbClient *postgresdb.DBClient
log logging.Logger
}

func NewStore(log logging.Logger) (*Store, error) {
dbClient, err := postgresdb.NewDBClient(log)
if err != nil {
return nil, err
}
return &Store{log: log, dbClient: dbClient}, nil
}

func prepareError(err error, name string, operationLog string) (returnErr error) {
if gErr, ok := err.(gerrors.Gerror); ok {
switch gerrors.GetErrorType(gErr) {
case postgresdb.ObjectNotExist:
returnErr = gerrors.Newf(gerrors.NotFound, "%s failed for %s not exist", operationLog, name)
case postgresdb.DuplicateRecord:
returnErr = gerrors.Newf(gerrors.RecordAlreadyExists, "%s failed, '%s' already exists", operationLog, name)
case postgresdb.PostgresDBError:
returnErr = gerrors.Newf(gerrors.InternalError, "%s failed for '%s', Reason: %v", operationLog, name, err.Error())
}
return
}
return err
}
120 changes: 120 additions & 0 deletions capten/common-pkg/capten-store/tekton_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package captenstore

import (
"fmt"
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func (a *Store) UpsertTektonProject(tektonProject *model.TektonProject) error {
gitProjectUUID, err := uuid.Parse(tektonProject.GitProjectId)
if err != nil {
return err
}

if tektonProject.Id == "" {
project := TektonProject{
ID: 1,
GitProjectID: gitProjectUUID,
GitProjectURL: tektonProject.GitProjectUrl,
Status: tektonProject.Status,
LastUpdateTime: time.Now(),
}
return a.dbClient.Create(&project)
}

project := TektonProject{GitProjectID: gitProjectUUID,
GitProjectURL: tektonProject.GitProjectUrl,
Status: tektonProject.Status,
LastUpdateTime: time.Now()}
return a.dbClient.Update(project, TektonProject{ID: 1})
}

func (a *Store) DeleteTektonProject(id string) error {
err := a.dbClient.Delete(TektonProject{}, TektonProject{ID: 1})
if err != nil {
err = prepareError(err, id, "Delete")
}
return err
}

func (a *Store) GetTektonProjectForID(id string) (*model.TektonProject, error) {
project := TektonProject{}
err := a.dbClient.Find(&project, TektonProject{ID: 1})
if err != nil {
return nil, err
} else if project.ID == 0 {
return nil, gorm.ErrRecordNotFound
}

tektonProject := &model.TektonProject{
Id: "1",
GitProjectId: project.GitProjectID.String(),
GitProjectUrl: project.GitProjectURL,
Status: project.Status,
LastUpdateTime: project.LastUpdateTime.String(),
}
return tektonProject, err
}

func (a *Store) GetTektonProject() (*model.TektonProject, error) {
return a.updateTektonProject()
}

func (a *Store) updateTektonProject() (*model.TektonProject, error) {
allTektonGitProjects, err := a.GetGitProjectsByLabels([]string{"tekton"})
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

if len(allTektonGitProjects) == 0 {
return nil, fmt.Errorf("no git project found with tekton tag")
}
tektonGitProject := allTektonGitProjects[0]
gitProjectUUID, err := uuid.Parse(tektonGitProject.Id)
if err != nil {
return nil, err
}

tektonProject, err := a.GetTektonProjectForID("1")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
project := TektonProject{
ID: 1,
GitProjectID: gitProjectUUID,
GitProjectURL: tektonGitProject.ProjectUrl,
LastUpdateTime: time.Now(),
}
err = a.dbClient.Create(&project)
if err != nil {
return nil, err
}
return a.GetTektonProjectForID("1")
} else {
return nil, err
}
}

if tektonProject.GitProjectId == tektonGitProject.Id &&
tektonProject.GitProjectUrl == tektonGitProject.ProjectUrl {
return tektonProject, nil
}

project := TektonProject{GitProjectID: gitProjectUUID,
GitProjectURL: tektonGitProject.ProjectUrl,
Status: tektonProject.Status,
LastUpdateTime: time.Now()}
err = a.dbClient.Update(&project, TektonProject{ID: 1})
if err != nil {
return nil, err
}

// project already registered, return that
return a.GetTektonProjectForID("1")
}
Loading

0 comments on commit a56409c

Please sign in to comment.