From 5c956eb7abc90d71c6ac5d56d483f029bd5b53bf Mon Sep 17 00:00:00 2001 From: venkat k Date: Thu, 30 May 2024 22:14:38 +0530 Subject: [PATCH] handle db insert when first resource add cases --- capten/agent/internal/api/container_registry.go | 2 +- .../agent/internal/api/plugin_cloud_provider_apis.go | 2 +- capten/agent/internal/api/plugin_git_apis.go | 2 +- capten/common-pkg/capten-store/cloud_provider.go | 10 ++++++++++ capten/common-pkg/capten-store/container_registry.go | 11 +++++++++++ capten/common-pkg/capten-store/git_projects.go | 10 ++++++++++ capten/common-pkg/capten-store/model.go | 4 ++-- .../common-pkg/plugin-store/plugin_store_handler.go | 5 +++-- charts/kad/templates/agent-deployment.yaml | 8 ++++++++ charts/kad/values.yaml | 1 + 10 files changed, 48 insertions(+), 7 deletions(-) diff --git a/capten/agent/internal/api/container_registry.go b/capten/agent/internal/api/container_registry.go index 40ea028c..0a43d8e0 100644 --- a/capten/agent/internal/api/container_registry.go +++ b/capten/agent/internal/api/container_registry.go @@ -63,7 +63,7 @@ func (a *Agent) AddContainerRegistry(ctx context.Context, request *captenplugins Labels: request.Labels, RegistryType: request.RegistryType, } - if err := a.as.UpsertContainerRegistry(&ContainerRegistry); err != nil { + if err := a.as.AddContainerRegistry(&ContainerRegistry); err != nil { a.log.Errorf("failed to store Container registry to DB, %v", err) return &captenpluginspb.AddContainerRegistryResponse{ Status: captenpluginspb.StatusCode_INTERNAL_ERROR, diff --git a/capten/agent/internal/api/plugin_cloud_provider_apis.go b/capten/agent/internal/api/plugin_cloud_provider_apis.go index 51253895..25ade4c6 100644 --- a/capten/agent/internal/api/plugin_cloud_provider_apis.go +++ b/capten/agent/internal/api/plugin_cloud_provider_apis.go @@ -36,7 +36,7 @@ func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.A CloudType: request.CloudType, Labels: request.Labels, } - if err := a.as.UpsertCloudProvider(&CloudProvider); err != nil { + if err := a.as.AddCloudProvider(&CloudProvider); err != nil { a.log.Errorf("failed to store cloud provider to DB, %v", err) return &captenpluginspb.AddCloudProviderResponse{ Status: captenpluginspb.StatusCode_INTERNAL_ERROR, diff --git a/capten/agent/internal/api/plugin_git_apis.go b/capten/agent/internal/api/plugin_git_apis.go index 13ca8c8c..844cafd6 100644 --- a/capten/agent/internal/api/plugin_git_apis.go +++ b/capten/agent/internal/api/plugin_git_apis.go @@ -50,7 +50,7 @@ func (a *Agent) AddGitProject(ctx context.Context, request *captenpluginspb.AddG ProjectUrl: request.ProjectUrl, Labels: request.Labels, } - if err := a.as.UpsertGitProject(&gitProject); err != nil { + if err := a.as.AddGitProject(&gitProject); err != nil { a.log.Errorf("failed to store git project to DB, %v", err) return &captenpluginspb.AddGitProjectResponse{ Status: captenpluginspb.StatusCode_INTERNAL_ERROR, diff --git a/capten/common-pkg/capten-store/cloud_provider.go b/capten/common-pkg/capten-store/cloud_provider.go index 97ec8577..caacb6e7 100644 --- a/capten/common-pkg/capten-store/cloud_provider.go +++ b/capten/common-pkg/capten-store/cloud_provider.go @@ -8,6 +8,16 @@ import ( "github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb" ) +func (a *Store) AddCloudProvider(config *captenpluginspb.CloudProvider) error { + provider := CloudProvider{ + ID: uuid.MustParse(config.Id), + CloudType: config.CloudType, + Labels: config.Labels, + LastUpdateTime: time.Now(), + } + return a.dbClient.Create(&provider) +} + func (a *Store) UpsertCloudProvider(config *captenpluginspb.CloudProvider) error { if config.Id == "" { provider := CloudProvider{ diff --git a/capten/common-pkg/capten-store/container_registry.go b/capten/common-pkg/capten-store/container_registry.go index 954eebce..c7c4dbd0 100644 --- a/capten/common-pkg/capten-store/container_registry.go +++ b/capten/common-pkg/capten-store/container_registry.go @@ -8,6 +8,17 @@ import ( "github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb" ) +func (a *Store) AddContainerRegistry(config *captenpluginspb.ContainerRegistry) error { + registry := ContainerRegistry{ + ID: uuid.MustParse(config.Id), + RegistryURL: config.RegistryUrl, + RegistryType: config.RegistryType, + Labels: config.Labels, + LastUpdateTime: time.Now(), + } + return a.dbClient.Create(®istry) +} + func (a *Store) UpsertContainerRegistry(config *captenpluginspb.ContainerRegistry) error { if config.Id == "" { registry := ContainerRegistry{ diff --git a/capten/common-pkg/capten-store/git_projects.go b/capten/common-pkg/capten-store/git_projects.go index 3caa8070..7c933691 100644 --- a/capten/common-pkg/capten-store/git_projects.go +++ b/capten/common-pkg/capten-store/git_projects.go @@ -8,6 +8,16 @@ import ( "github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb" ) +func (a *Store) AddGitProject(config *captenpluginspb.GitProject) error { + project := GitProject{ + ID: uuid.MustParse(config.Id), + ProjectURL: config.ProjectUrl, + Labels: config.Labels, + LastUpdateTime: time.Now(), + } + return a.dbClient.Create(&project) +} + func (a *Store) UpsertGitProject(config *captenpluginspb.GitProject) error { if config.Id == "" { project := GitProject{ diff --git a/capten/common-pkg/capten-store/model.go b/capten/common-pkg/capten-store/model.go index be084dba..92020cc8 100644 --- a/capten/common-pkg/capten-store/model.go +++ b/capten/common-pkg/capten-store/model.go @@ -189,7 +189,7 @@ func (CrossplaneProject) TableName() string { } type PluginStoreConfig struct { - StoreType int `json:"id" gorm:"column:store_type;primaryKey"` + StoreType int `json:"store_type" gorm:"column:store_type;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"` @@ -201,7 +201,7 @@ func (PluginStoreConfig) TableName() string { } type PluginStoreData struct { - StoreType int `json:"id" gorm:"column:store_type;primaryKey"` + StoreType int `json:"store_type" gorm:"column:store_type;primaryKey"` GitProjectID uuid.UUID `json:"git_project_id" gorm:"column:git_project_id"` PluginName string `json:"plugin_name" gorm:"column:plugin_name"` Category string `json:"category" gorm:"column:category"` diff --git a/capten/common-pkg/plugin-store/plugin_store_handler.go b/capten/common-pkg/plugin-store/plugin_store_handler.go index 6492f095..628ee763 100644 --- a/capten/common-pkg/plugin-store/plugin_store_handler.go +++ b/capten/common-pkg/plugin-store/plugin_store_handler.go @@ -109,7 +109,7 @@ func (p *PluginStore) SyncPlugins(storeType pluginstorepb.StoreType) error { addedPlugins := map[string]bool{} for _, pluginName := range plugins.Plugins { - err := p.addPluginApp(config.GitProjectId, pluginStoreDir, pluginName) + err := p.addPluginApp(config.GitProjectId, pluginStoreDir, pluginName, storeType) if err != nil { p.log.Errorf("%v", err) continue @@ -161,7 +161,7 @@ func (p *PluginStore) clonePluginStoreProject(projectURL, projectId string, return } -func (p *PluginStore) addPluginApp(gitProjectId, pluginStoreDir, pluginName string) error { +func (p *PluginStore) addPluginApp(gitProjectId, pluginStoreDir, pluginName string, storeType pluginstorepb.StoreType) error { appData, err := os.ReadFile(p.getPluginFilePath(pluginStoreDir, pluginName)) if err != nil { return errors.WithMessagef(err, "failed to read store plugin %s", pluginName) @@ -185,6 +185,7 @@ func (p *PluginStore) addPluginApp(gitProjectId, pluginStoreDir, pluginName stri } plugin := &pluginstorepb.PluginData{ + StoreType: storeType, PluginName: pluginData.PluginName, Description: pluginData.Description, Category: pluginData.Category, diff --git a/charts/kad/templates/agent-deployment.yaml b/charts/kad/templates/agent-deployment.yaml index da97363e..2ccfd70a 100644 --- a/charts/kad/templates/agent-deployment.yaml +++ b/charts/kad/templates/agent-deployment.yaml @@ -30,6 +30,9 @@ spec: securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} serviceAccountName: {{ include "kad.serviceAccountName" . }} + volumes: + - name: plugin-store-clone-dir + emptyDir: {} containers: - name: {{ .Chart.Name }}-agent securityContext: @@ -88,8 +91,13 @@ spec: key: clusterIssuerName - name: PG_DB_ADMIN_CRED_IDENTIFIER value: {{ .Values.postgres.adminCredIdentifer }} + - name: PLUGIN_STORE_PROJECT_MOUNT + value: {{ .Values.env.pluginsStoreProjectMount }} resources: {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: plugin-store-clone-dir + mountPath: {{ .Values.env.pluginsStoreProjectMount }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} diff --git a/charts/kad/values.yaml b/charts/kad/values.yaml index a663f2ad..f0b56c6d 100644 --- a/charts/kad/values.yaml +++ b/charts/kad/values.yaml @@ -37,6 +37,7 @@ securityContext: env: logLevel: info + pluginsStoreProjectMount: /plugins-store service: type: ClusterIP