From 943f6874f379b42c51a0d8b20e0331b18764d325 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 --- .../agent/internal/api/container_registry.go | 2 +- .../internal/api/plugin_cloud_provider_apis.go | 2 +- .../api/plugin_crossplane_provider_apis.go | 2 +- capten/agent/internal/api/plugin_git_apis.go | 2 +- .../common-pkg/capten-store/cloud_provider.go | 10 ++++++++++ .../capten-store/container_registry.go | 11 +++++++++++ .../capten-store/crossplane_provider.go | 12 ++++++++++++ capten/common-pkg/capten-store/git_projects.go | 10 ++++++++++ .../common-pkg/capten-store/managed_cluster.go | 12 ++++++++++++ capten/common-pkg/capten-store/model.go | 4 ++-- capten/common-pkg/crossplane/cluster_claims.go | 18 ++++++++++++++---- .../plugin-store/plugin_store_handler.go | 5 +++-- charts/kad/Chart.yaml | 4 ++-- charts/kad/templates/agent-deployment.yaml | 8 ++++++++ charts/kad/values.yaml | 1 + 15 files changed, 89 insertions(+), 14 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_crossplane_provider_apis.go b/capten/agent/internal/api/plugin_crossplane_provider_apis.go index 5bb4974b..82fc8982 100644 --- a/capten/agent/internal/api/plugin_crossplane_provider_apis.go +++ b/capten/agent/internal/api/plugin_crossplane_provider_apis.go @@ -47,7 +47,7 @@ func (a *Agent) AddCrossplanProvider(ctx context.Context, request *captenplugins Status: string(model.CrossPlaneProviderOutofSynch), } - if err := a.as.UpsertCrossplaneProvider(&provider); err != nil { + if err := a.as.AddCrossplaneProvider(&provider); err != nil { a.log.Errorf("failed to store crossplane provider to DB, %v", err) return &captenpluginspb.AddCrossplanProviderResponse{ 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/crossplane_provider.go b/capten/common-pkg/capten-store/crossplane_provider.go index bdd690e6..e29e4ad0 100644 --- a/capten/common-pkg/capten-store/crossplane_provider.go +++ b/capten/common-pkg/capten-store/crossplane_provider.go @@ -11,6 +11,18 @@ import ( "github.com/kube-tarian/kad/capten/model" ) +func (a *Store) AddCrossplaneProvider(crossplaneProvider *model.CrossplaneProvider) error { + provider := CrossplaneProvider{ + ID: uuid.MustParse(crossplaneProvider.Id), + ProviderName: crossplaneProvider.ProviderName, + CloudProviderID: crossplaneProvider.CloudProviderId, + CloudType: crossplaneProvider.CloudType, + Status: crossplaneProvider.Status, + LastUpdateTime: time.Now(), + } + return a.dbClient.Create(&provider) +} + func (a *Store) UpsertCrossplaneProvider(crossplaneProvider *model.CrossplaneProvider) error { if crossplaneProvider.Id == "" { provider := CrossplaneProvider{ 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/managed_cluster.go b/capten/common-pkg/capten-store/managed_cluster.go index 47b158e4..2dde8a66 100644 --- a/capten/common-pkg/capten-store/managed_cluster.go +++ b/capten/common-pkg/capten-store/managed_cluster.go @@ -8,6 +8,18 @@ import ( "github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb" ) +func (a *Store) AddManagedCluster(managedCluster *captenpluginspb.ManagedCluster) error { + cluster := ManagedCluster{ + ID: uuid.MustParse(managedCluster.Id), + ClusterName: managedCluster.ClusterName, + ClusterEndpoint: managedCluster.ClusterEndpoint, + ClusterDeployStatus: managedCluster.ClusterDeployStatus, + AppDeployStatus: managedCluster.AppDeployStatus, + LastUpdateTime: time.Now(), + } + return a.dbClient.Create(&cluster) +} + func (a *Store) UpsertManagedCluster(managedCluster *captenpluginspb.ManagedCluster) error { if managedCluster.Id == "" { cluster := ManagedCluster{ 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/crossplane/cluster_claims.go b/capten/common-pkg/crossplane/cluster_claims.go index 8fdc6838..d61a4129 100644 --- a/capten/common-pkg/crossplane/cluster_claims.go +++ b/capten/common-pkg/crossplane/cluster_claims.go @@ -199,9 +199,11 @@ func (h *ClusterClaimSyncHandler) updateManagedClusters(clusterCliams []model.Cl managedCluster := &captenpluginspb.ManagedCluster{} managedCluster.ClusterName = clusterCliam.Metadata.Name + createCluster := false clusterObj, ok := clusters[managedCluster.ClusterName] if !ok { managedCluster.Id = uuid.New().String() + createCluster = true } else { h.log.Infof("found existing managed clusterId %s, updating", clusterObj.Id) managedCluster.Id = clusterObj.Id @@ -221,10 +223,18 @@ func (h *ClusterClaimSyncHandler) updateManagedClusters(clusterCliams []model.Cl } managedCluster.ClusterEndpoint = k8sEndpoint - err = h.dbStore.UpsertManagedCluster(managedCluster) - if err != nil { - h.log.Info("failed to update information to db, %v", err) - continue + if createCluster { + err = h.dbStore.AddManagedCluster(managedCluster) + if err != nil { + h.log.Info("failed to update information to db, %v", err) + continue + } + } else { + err = h.dbStore.UpsertManagedCluster(managedCluster) + if err != nil { + h.log.Info("failed to update information to db, %v", err) + continue + } } err = vaultcred.RegisterClusterVaultAuth(managedCluster.Id, managedCluster.ClusterName) 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/Chart.yaml b/charts/kad/Chart.yaml index faa5c6c3..ebe951ee 100644 --- a/charts/kad/Chart.yaml +++ b/charts/kad/Chart.yaml @@ -15,10 +15,10 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 1.0.3 +version: 1.0.4 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "2.0.1" +appVersion: "2.0.2" 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