Skip to content

Commit

Permalink
Merge pull request #500 from kube-tarian/postgres-fix
Browse files Browse the repository at this point in the history
handle db insert when first resource add cases
  • Loading branch information
vramk23 authored May 30, 2024
2 parents 0f8e275 + 943f687 commit 1a3da59
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 14 deletions.
2 changes: 1 addition & 1 deletion capten/agent/internal/api/container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion capten/agent/internal/api/plugin_git_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions capten/common-pkg/capten-store/cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
11 changes: 11 additions & 0 deletions capten/common-pkg/capten-store/container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registry)
}

func (a *Store) UpsertContainerRegistry(config *captenpluginspb.ContainerRegistry) error {
if config.Id == "" {
registry := ContainerRegistry{
Expand Down
12 changes: 12 additions & 0 deletions capten/common-pkg/capten-store/crossplane_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 10 additions & 0 deletions capten/common-pkg/capten-store/git_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
12 changes: 12 additions & 0 deletions capten/common-pkg/capten-store/managed_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions capten/common-pkg/capten-store/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
18 changes: 14 additions & 4 deletions capten/common-pkg/crossplane/cluster_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions capten/common-pkg/plugin-store/plugin_store_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions charts/kad/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 8 additions & 0 deletions charts/kad/templates/agent-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions charts/kad/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ securityContext:

env:
logLevel: info
pluginsStoreProjectMount: /plugins-store

service:
type: ClusterIP
Expand Down

0 comments on commit 1a3da59

Please sign in to comment.