Skip to content

Commit

Permalink
refactor: update the struct of ModuleConfig (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
healthjyk committed Dec 13, 2023
1 parent 7531425 commit 3d09a64
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 147 deletions.
22 changes: 20 additions & 2 deletions pkg/apis/workspace/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Workspace struct {
}

// ModuleConfigs is a set of multiple ModuleConfig, whose key is the module name.
type ModuleConfigs map[string]ModuleConfig
type ModuleConfigs map[string]*ModuleConfig

// ModuleConfig is the config of a module, which contains a default and several patcher blocks.
//
Expand All @@ -62,7 +62,25 @@ type ModuleConfigs map[string]ModuleConfig
// "projectSelector": []string{"foo", "bar"},
// },
// }
type ModuleConfig map[string]GenericConfig
type ModuleConfig 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"`
}

// ModulePatcherConfigs is a group of ModulePatcherConfig.
type ModulePatcherConfigs map[string]*ModulePatcherConfig

// ModulePatcherConfig is a patcher block of the module config.
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 {
Expand Down
14 changes: 9 additions & 5 deletions pkg/cmd/build/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ func buildMockWorkspace() *workspace.Workspace {
Name: "test",
Modules: workspace.ModuleConfigs{
"database": {
"default": {
Default: workspace.GenericConfig{
"type": "aws",
"version": "5.7",
"instanceType": "db.t3.micro",
},
"smallClass": {
"instanceType": "db.t3.small",
"projectSelector": []any{"foo", "bar"},
ModulePatcherConfigs: workspace.ModulePatcherConfigs{
"smallClass": {
GenericConfig: workspace.GenericConfig{
"instanceType": "db.t3.small",
},
ProjectSelector: []string{"foo", "bar"},
},
},
},
"port": {
"default": {
Default: workspace.GenericConfig{
"type": "aws",
},
},
Expand Down
16 changes: 1 addition & 15 deletions pkg/cmd/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
apisworkspace "kusionstack.io/kusion/pkg/apis/workspace"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/build/builders/kcl"
"kusionstack.io/kusion/pkg/log"
Expand Down Expand Up @@ -79,7 +78,7 @@ func Intent(o *builders.Options, p *project.Project, s *stack.Stack) (*intent.In
if err != nil {
return nil, err
}
ws, err := getWorkspace(s.GetName())
ws, err := workspace.GetWorkspaceByDefaultOperator(s.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -125,19 +124,6 @@ func buildAppConfigs(o *builders.Options, stack *stack.Stack) (map[string]inputs
return appConfigs, nil
}

func getWorkspace(stackName string) (*apisworkspace.Workspace, error) {
wsOperator, err := workspace.NewDefaultOperator()
if err != nil {
return nil, fmt.Errorf("new workspace operator failed, %w", err)
}
// stack name should be same as the workspace name
ws, err := wsOperator.GetWorkspace(stackName)
if err != nil {
return nil, fmt.Errorf("get workspace %s failed, %w", stackName, err)
}
return ws, nil
}

func IntentFromFile(filePath string) (*intent.Intent, error) {
b, err := os.ReadFile(filePath)
if err != nil {
Expand Down
33 changes: 19 additions & 14 deletions pkg/cmd/build/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/apis/workspace"
workspaceapi "kusionstack.io/kusion/pkg/apis/workspace"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/build/builders/kcl"
appconfigmodel "kusionstack.io/kusion/pkg/modules/inputs"
"kusionstack.io/kusion/pkg/workspace"
)

var (
Expand Down Expand Up @@ -119,33 +120,37 @@ resources:
},
}

ws = &workspace.Workspace{
ws = &workspaceapi.Workspace{
Name: "default",
Modules: workspace.ModuleConfigs{
Modules: workspaceapi.ModuleConfigs{
"database": {
"default": {
Default: workspaceapi.GenericConfig{
"type": "aws",
"version": "5.7",
"instanceType": "db.t3.micro",
},
"smallClass": {
"instanceType": "db.t3.small",
"projectSelector": []any{"foo", "bar"},
ModulePatcherConfigs: workspaceapi.ModulePatcherConfigs{
"smallClass": {
GenericConfig: workspaceapi.GenericConfig{
"instanceType": "db.t3.small",
},
ProjectSelector: []string{"foo", "bar"},
},
},
},
"port": {
"default": {
Default: workspaceapi.GenericConfig{
"type": "aws",
},
},
},
Runtimes: &workspace.RuntimeConfigs{
Kubernetes: &workspace.KubernetesConfig{
Runtimes: &workspaceapi.RuntimeConfigs{
Kubernetes: &workspaceapi.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
},
},
Backends: &workspace.BackendConfigs{
Local: &workspace.LocalFileConfig{},
Backends: &workspaceapi.BackendConfigs{
Local: &workspaceapi.LocalFileConfig{},
},
}
)
Expand Down Expand Up @@ -226,7 +231,7 @@ func TestBuildIntent(t *testing.T) {
}, stack: &stack.Stack{},
mockers: []*mockey.MockBuilder{
mockey.Mock(kcl.Run).Return(&kcl.CompileResult{Documents: []kclgo.KCLResult{apcMap}}, nil),
mockey.Mock(getWorkspace).Return(ws, nil),
mockey.Mock(workspace.GetWorkspaceByDefaultOperator).Return(ws, nil),
},
},
want: intentModel3,
Expand Down Expand Up @@ -276,7 +281,7 @@ func TestBuildIntent(t *testing.T) {
},
mockers: []*mockey.MockBuilder{
mockey.Mock(kcl.Run).Return(&kcl.CompileResult{Documents: []kclgo.KCLResult{apcMap}}, nil),
mockey.Mock(getWorkspace).Return(ws, nil),
mockey.Mock(workspace.GetWorkspaceByDefaultOperator).Return(ws, nil),
},
},
want: intentModel3,
Expand Down
14 changes: 9 additions & 5 deletions pkg/modules/generators/app_configurations_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@ func buildMockWorkspace() *workspace.Workspace {
Name: "test",
Modules: workspace.ModuleConfigs{
"database": {
"default": {
Default: workspace.GenericConfig{
"type": "aws",
"version": "5.7",
"instanceType": "db.t3.micro",
},
"smallClass": {
"instanceType": "db.t3.small",
"projectSelector": []any{"foo", "bar"},
ModulePatcherConfigs: workspace.ModulePatcherConfigs{
"smallClass": {
GenericConfig: workspace.GenericConfig{
"instanceType": "db.t3.small",
},
ProjectSelector: []string{"foo", "bar"},
},
},
},
"port": {
"default": {
Default: workspace.GenericConfig{
"type": "aws",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/workspace/testdata/workspaces/for_delete_ws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ modules:
type: aws
version: "5.7"
smallClass:
instanceType: db.t3.small
projectSelector:
- foo
- bar
instanceType: db.t3.small
port:
default:
type: aws
Expand Down
2 changes: 1 addition & 1 deletion pkg/workspace/testdata/workspaces/for_update_ws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ modules:
type: aws
version: "5.7"
smallClass:
instanceType: db.t3.small
projectSelector:
- foo
- bar
instanceType: db.t3.small
port:
default:
type: aws
Expand Down
36 changes: 7 additions & 29 deletions pkg/workspace/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"os"

"gopkg.in/yaml.v3"

"kusionstack.io/kusion/pkg/apis/workspace"
)

Expand Down Expand Up @@ -66,8 +64,8 @@ func GetProjectModuleConfigs(configs workspace.ModuleConfigs, projectName string
// GetProjectModuleConfig returns the module config of a specified project, should be called after
// ValidateModuleConfig.
// If got empty module config, ErrEmptyProjectModuleConfig will get returned.
func GetProjectModuleConfig(config workspace.ModuleConfig, projectName string) (workspace.GenericConfig, error) {
if len(config) == 0 {
func GetProjectModuleConfig(config *workspace.ModuleConfig, projectName string) (workspace.GenericConfig, error) {
if config == nil {
return nil, ErrEmptyModuleConfig
}
if projectName == "" {
Expand All @@ -79,30 +77,26 @@ func GetProjectModuleConfig(config workspace.ModuleConfig, projectName string) (

// getProjectModuleConfig gets the module config of a specified project without checking the correctness
// of project name.
func getProjectModuleConfig(config workspace.ModuleConfig, projectName string) (workspace.GenericConfig, error) {
projectCfg := config[workspace.DefaultBlock]
func getProjectModuleConfig(config *workspace.ModuleConfig, projectName string) (workspace.GenericConfig, error) {
projectCfg := config.Default
if len(projectCfg) == 0 {
projectCfg = make(workspace.GenericConfig)
}

for name, cfg := range config {
for name, cfg := range config.ModulePatcherConfigs {
if name == workspace.DefaultBlock {
continue
}
projects, err := parseProjectsFromProjectSelector(cfg[workspace.ProjectSelectorField])
if err != nil {
return nil, fmt.Errorf("%w, patcher block: %s", err, name)
}
// check the project is assigned in the block or not.
var contain bool
for _, project := range projects {
for _, project := range cfg.ProjectSelector {
if projectName == project {
contain = true
break
}
}
if contain {
for k, v := range cfg {
for k, v := range cfg.GenericConfig {
if k == workspace.ProjectSelectorField {
continue
}
Expand All @@ -118,22 +112,6 @@ func getProjectModuleConfig(config workspace.ModuleConfig, projectName string) (
return projectCfg, nil
}

// parseProjectsFromProjectSelector parses the projects in projectSelector field to string slice.
func parseProjectsFromProjectSelector(unstructuredProjects any) ([]string, error) {
var projects []string
bytes, err := yaml.Marshal(unstructuredProjects)
if err != nil {
return nil, fmt.Errorf("%w, marshal failed: %v", ErrInvalidModuleConfigProjectSelector, err)
}
if err = yaml.Unmarshal(bytes, &projects); err != nil {
return nil, fmt.Errorf("%w, unmarshal failed: %v", ErrInvalidModuleConfigProjectSelector, err)
}
if len(projects) == 0 {
return nil, fmt.Errorf("%w, empty projects", ErrInvalidModuleConfigProjectSelector)
}
return projects, nil
}

// GetKubernetesConfig returns kubernetes config from runtime config, should be called after
// ValidateRuntimeConfigs.
// If got empty kubernetes config, ErrEmptyKubernetesConfig will get returned.
Expand Down
2 changes: 1 addition & 1 deletion pkg/workspace/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_GetProjectModuleConfig(t *testing.T) {
success bool
projectName string
projectConfig workspace.GenericConfig
moduleConfig workspace.ModuleConfig
moduleConfig *workspace.ModuleConfig
}{
{
name: "successfully get default project module config",
Expand Down
Loading

0 comments on commit 3d09a64

Please sign in to comment.