diff --git a/pkg/apis/api.kusion.io/v1/types.go b/pkg/apis/api.kusion.io/v1/types.go index 1063909e..0577d511 100644 --- a/pkg/apis/api.kusion.io/v1/types.go +++ b/pkg/apis/api.kusion.io/v1/types.go @@ -96,6 +96,7 @@ type Workspace struct { } // ModuleConfigs is a set of multiple ModuleConfig, whose key is the module name. +// The module name format is "source@version". type ModuleConfigs map[string]*ModuleConfig // GenericConfig is a generic model to describe config which shields the difference among multiple concrete @@ -112,11 +113,9 @@ type GenericConfig map[string]any // in multiple patchers is not allowed. For a project, if not specified in the patcher block's // "projectSelector" field, the default config will be used. // -// Take the ModuleConfig of "mysql" for an example, which is shown as below: +// Take the ModuleConfig of "database" for an example, which is shown as below: // -// config := ModuleConfig { -// "path": "ghcr.io/kusionstack/mysql" -// "version": "0.1.0" +// config := ModuleConfig { // "default": { // "type": "aws", // "version": "5.7", @@ -128,17 +127,9 @@ type GenericConfig map[string]any // }, // } type ModuleConfig struct { - // Path is the path of the module. It can be a local path or a remote URL - Path string `yaml:"path" json:"path"` - // Version is the version of the module. - Version string `yaml:"version" json:"version"` - // Configs contains all levels of module configs - Configs Configs `yaml:"configs" json:"configs"` -} - -type Configs struct { // Default is default block of the module config. Default GenericConfig `yaml:"default" json:"default"` + // ModulePatcherConfigs are the patcher blocks of the module config. ModulePatcherConfigs `yaml:",inline,omitempty" json:",inline,omitempty"` } diff --git a/pkg/cmd/workspace/util/testdata/valid_ws.yaml b/pkg/cmd/workspace/util/testdata/valid_ws.yaml index fedaaa19..60a648b7 100644 --- a/pkg/cmd/workspace/util/testdata/valid_ws.yaml +++ b/pkg/cmd/workspace/util/testdata/valid_ws.yaml @@ -1,21 +1,17 @@ modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: '5.7' - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: kubernetes: kubeConfig: /etc/kubeconfig.yaml diff --git a/pkg/modules/generators/app_configurations_generator_test.go b/pkg/modules/generators/app_configurations_generator_test.go index 86ba435e..95074a74 100644 --- a/pkg/modules/generators/app_configurations_generator_test.go +++ b/pkg/modules/generators/app_configurations_generator_test.go @@ -184,37 +184,29 @@ func buildMockWorkspace(namespace string) *v1.Workspace { return &v1.Workspace{ Name: "test", Modules: v1.ModuleConfigs{ - "mysql": &v1.ModuleConfig{ - Path: "kusionstack.io/mysql", - Version: "v1.0.0", - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "bar"}, + "kusionstack/database@v0.1": { + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"foo", "bar"}, }, }, }, - "port": &v1.ModuleConfig{ - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - }, + "port": { + Default: v1.GenericConfig{ + "type": "aws", }, }, - "namespace": &v1.ModuleConfig{ - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "name": namespace, - }, + "namespace": { + Default: v1.GenericConfig{ + "name": namespace, }, }, }, diff --git a/pkg/workspace/storages/local_test.go b/pkg/workspace/storages/local_test.go index 92b4bf73..4a0bc92f 100644 --- a/pkg/workspace/storages/local_test.go +++ b/pkg/workspace/storages/local_test.go @@ -19,30 +19,24 @@ func mockWorkspace(name string) *v1.Workspace { return &v1.Workspace{ Name: name, Modules: map[string]*v1.ModuleConfig{ - "mysql": { - Path: "ghcr.io/kusionstack/mysql", - Version: "0.1.0", - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "bar"}, + "database": { + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"foo", "bar"}, }, }, }, - "network": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - }, + "port": { + Default: v1.GenericConfig{ + "type": "aws", }, }, }, @@ -66,31 +60,27 @@ func mockWorkspace(name string) *v1.Workspace { func mockWorkspaceContent() string { return ` modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: '5.7' - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 + kubernetes: + kubeConfig: /etc/kubeconfig.yaml + terraform: + aws: + source: hashicorp/aws + version: 1.0.4 + region: us-east-1 ` } diff --git a/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml b/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml index c05c5536..98ebd305 100755 --- a/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/for_delete_workspaces/dev.yaml @@ -1,23 +1,17 @@ modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: "5.7" - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - path: "" - version: "" - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: kubernetes: kubeConfig: /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..98ebd305 100644 --- a/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/for_set_current_workspaces/dev.yaml @@ -1,26 +1,22 @@ modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: '5.7' - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 + kubernetes: + kubeConfig: /etc/kubeconfig.yaml + terraform: + aws: + source: hashicorp/aws + version: 1.0.4 + region: us-east-1 diff --git a/pkg/workspace/storages/testdata/workspaces/dev.yaml b/pkg/workspace/storages/testdata/workspaces/dev.yaml index e0e63015..98ebd305 100644 --- a/pkg/workspace/storages/testdata/workspaces/dev.yaml +++ b/pkg/workspace/storages/testdata/workspaces/dev.yaml @@ -1,26 +1,22 @@ modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: '5.7' - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 + kubernetes: + kubeConfig: /etc/kubeconfig.yaml + terraform: + aws: + source: hashicorp/aws + version: 1.0.4 + region: us-east-1 diff --git a/pkg/workspace/storages/testdata/workspaces/prod.yaml b/pkg/workspace/storages/testdata/workspaces/prod.yaml index e0e63015..98ebd305 100644 --- a/pkg/workspace/storages/testdata/workspaces/prod.yaml +++ b/pkg/workspace/storages/testdata/workspaces/prod.yaml @@ -1,26 +1,22 @@ modules: - mysql: - path: ghcr.io/kusionstack/mysql - version: 0.1.0 - configs: - default: - instanceType: db.t3.micro - type: aws - version: '5.7' - smallClass: - projectSelector: - - foo - - bar - instanceType: db.t3.small - network: - configs: - default: - type: aws + database: + default: + instanceType: db.t3.micro + type: aws + version: "5.7" + smallClass: + projectSelector: + - foo + - bar + instanceType: db.t3.small + port: + default: + type: aws runtimes: - kubernetes: - kubeConfig: /etc/kubeconfig.yaml - terraform: - aws: - source: hashicorp/aws - version: 1.0.4 - region: us-east-1 + kubernetes: + kubeConfig: /etc/kubeconfig.yaml + terraform: + aws: + source: hashicorp/aws + version: 1.0.4 + region: us-east-1 diff --git a/pkg/workspace/util.go b/pkg/workspace/util.go index f189ad85..11786751 100644 --- a/pkg/workspace/util.go +++ b/pkg/workspace/util.go @@ -51,12 +51,12 @@ func GetProjectModuleConfig(config *v1.ModuleConfig, projectName string) (v1.Gen // getProjectModuleConfig gets the module config of a specified project without checking the correctness of project name. func getProjectModuleConfig(config *v1.ModuleConfig, projectName string) (v1.GenericConfig, error) { - projectCfg := config.Configs.Default + projectCfg := config.Default if len(projectCfg) == 0 { projectCfg = make(v1.GenericConfig) } - for name, cfg := range config.Configs.ModulePatcherConfigs { + for name, cfg := range config.ModulePatcherConfigs { if name == v1.DefaultBlock { continue } diff --git a/pkg/workspace/util_test.go b/pkg/workspace/util_test.go index a88d39fe..f2c41f41 100644 --- a/pkg/workspace/util_test.go +++ b/pkg/workspace/util_test.go @@ -37,12 +37,12 @@ func Test_GetProjectModuleConfigs(t *testing.T) { moduleConfigs: mockValidModuleConfigs(), success: true, expectedProjectConfigs: map[string]v1.GenericConfig{ - "mysql": { + "database": { "type": "aws", "version": "5.7", "instanceType": "db.t3.small", }, - "network": { + "port": { "type": "aws", }, }, @@ -69,7 +69,7 @@ func Test_GetProjectModuleConfig(t *testing.T) { { name: "successfully get default project module config", projectName: "baz", - moduleConfig: mockValidModuleConfigs()["mysql"], + moduleConfig: mockValidModuleConfigs()["database"], success: true, expectedProjectConfig: v1.GenericConfig{ "type": "aws", @@ -80,7 +80,7 @@ func Test_GetProjectModuleConfig(t *testing.T) { { name: "successfully get override project module config", projectName: "foo", - moduleConfig: mockValidModuleConfigs()["mysql"], + moduleConfig: mockValidModuleConfigs()["database"], success: true, expectedProjectConfig: v1.GenericConfig{ "type": "aws", @@ -91,7 +91,7 @@ func Test_GetProjectModuleConfig(t *testing.T) { { name: "failed to get config empty project name", projectName: "", - moduleConfig: mockValidModuleConfigs()["mysql"], + moduleConfig: mockValidModuleConfigs()["database"], success: false, expectedProjectConfig: nil, }, diff --git a/pkg/workspace/validation.go b/pkg/workspace/validation.go index 182ae22d..47a492e2 100644 --- a/pkg/workspace/validation.go +++ b/pkg/workspace/validation.go @@ -84,11 +84,10 @@ func ValidateModuleConfigs(configs v1.ModuleConfigs) error { // ValidateModuleConfig is used to validate the moduleConfig is valid or not. func ValidateModuleConfig(config *v1.ModuleConfig) error { - // todo validate path and version in the config when we have turned workload into a module - if err := ValidateModuleDefaultConfig(config.Configs.Default); err != nil { + if err := ValidateModuleDefaultConfig(config.Default); err != nil { return err } - if err := ValidateModulePatcherConfigs(config.Configs.ModulePatcherConfigs); err != nil { + if err := ValidateModulePatcherConfigs(config.ModulePatcherConfigs); err != nil { return err } return nil diff --git a/pkg/workspace/validation_test.go b/pkg/workspace/validation_test.go index 16b7d0b5..9cb3c1a9 100644 --- a/pkg/workspace/validation_test.go +++ b/pkg/workspace/validation_test.go @@ -18,30 +18,24 @@ func mockValidWorkspace(name string) *v1.Workspace { func mockValidModuleConfigs() map[string]*v1.ModuleConfig { return map[string]*v1.ModuleConfig{ - "mysql": { - Path: "ghcr.io/kusionstack/mysql", - Version: "0.1.0", - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "bar"}, + "database": { + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"foo", "bar"}, }, }, }, - "network": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - }, + "port": { + Default: v1.GenericConfig{ + "type": "aws", }, }, } @@ -50,133 +44,115 @@ func mockValidModuleConfigs() map[string]*v1.ModuleConfig { func mockInvalidModuleConfigs() map[string]v1.ModuleConfig { return map[string]v1.ModuleConfig{ "empty default block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{}, - }, + Default: v1.GenericConfig{}, }, "not empty projectSelector in default block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - "projectSelector": []string{"foo", "bar"}, - }, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + "projectSelector": []string{"foo", "bar"}, }, }, "empty patcher block name": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "bar"}, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"foo", "bar"}, }, }, }, "empty patcher block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": nil, - }, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": nil, }, }, "empty config in patcher block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - ProjectSelector: []string{"foo", "bar"}, - }, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + ProjectSelector: []string{"foo", "bar"}, }, }, }, "empty project selector in patcher block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, }, }, }, "empty project name": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"", "bar"}, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"", "bar"}, }, }, }, "repeated projects in one patcher block": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "foo"}, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, + ProjectSelector: []string{"foo", "foo"}, }, }, }, "repeated projects in multiple patcher blocks": { - Configs: v1.Configs{ - Default: v1.GenericConfig{ - "type": "aws", - "version": "5.7", - "instanceType": "db.t3.micro", - }, - ModulePatcherConfigs: v1.ModulePatcherConfigs{ - "smallClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.small", - }, - ProjectSelector: []string{"foo", "bar"}, + Default: v1.GenericConfig{ + "type": "aws", + "version": "5.7", + "instanceType": "db.t3.micro", + }, + ModulePatcherConfigs: v1.ModulePatcherConfigs{ + "smallClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.small", }, - "largeClass": { - GenericConfig: v1.GenericConfig{ - "instanceType": "db.t3.large", - }, - ProjectSelector: []string{"foo"}, + ProjectSelector: []string{"foo", "bar"}, + }, + "largeClass": { + GenericConfig: v1.GenericConfig{ + "instanceType": "db.t3.large", }, + ProjectSelector: []string{"foo"}, }, }, }, @@ -278,7 +254,7 @@ func TestValidateModuleConfig(t *testing.T) { { name: "valid module config", success: true, - moduleConfig: *mockValidModuleConfigs()["mysql"], + moduleConfig: *mockValidModuleConfigs()["database"], }, } for desc, cfg := range mockInvalidModuleConfigs() {