diff --git a/pkg/apis/api.kusion.io/v1/types.go b/pkg/apis/api.kusion.io/v1/types.go index 555a0d2a..575812c2 100644 --- a/pkg/apis/api.kusion.io/v1/types.go +++ b/pkg/apis/api.kusion.io/v1/types.go @@ -88,11 +88,11 @@ type Workspace struct { // Modules are the configs of a set of modules. Modules ModuleConfigs `yaml:"modules,omitempty" json:"modules,omitempty"` - // Runtimes are the configs of a set of runtimes. - Runtimes *RuntimeConfigs `yaml:"runtimes,omitempty" json:"runtimes,omitempty"` - // SecretStore represents a secure external location for storing secrets. SecretStore *SecretStoreSpec `yaml:"secretStore,omitempty" json:"secretStore,omitempty"` + + // Context contains workspace-level configurations, such as topologies, server endpoints, metadata, etc. + Context GenericConfig `yaml:"context,omitempty" json:"context,omitempty"` } // ModuleConfigs is a set of multiple ModuleConfig, whose key is the module name. @@ -114,17 +114,19 @@ type GenericConfig map[string]any // // Take the ModuleConfig of "mysql" for an example, which is shown as below: // -// config := ModuleConfig { -// "path": "ghcr.io/kusionstack/mysql" +// config := ModuleConfig{ +// "path": "ghcr.io/kusionstack/mysql" // "version": "0.1.0" -// "default": { -// "type": "aws", -// "version": "5.7", -// "instanceType": "db.t3.micro", -// }, -// "smallClass": { -// "instanceType": "db.t3.small", -// "projectSelector": []string{"foo", "bar"}, +// "configs": { +// "default": { +// "type": "aws", +// "version": "5.7", +// "instanceType": "db.t3.micro", +// }, +// "smallClass": { +// "instanceType": "db.t3.small", +// "projectSelector": []string{"foo", "bar"}, +// }, // }, // } type ModuleConfig struct { @@ -150,45 +152,10 @@ type ModulePatcherConfigs map[string]*ModulePatcherConfig type ModulePatcherConfig struct { // GenericConfig contains the module configs. GenericConfig `yaml:",inline" json:",inline"` - // ProjectSelector contains the selected projects. ProjectSelector []string `yaml:"projectSelector" json:"projectSelector"` } -// RuntimeConfigs contains a set of runtime config. -type RuntimeConfigs struct { - // Kubernetes contains the config to access a kubernetes cluster. - Kubernetes *KubernetesConfig `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"` - - // Terraform contains the config of multiple terraform providers. - Terraform TerraformConfig `yaml:"terraform,omitempty" json:"terraform,omitempty"` -} - -// KubernetesConfig contains config to access a kubernetes cluster. -type KubernetesConfig struct { - // KubeConfig is the path of the kubeconfig file. - KubeConfig string `yaml:"kubeConfig" json:"kubeConfig"` -} - -// TerraformConfig contains the config of multiple terraform provider config, whose key is -// the provider name. -type TerraformConfig map[string]*ProviderConfig - -// ProviderConfig contains the full configurations of a specified provider. It is the combination -// of the specified provider's config in blocks "terraform/required_providers" and "providers" in -// terraform hcl file, where the former is described by fields Source and Version, and the latter -// is described by GenericConfig cause different provider has different config. -type ProviderConfig struct { - // Source of the provider. - Source string `yaml:"source" json:"source"` - - // Version of the provider. - Version string `yaml:"version" json:"version"` - - // GenericConfig is used to describe the config of a specified terraform provider. - GenericConfig `yaml:",inline,omitempty" json:",inline,omitempty"` -} - type ExtensionKind string const ( @@ -358,7 +325,7 @@ type Resource struct { // providerNamespace:providerName:resourceType:resourceName for Terraform resources ID string `yaml:"id" json:"id"` - // Type represents all Runtimes we supported like Kubernetes and Terraform + // Type represents all Context we supported like Kubernetes and Terraform Type Type `yaml:"type" json:"type"` // Attributes represents all specified attributes of this resource diff --git a/pkg/engine/api/builders/appconfig_builder_test.go b/pkg/engine/api/builders/appconfig_builder_test.go index c86d3cf8..8e6d305a 100644 --- a/pkg/engine/api/builders/appconfig_builder_test.go +++ b/pkg/engine/api/builders/appconfig_builder_test.go @@ -76,9 +76,9 @@ func buildMockApp() (string, *internalv1.AppConfiguration) { func buildMockWorkspace() *v1.Workspace { return &v1.Workspace{ Name: "test", - Runtimes: &v1.RuntimeConfigs{ - Kubernetes: &v1.KubernetesConfig{ - KubeConfig: "/etc/kubeconfig.yaml", + Context: map[string]any{ + "Kubernetes": map[string]string{ + "Config": "/etc/kubeconfig.yaml", }, }, } diff --git a/pkg/modules/generators/app_configurations_generator.go b/pkg/modules/generators/app_configurations_generator.go index f5909588..50f54534 100644 --- a/pkg/modules/generators/app_configurations_generator.go +++ b/pkg/modules/generators/app_configurations_generator.go @@ -147,8 +147,6 @@ func (g *appConfigurationGenerator) Generate(spec *v1.Spec) error { return err } - // Add kubeConfig from workspace if exist - modules.AddKubeConfigIf(spec, g.ws) return nil } @@ -316,7 +314,6 @@ func (g *appConfigurationGenerator) callModules( } // parse module result - // todo extract to a method for _, res := range response.Resources { temp := &v1.Resource{} err = yaml.Unmarshal(res, temp) @@ -327,7 +324,6 @@ func (g *appConfigurationGenerator) callModules( } // parse patcher - // todo extract to a method for _, patcher := range response.Patchers { temp := &internalv1.Patcher{} err = yaml.Unmarshal(patcher, temp) diff --git a/pkg/modules/generators/app_configurations_generator_test.go b/pkg/modules/generators/app_configurations_generator_test.go index 86ba435e..a6f292a6 100644 --- a/pkg/modules/generators/app_configurations_generator_test.go +++ b/pkg/modules/generators/app_configurations_generator_test.go @@ -218,9 +218,9 @@ func buildMockWorkspace(namespace string) *v1.Workspace { }, }, }, - Runtimes: &v1.RuntimeConfigs{ - Kubernetes: &v1.KubernetesConfig{ - KubeConfig: "/etc/kubeconfig.yaml", + Context: map[string]any{ + "Kubernetes": map[string]string{ + "Config": "/etc/kubeconfig.yaml", }, }, } diff --git a/pkg/modules/plugin.go b/pkg/modules/plugin.go index 927422a6..99385c45 100644 --- a/pkg/modules/plugin.go +++ b/pkg/modules/plugin.go @@ -60,7 +60,7 @@ func (p *Plugin) initModule() error { key := p.key split := strings.Split(key, "@") msg := "init module failed. Invalid plugin module key: %s. " + - "The correct format for a key should be as follows: namespace/moduleName@version. e.g. kusionstack/mysql@v0.1.0" + "The correct format for a key should be as follows: org/moduleName@version. e.g. kusionstack/mysql@v0.1.0" if len(split) != 2 { return fmt.Errorf(msg, key) } diff --git a/pkg/modules/util.go b/pkg/modules/util.go index 4a35b35d..3e8554d8 100644 --- a/pkg/modules/util.go +++ b/pkg/modules/util.go @@ -8,7 +8,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" - "kusionstack.io/kusion/pkg/workspace" ) // CallGeneratorFuncs calls each NewGeneratorFunc in the given slice @@ -167,22 +166,3 @@ func PatchResource[T any](resources map[string][]*v1.Resource, gvk string, patch } return nil } - -// AddKubeConfigIf adds kubeConfig from workspace to extensions of Kubernetes type resource in intent. -// If there is already has kubeConfig in extensions, use the kubeConfig in extensions. -func AddKubeConfigIf(i *v1.Spec, ws *v1.Workspace) { - config := workspace.GetKubernetesConfig(ws.Runtimes) - if config == nil || config.KubeConfig == "" { - return - } - for n, resource := range i.Resources { - if resource.Type == v1.Kubernetes { - if resource.Extensions == nil { - i.Resources[n].Extensions = make(map[string]any) - } - if extensionsKubeConfig, ok := resource.Extensions[v1.ResourceExtensionKubeConfig]; !ok || extensionsKubeConfig == "" { - i.Resources[n].Extensions[v1.ResourceExtensionKubeConfig] = config.KubeConfig - } - } - } -} diff --git a/pkg/modules/util_test.go b/pkg/modules/util_test.go index 81e615d0..e783bb8e 100644 --- a/pkg/modules/util_test.go +++ b/pkg/modules/util_test.go @@ -260,9 +260,6 @@ func TestAddKubeConfigIf(t *testing.T) { name: "empty kubeConfig in workspace", ws: &v1.Workspace{ Name: "dev", - Runtimes: &v1.RuntimeConfigs{ - Kubernetes: &v1.KubernetesConfig{}, - }, }, i: &v1.Spec{ Resources: v1.Resources{ @@ -289,125 +286,10 @@ func TestAddKubeConfigIf(t *testing.T) { }, }, }, - { - name: "add kubeConfig", - ws: &v1.Workspace{ - Name: "dev", - Runtimes: &v1.RuntimeConfigs{ - Kubernetes: &v1.KubernetesConfig{ - KubeConfig: "/etc/kubeConfig.yaml", - }, - }, - }, - i: &v1.Spec{ - Resources: v1.Resources{ - { - ID: "mock-id-1", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: nil, - }, - { - ID: "mock-id-2", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "mock-extensions-key": "mock-extensions-value", - }, - }, - { - ID: "mock-id-2", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "kubeConfig": "/etc/should-use-kubeConfig.yaml", - }, - }, - { - ID: "mock-id-3", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "kubeConfig": "", - }, - }, - { - ID: "mock-id-4", - Type: "Terraform", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: nil, - }, - }, - }, - expectedSpec: &v1.Spec{ - Resources: v1.Resources{ - { - ID: "mock-id-1", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "kubeConfig": "/etc/kubeConfig.yaml", - }, - }, - { - ID: "mock-id-2", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "mock-extensions-key": "mock-extensions-value", - "kubeConfig": "/etc/kubeConfig.yaml", - }, - }, - { - ID: "mock-id-2", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "kubeConfig": "/etc/should-use-kubeConfig.yaml", - }, - }, - { - ID: "mock-id-3", - Type: "Kubernetes", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: map[string]any{ - "kubeConfig": "/etc/kubeConfig.yaml", - }, - }, - { - ID: "mock-id-4", - Type: "Terraform", - Attributes: map[string]any{ - "mock-key": "mock-value", - }, - Extensions: nil, - }, - }, - }, - }, } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - AddKubeConfigIf(tc.i, tc.ws) assert.Equal(t, *tc.expectedSpec, *tc.i) }) } diff --git a/pkg/workspace/storages/local_test.go b/pkg/workspace/storages/local_test.go index 92b4bf73..554e2768 100644 --- a/pkg/workspace/storages/local_test.go +++ b/pkg/workspace/storages/local_test.go @@ -46,18 +46,9 @@ func mockWorkspace(name string) *v1.Workspace { }, }, }, - Runtimes: &v1.RuntimeConfigs{ - Kubernetes: &v1.KubernetesConfig{ - KubeConfig: "/etc/kubeconfig.yaml", - }, - Terraform: v1.TerraformConfig{ - "aws": { - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, + Context: map[string]any{ + "kubernetes": v1.GenericConfig{ + "config": "/etc/kubeconfig.yaml", }, }, } @@ -83,14 +74,9 @@ modules: configs: default: type: aws -runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 +context: + kubernetes: + config: /etc/kubeconfig.yaml ` } diff --git a/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml b/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml index c05c5536..21e255ac 100755 --- a/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml @@ -18,11 +18,6 @@ modules: configs: default: type: aws -runtimes: +context: kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 + config: /etc/kubeconfig.yaml diff --git a/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml b/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml index e0e63015..3a5f972a 100644 --- a/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml @@ -16,11 +16,6 @@ modules: configs: default: type: aws -runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 +context: + kubernetes: + config: /etc/kubeconfig.yaml \ No newline at end of file diff --git a/pkg/workspace/storages/testdata/workspaces/dev.yaml b/pkg/workspace/storages/testdata/workspaces/dev.yaml index e0e63015..587b56f0 100644 --- a/pkg/workspace/storages/testdata/workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/workspaces/dev.yaml @@ -16,11 +16,6 @@ modules: configs: default: type: aws -runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 +context: + kubernetes: + config: /etc/kubeconfig.yaml diff --git a/pkg/workspace/storages/testdata/workspaces/prod.yaml b/pkg/workspace/storages/testdata/workspaces/prod.yaml index e0e63015..3a5f972a 100644 --- a/pkg/workspace/storages/testdata/workspaces/prod.yaml +++ b/pkg/workspace/storages/testdata/workspaces/prod.yaml @@ -16,11 +16,6 @@ modules: configs: default: type: aws -runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 +context: + kubernetes: + config: /etc/kubeconfig.yaml \ No newline at end of file diff --git a/pkg/workspace/util.go b/pkg/workspace/util.go index f189ad85..8e3e80b4 100644 --- a/pkg/workspace/util.go +++ b/pkg/workspace/util.go @@ -82,40 +82,6 @@ func getProjectModuleConfig(config *v1.ModuleConfig, projectName string) (v1.Gen return projectCfg, nil } -// GetKubernetesConfig returns kubernetes config from runtime config, should be called after -// ValidateRuntimeConfigs. -// If got empty kubernetes config, return nil. -func GetKubernetesConfig(configs *v1.RuntimeConfigs) *v1.KubernetesConfig { - if configs == nil { - return nil - } - return configs.Kubernetes -} - -// GetTerraformConfig returns terraform config from runtime config, should be called after -// ValidateRuntimeConfigs. -// If got empty terraform config, return nil. -func GetTerraformConfig(configs *v1.RuntimeConfigs) v1.TerraformConfig { - if configs == nil { - return nil - } - return configs.Terraform -} - -// GetProviderConfig returns the specified terraform provider config from runtime config, should be called -// after ValidateRuntimeConfigs. -// If got empty terraform config, return nil config and nil error. -func GetProviderConfig(configs *v1.RuntimeConfigs, providerName string) (*v1.ProviderConfig, error) { - if providerName == "" { - return nil, ErrEmptyTerraformProviderName - } - config := GetTerraformConfig(configs) - if config == nil { - return nil, nil - } - return config[providerName], nil -} - // GetInt32PointerFromGenericConfig returns the value of the key in config which should be of type int. // If exist but not int, return error. If not exist, return nil. func GetInt32PointerFromGenericConfig(config v1.GenericConfig, key string) (*int32, error) { diff --git a/pkg/workspace/util_test.go b/pkg/workspace/util_test.go index a88d39fe..7bf15dd7 100644 --- a/pkg/workspace/util_test.go +++ b/pkg/workspace/util_test.go @@ -106,90 +106,6 @@ func Test_GetProjectModuleConfig(t *testing.T) { } } -func Test_GetKubernetesConfig(t *testing.T) { - testcases := []struct { - name string - runtimeConfigs *v1.RuntimeConfigs - expectedKubernetesConfig *v1.KubernetesConfig - }{ - { - name: "successfully get kubernetes config", - runtimeConfigs: mockValidRuntimeConfigs(), - expectedKubernetesConfig: mockValidKubernetesConfig(), - }, - { - name: "get nil kubernetes config", - runtimeConfigs: nil, - expectedKubernetesConfig: nil, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - cfg := GetKubernetesConfig(tc.runtimeConfigs) - assert.Equal(t, tc.expectedKubernetesConfig, cfg) - }) - } -} - -func Test_GetTerraformConfig(t *testing.T) { - testcases := []struct { - name string - runtimeConfigs *v1.RuntimeConfigs - expectedTerraformConfig v1.TerraformConfig - }{ - { - name: "successfully get terraform config", - runtimeConfigs: mockValidRuntimeConfigs(), - expectedTerraformConfig: mockValidTerraformConfig(), - }, - { - name: "get nil terraform config", - runtimeConfigs: nil, - expectedTerraformConfig: nil, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - cfg := GetTerraformConfig(tc.runtimeConfigs) - assert.Equal(t, tc.expectedTerraformConfig, cfg) - }) - } -} - -func Test_GetTerraformProviderConfig(t *testing.T) { - testcases := []struct { - name string - providerName string - runtimeConfigs *v1.RuntimeConfigs - success bool - expectedProviderConfig *v1.ProviderConfig - }{ - { - name: "successfully get terraform provider config", - providerName: "aws", - runtimeConfigs: mockValidRuntimeConfigs(), - success: true, - expectedProviderConfig: &v1.ProviderConfig{ - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - cfg, err := GetProviderConfig(tc.runtimeConfigs, tc.providerName) - assert.Equal(t, tc.success, err == nil) - assert.Equal(t, tc.expectedProviderConfig, cfg) - }) - } -} - func Test_GetIntFieldFromGenericConfig(t *testing.T) { r2 := int32(2) diff --git a/pkg/workspace/validation.go b/pkg/workspace/validation.go index 182ae22d..ac7b4ac3 100644 --- a/pkg/workspace/validation.go +++ b/pkg/workspace/validation.go @@ -23,23 +23,14 @@ var ( ErrEmptyModuleConfigProjectName = errors.New("empty project name at projectSelector in module config patcher block") ErrRepeatedModuleConfigSelectedProjects = errors.New("project should not repeat in one patcher block's projectSelector") ErrMultipleModuleConfigSelectedProjects = errors.New("a project cannot assign in more than one patcher block's projectSelector") - - ErrEmptyKubeConfig = errors.New("empty kubeconfig") - ErrEmptyTerraformProviderName = errors.New("empty terraform provider name") - ErrEmptyTerraformProviderConfig = errors.New("empty terraform provider config") - ErrEmptyTerraformProviderSource = errors.New("empty provider source") - ErrEmptyTerraformProviderVersion = errors.New("empty provider version") - ErrEmptyTerraformProviderConfigKey = errors.New("empty provider config key") - ErrEmptyTerraformProviderConfigValue = errors.New("empty provider config value") - - ErrMissingProvider = errors.New("invalid secret store spec, missing provider config") - ErrMultiSecretStoreProviders = errors.New("may not specify more than 1 secret store provider") - ErrEmptyAWSRegion = errors.New("region must be provided when using AWS Secrets Manager") - ErrEmptyVaultServer = errors.New("server address must be provided when using Hashicorp Vault") - ErrEmptyVaultURL = errors.New("vault url must be provided when using Azure KeyVault") - ErrEmptyTenantID = errors.New("azure tenant id must be provided when using Azure KeyVault") - ErrEmptyAlicloudRegion = errors.New("region must be provided when using Alicloud Secrets Manager") - ErrMissingProviderType = errors.New("must specify a provider type") + ErrMissingProvider = errors.New("invalid secret store spec, missing provider config") + ErrMultiSecretStoreProviders = errors.New("may not specify more than 1 secret store provider") + ErrEmptyAWSRegion = errors.New("region must be provided when using AWS Secrets Manager") + ErrEmptyVaultServer = errors.New("server address must be provided when using Hashicorp Vault") + ErrEmptyVaultURL = errors.New("vault url must be provided when using Azure KeyVault") + ErrEmptyTenantID = errors.New("azure tenant id must be provided when using Azure KeyVault") + ErrEmptyAlicloudRegion = errors.New("region must be provided when using Alicloud Secrets Manager") + ErrMissingProviderType = errors.New("must specify a provider type") ) // ValidateWorkspace is used to validate the workspace get or set in the storage. @@ -52,11 +43,6 @@ func ValidateWorkspace(ws *v1.Workspace) error { return err } } - if ws.Runtimes != nil { - if err := ValidateRuntimeConfigs(ws.Runtimes); err != nil { - return err - } - } if ws.SecretStore != nil { if allErrs := ValidateSecretStoreConfig(ws.SecretStore); allErrs != nil { return utilerrors.NewAggregate(allErrs) @@ -151,64 +137,6 @@ func ValidateModulePatcherConfigs(config v1.ModulePatcherConfigs) error { return nil } -// ValidateRuntimeConfigs is used to validate the runtimeConfigs is valid or not. -func ValidateRuntimeConfigs(configs *v1.RuntimeConfigs) error { - if configs.Kubernetes != nil { - if err := ValidateKubernetesConfig(configs.Kubernetes); err != nil { - return err - } - } - if configs.Terraform != nil { - if err := ValidateTerraformConfig(configs.Terraform); err != nil { - return err - } - } - return nil -} - -// ValidateKubernetesConfig is used to validate the kubernetesConfig is valid or not. -func ValidateKubernetesConfig(config *v1.KubernetesConfig) error { - if config.KubeConfig == "" { - return ErrEmptyKubeConfig - } - return nil -} - -// ValidateTerraformConfig is used to validate the terraformConfig is valid or not. -func ValidateTerraformConfig(config v1.TerraformConfig) error { - for name, cfg := range config { - if name == "" { - return ErrEmptyTerraformProviderName - } - if cfg == nil { - return fmt.Errorf("%w of provider %s", ErrEmptyTerraformProviderConfig, name) - } - if err := ValidateProviderConfig(cfg); err != nil { - return fmt.Errorf("invalid terraform provider %s: %w", name, err) - } - } - return nil -} - -// ValidateProviderConfig is used to validate the providerConfig is valid or not. -func ValidateProviderConfig(config *v1.ProviderConfig) error { - if config.Source == "" { - return ErrEmptyTerraformProviderSource - } - if config.Version == "" { - return ErrEmptyTerraformProviderVersion - } - for k, v := range config.GenericConfig { - if k == "" { - return ErrEmptyTerraformProviderConfigKey - } - if v == nil { - return fmt.Errorf("%w of field %s", ErrEmptyTerraformProviderConfigValue, k) - } - } - return nil -} - // ValidateSecretStoreConfig tests that the specified SecretStoreSpec has valid data. func ValidateSecretStoreConfig(spec *v1.SecretStoreSpec) []error { if spec.Provider == nil { diff --git a/pkg/workspace/validation_test.go b/pkg/workspace/validation_test.go index 16b7d0b5..dd3dfcd4 100644 --- a/pkg/workspace/validation_test.go +++ b/pkg/workspace/validation_test.go @@ -10,9 +10,13 @@ import ( func mockValidWorkspace(name string) *v1.Workspace { return &v1.Workspace{ - Name: name, - Modules: mockValidModuleConfigs(), - Runtimes: mockValidRuntimeConfigs(), + Name: name, + Modules: mockValidModuleConfigs(), + Context: map[string]any{ + "Kubernetes": map[string]string{ + "Config": "/etc/kubeconfig.yaml", + }, + }, } } @@ -183,31 +187,6 @@ func mockInvalidModuleConfigs() map[string]v1.ModuleConfig { } } -func mockValidRuntimeConfigs() *v1.RuntimeConfigs { - return &v1.RuntimeConfigs{ - Kubernetes: mockValidKubernetesConfig(), - Terraform: mockValidTerraformConfig(), - } -} - -func mockValidKubernetesConfig() *v1.KubernetesConfig { - return &v1.KubernetesConfig{ - KubeConfig: "/etc/kubeconfig.yaml", - } -} - -func mockValidTerraformConfig() v1.TerraformConfig { - return v1.TerraformConfig{ - "aws": { - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, - } -} - func TestValidateWorkspace(t *testing.T) { testcases := []struct { name string @@ -301,146 +280,6 @@ func TestValidateModuleConfig(t *testing.T) { } } -func TestValidateRuntimeConfigs(t *testing.T) { - testcases := []struct { - name string - success bool - runtimeConfigs *v1.RuntimeConfigs - }{ - { - name: "valid runtime configs", - success: true, - runtimeConfigs: mockValidRuntimeConfigs(), - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := ValidateRuntimeConfigs(tc.runtimeConfigs) - assert.Equal(t, tc.success, err == nil) - }) - } -} - -func TestValidateKubernetesConfig(t *testing.T) { - testcases := []struct { - name string - success bool - kubernetesConfig *v1.KubernetesConfig - }{ - { - name: "valid kubernetes config", - success: true, - kubernetesConfig: mockValidKubernetesConfig(), - }, - { - name: "invalid kubernetes config empty kubeconfig", - success: false, - kubernetesConfig: &v1.KubernetesConfig{}, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := ValidateKubernetesConfig(tc.kubernetesConfig) - assert.Equal(t, tc.success, err == nil) - }) - } -} - -func TestValidateTerraformConfig(t *testing.T) { - testcases := []struct { - name string - success bool - terraformConfig v1.TerraformConfig - }{ - { - name: "valid terraform config", - success: true, - terraformConfig: mockValidTerraformConfig(), - }, - { - name: "invalid terraform config empty provider name", - success: false, - terraformConfig: v1.TerraformConfig{ - "": { - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, - }, - }, - { - name: "invalid terraform config empty provider config", - success: false, - terraformConfig: v1.TerraformConfig{ - "aws": nil, - }, - }, - { - name: "invalid terraform config empty provider source", - success: false, - terraformConfig: v1.TerraformConfig{ - "aws": { - Source: "", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, - }, - }, - { - name: "invalid terraform config empty provider version", - success: false, - terraformConfig: v1.TerraformConfig{ - "aws": { - Source: "hashicorp/aws", - Version: "", - GenericConfig: v1.GenericConfig{ - "region": "us-east-1", - }, - }, - }, - }, - { - name: "invalid terraform config empty provider config key", - success: false, - terraformConfig: v1.TerraformConfig{ - "aws": { - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "": "us-east-1", - }, - }, - }, - }, - { - name: "invalid terraform config empty provider config value", - success: false, - terraformConfig: v1.TerraformConfig{ - "aws": { - Source: "hashicorp/aws", - Version: "1.0.4", - GenericConfig: v1.GenericConfig{ - "region": nil, - }, - }, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := ValidateTerraformConfig(tc.terraformConfig) - assert.Equal(t, tc.success, err == nil) - }) - } -} - func TestValidateAWSSecretStore(t *testing.T) { type args struct { ss *v1.AWSProvider