-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #460 from kube-tarian/postgres-migrate
migrate database from cassandra to postgres
Showing
170 changed files
with
3,782 additions
and
6,568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
124 changes: 0 additions & 124 deletions
124
capten/agent/internal/capten-store/container_registry.go
This file was deleted.
Oops, something went wrong.
145 changes: 0 additions & 145 deletions
145
capten/agent/internal/capten-store/crossplane_project.go
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
140
capten/common-pkg/capten-store/cluster_plugin_config_store.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®istry) | ||
} | ||
|
||
registry := ContainerRegistry{RegistryURL: config.RegistryUrl, | ||
ID: uuid.MustParse(config.Id), | ||
RegistryType: config.RegistryType, | ||
Labels: config.Labels, | ||
LastUpdateTime: time.Now()} | ||
return a.dbClient.Update(®istry, ContainerRegistry{ID: registry.ID}) | ||
} | ||
|
||
func (a *Store) GetContainerRegistryForID(id string) (*captenpluginspb.ContainerRegistry, error) { | ||
registry := ContainerRegistry{} | ||
err := a.dbClient.Find(®istry, 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(®istries, 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(®istries, "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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
744
capten/common-pkg/capten-store/integration_store_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.