Skip to content

Commit

Permalink
handle db insert when first resource add cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vramk23 committed May 30, 2024
1 parent 0f8e275 commit 5c956eb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 7 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
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
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
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
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
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 5c956eb

Please sign in to comment.