From d165e31989f8d787a01acf86cbc477ab7628a671 Mon Sep 17 00:00:00 2001 From: Amritanshu Sikdar Date: Wed, 20 Mar 2024 08:03:52 +0100 Subject: [PATCH 1/4] add new EnableModuleConfig flag --- cmd/kyma/alpha/create/module/moduleconfig.go | 29 ++++++++++---------- pkg/module/template.go | 21 +++++++------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cmd/kyma/alpha/create/module/moduleconfig.go b/cmd/kyma/alpha/create/module/moduleconfig.go index e0a774db2..9117ffd29 100644 --- a/cmd/kyma/alpha/create/module/moduleconfig.go +++ b/cmd/kyma/alpha/create/module/moduleconfig.go @@ -15,20 +15,21 @@ import ( ) type Config struct { - Name string `yaml:"name" comment:"required, the name of the Module"` - Version string `yaml:"version" comment:"required, the version of the Module"` - Channel string `yaml:"channel" comment:"required, channel that should be used in the ModuleTemplate"` - ManifestPath string `yaml:"manifest" comment:"required, relative path or remote URL to the manifests"` - Mandatory bool `yaml:"mandatory" comment:"optional, default=false, indicates whether the module is mandatory to be installed on all clusters"` - DefaultCRPath string `yaml:"defaultCR" comment:"optional, relative path or remote URL to a YAML file containing the default CR for the module"` - ResourceName string `yaml:"resourceName" comment:"optional, default={NAME}-{CHANNEL}, the name for the ModuleTemplate that will be created"` - Namespace string `yaml:"namespace" comment:"optional, default=kcp-system, the namespace where the ModuleTemplate will be deployed"` - Security string `yaml:"security" comment:"optional, name of the security scanners config file"` - Internal bool `yaml:"internal" comment:"optional, default=false, determines whether the ModuleTemplate should have the internal flag or not"` - Beta bool `yaml:"beta" comment:"optional, default=false, determines whether the ModuleTemplate should have the beta flag or not"` - Labels map[string]string `yaml:"labels" comment:"optional, additional labels for the ModuleTemplate"` - Annotations map[string]string `yaml:"annotations" comment:"optional, additional annotations for the ModuleTemplate"` - CustomStateChecks []v1beta2.CustomStateCheck `yaml:"customStateCheck" comment:"optional, specifies custom state check for module"` + Name string `yaml:"name" comment:"required, the name of the Module"` + Version string `yaml:"version" comment:"required, the version of the Module"` + Channel string `yaml:"channel" comment:"required, channel that should be used in the ModuleTemplate"` + ManifestPath string `yaml:"manifest" comment:"required, relative path or remote URL to the manifests"` + Mandatory bool `yaml:"mandatory" comment:"optional, default=false, indicates whether the module is mandatory to be installed on all clusters"` + DefaultCRPath string `yaml:"defaultCR" comment:"optional, relative path or remote URL to a YAML file containing the default CR for the module"` + ResourceName string `yaml:"resourceName" comment:"optional, default={NAME}-{CHANNEL}, the name for the ModuleTemplate that will be created"` + Namespace string `yaml:"namespace" comment:"optional, default=kcp-system, the namespace where the ModuleTemplate will be deployed"` + Security string `yaml:"security" comment:"optional, name of the security scanners config file"` + Internal bool `yaml:"internal" comment:"optional, default=false, determines whether the ModuleTemplate should have the internal flag or not"` + Beta bool `yaml:"beta" comment:"optional, default=false, determines whether the ModuleTemplate should have the beta flag or not"` + Labels map[string]string `yaml:"labels" comment:"optional, additional labels for the ModuleTemplate"` + Annotations map[string]string `yaml:"annotations" comment:"optional, additional annotations for the ModuleTemplate"` + CustomStateChecks []v1beta2.CustomStateCheck `yaml:"customStateCheck" comment:"optional, specifies custom state check for module"` + EnableModuleConfig bool `yaml:"enableModuleConfig" comment:"optional, default=false, to let the LifecycleManager know which module CR should be synced back to the KCP"` } const ( diff --git a/pkg/module/template.go b/pkg/module/template.go index fd724e4a3..1dd8426d7 100644 --- a/pkg/module/template.go +++ b/pkg/module/template.go @@ -55,19 +55,20 @@ spec: ) type moduleTemplateData struct { - ResourceName string // K8s resource name of the generated ModuleTemplate - Namespace string - Descriptor compdesc.ComponentDescriptorVersion // descriptor info for the template - Channel string - Data string // contents for the spec.data section of the template taken from the defaults.yaml file in the mod folder - Labels map[string]string - Annotations map[string]string - CustomStateChecks []v1beta2.CustomStateCheck - Mandatory bool + ResourceName string // K8s resource name of the generated ModuleTemplate + Namespace string + Descriptor compdesc.ComponentDescriptorVersion // descriptor info for the template + Channel string + Data string // contents for the spec.data section of the template taken from the defaults.yaml file in the mod folder + Labels map[string]string + Annotations map[string]string + CustomStateChecks []v1beta2.CustomStateCheck + Mandatory bool + EnableModuleConfig bool } func Template(remote ocm.ComponentVersionAccess, moduleTemplateName, namespace, channel string, data []byte, - labels, annotations map[string]string, customsStateChecks []v1beta2.CustomStateCheck, mandatory bool) ([]byte, + labels, annotations map[string]string, customsStateChecks []v1beta2.CustomStateCheck, mandatory bool, enableModuleConfig bool) ([]byte, error) { descriptor := remote.GetDescriptor() ref, err := oci.ParseRef(descriptor.Name) From 206745a2034a6f8b053788b73616936957d8165b Mon Sep 17 00:00:00 2001 From: Amritanshu Sikdar Date: Wed, 20 Mar 2024 12:13:40 +0100 Subject: [PATCH 2/4] adapt module.go Run() conforming to the new flag --- cmd/kyma/alpha/create/module/module.go | 4 +++- pkg/module/template.go | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/kyma/alpha/create/module/module.go b/cmd/kyma/alpha/create/module/module.go index d874e4cdd..0993cc8ee 100644 --- a/cmd/kyma/alpha/create/module/module.go +++ b/cmd/kyma/alpha/create/module/module.go @@ -423,11 +423,13 @@ func (cmd *command) Run(cobraCmd *cobra.Command) error { cmd.NewStep("Generating module template...") var resourceName = "" mandatoryModule := false + enableModuleConfig := false var channel = cmd.opts.Channel if modCnf != nil { resourceName = modCnf.ResourceName channel = modCnf.Channel mandatoryModule = modCnf.Mandatory + enableModuleConfig = modCnf.EnableModuleConfig } var namespace = cmd.opts.Namespace @@ -440,7 +442,7 @@ func (cmd *command) Run(cobraCmd *cobra.Command) error { annotations := cmd.getModuleTemplateAnnotations(modCnf, crd) template, err := module.Template(componentVersionAccess, resourceName, namespace, - channel, modDef.DefaultCR, labels, annotations, modDef.CustomStateChecks, mandatoryModule) + channel, modDef.DefaultCR, labels, annotations, modDef.CustomStateChecks, mandatoryModule, enableModuleConfig) if err != nil { cmd.CurrentStep.Failure() return err diff --git a/pkg/module/template.go b/pkg/module/template.go index 1dd8426d7..7d4edff96 100644 --- a/pkg/module/template.go +++ b/pkg/module/template.go @@ -88,15 +88,16 @@ func Template(remote ocm.ComponentVersionAccess, moduleTemplateName, namespace, resourceName = shortName + "-" + channel } td := moduleTemplateData{ - ResourceName: resourceName, - Namespace: namespace, - Descriptor: cva, - Channel: channel, - Data: string(data), - Labels: labels, - Annotations: annotations, - CustomStateChecks: customsStateChecks, - Mandatory: mandatory, + ResourceName: resourceName, + Namespace: namespace, + Descriptor: cva, + Channel: channel, + Data: string(data), + Labels: labels, + Annotations: annotations, + CustomStateChecks: customsStateChecks, + Mandatory: mandatory, + EnableModuleConfig: enableModuleConfig, } t, err := template.New("modTemplate").Funcs(template.FuncMap{ From 1f361985ad8b2293faeacaad870a5be5bcb279b7 Mon Sep 17 00:00:00 2001 From: Amritanshu Sikdar Date: Wed, 20 Mar 2024 14:31:56 +0100 Subject: [PATCH 3/4] update tests --- pkg/module/template_test.go | 87 ++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/pkg/module/template_test.go b/pkg/module/template_test.go index 7413ac7ba..bb15fdde5 100644 --- a/pkg/module/template_test.go +++ b/pkg/module/template_test.go @@ -42,6 +42,7 @@ func TestTemplate(t *testing.T) { annotations map[string]string checks []v1beta2.CustomStateCheck mandatory bool + enableModuleConfig bool } tests := []struct { name string @@ -52,12 +53,13 @@ func TestTemplate(t *testing.T) { { name: "ModuleTemplate with default values", args: args{ - remote: accessVersion, - channel: "regular", - labels: map[string]string{}, - annotations: map[string]string{}, - checks: noCustomStateCheck, - mandatory: false, + remote: accessVersion, + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{}, + checks: noCustomStateCheck, + mandatory: false, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "", map[string]string{ @@ -69,13 +71,14 @@ func TestTemplate(t *testing.T) { { name: "ModuleTemplate with custom namespace", args: args{ - remote: accessVersion, - namespace: "kyma-system", - channel: "regular", - labels: map[string]string{}, - annotations: map[string]string{}, - checks: noCustomStateCheck, - mandatory: false, + remote: accessVersion, + namespace: "kyma-system", + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{}, + checks: noCustomStateCheck, + mandatory: false, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "kyma-system", map[string]string{ @@ -87,13 +90,14 @@ func TestTemplate(t *testing.T) { { name: "ModuleTemplate with extra labels", args: args{ - remote: accessVersion, - namespace: "kyma-system", - channel: "regular", - labels: map[string]string{"is-custom-label": "true"}, - annotations: map[string]string{}, - checks: noCustomStateCheck, - mandatory: false, + remote: accessVersion, + namespace: "kyma-system", + channel: "regular", + labels: map[string]string{"is-custom-label": "true"}, + annotations: map[string]string{}, + checks: noCustomStateCheck, + mandatory: false, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "kyma-system", map[string]string{ @@ -105,13 +109,14 @@ func TestTemplate(t *testing.T) { { name: "ModuleTemplate with extra annotations", args: args{ - remote: accessVersion, - namespace: "kyma-system", - channel: "regular", - labels: map[string]string{}, - annotations: map[string]string{"is-custom-annotation": "true"}, - checks: noCustomStateCheck, - mandatory: false, + remote: accessVersion, + namespace: "kyma-system", + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{"is-custom-annotation": "true"}, + checks: noCustomStateCheck, + mandatory: false, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "kyma-system", map[string]string{ @@ -123,11 +128,12 @@ func TestTemplate(t *testing.T) { { name: "ModuleTemplate with Custom State Check", args: args{ - remote: accessVersion, - channel: "regular", - labels: map[string]string{}, - annotations: map[string]string{}, - checks: defaultCustomStateCheck, + remote: accessVersion, + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{}, + checks: defaultCustomStateCheck, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "", map[string]string{shared.ModuleName: "template-operator"}, map[string]string{}, @@ -137,13 +143,14 @@ func TestTemplate(t *testing.T) { { name: "Mandatory ModuleTemplate", args: args{ - remote: accessVersion, - namespace: "kyma-system", - channel: "regular", - labels: map[string]string{}, - annotations: map[string]string{}, - checks: noCustomStateCheck, - mandatory: true, + remote: accessVersion, + namespace: "kyma-system", + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{}, + checks: noCustomStateCheck, + mandatory: true, + enableModuleConfig: false, }, want: getExpectedModuleTemplate(t, "kyma-system", map[string]string{shared.ModuleName: "template-operator"}, map[string]string{}, @@ -154,7 +161,7 @@ func TestTemplate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := Template(tt.args.remote, tt.args.moduleTemplateName, tt.args.namespace, tt.args.channel, - tt.args.data, tt.args.labels, tt.args.annotations, tt.args.checks, tt.args.mandatory) + tt.args.data, tt.args.labels, tt.args.annotations, tt.args.checks, tt.args.mandatory, tt.args.enableModuleConfig) if (err != nil) != tt.wantErr { t.Errorf("Template() error = %v, wantErr %v", err, tt.wantErr) return From 7e5a6247a7ef4f766c660db97c4f467f391f0319 Mon Sep 17 00:00:00 2001 From: Amritanshu Sikdar Date: Fri, 22 Mar 2024 11:42:03 +0100 Subject: [PATCH 4/4] adapt tests --- pkg/module/template_test.go | 50 +++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/pkg/module/template_test.go b/pkg/module/template_test.go index bb15fdde5..3f65608c9 100644 --- a/pkg/module/template_test.go +++ b/pkg/module/template_test.go @@ -65,7 +65,7 @@ func TestTemplate(t *testing.T) { map[string]string{ shared.ModuleName: "template-operator", }, map[string]string{}, - noCustomStateCheck, false), + noCustomStateCheck, false, false), wantErr: false, }, { @@ -84,7 +84,7 @@ func TestTemplate(t *testing.T) { map[string]string{ shared.ModuleName: "template-operator", }, map[string]string{}, - noCustomStateCheck, false), + noCustomStateCheck, false, false), wantErr: false, }, { @@ -103,7 +103,7 @@ func TestTemplate(t *testing.T) { map[string]string{ shared.ModuleName: "template-operator", "is-custom-label": "true", }, - map[string]string{}, noCustomStateCheck, false), + map[string]string{}, noCustomStateCheck, false, false), wantErr: false, }, { @@ -122,7 +122,7 @@ func TestTemplate(t *testing.T) { map[string]string{ shared.ModuleName: "template-operator", }, - map[string]string{"is-custom-annotation": "true"}, []v1beta2.CustomStateCheck{}, false), + map[string]string{"is-custom-annotation": "true"}, []v1beta2.CustomStateCheck{}, false, false), wantErr: false, }, { @@ -137,7 +137,7 @@ func TestTemplate(t *testing.T) { }, want: getExpectedModuleTemplate(t, "", map[string]string{shared.ModuleName: "template-operator"}, map[string]string{}, - defaultCustomStateCheck, false), + defaultCustomStateCheck, false, false), wantErr: false, }, { @@ -154,7 +154,24 @@ func TestTemplate(t *testing.T) { }, want: getExpectedModuleTemplate(t, "kyma-system", map[string]string{shared.ModuleName: "template-operator"}, map[string]string{}, - []v1beta2.CustomStateCheck{}, true), + []v1beta2.CustomStateCheck{}, true, false), + wantErr: false, + }, + { + name: "ModuleTemplate Enabling ModuleConfig Flag", + args: args{ + remote: accessVersion, + namespace: "kyma-system", + channel: "regular", + labels: map[string]string{}, + annotations: map[string]string{}, + checks: noCustomStateCheck, + mandatory: false, + enableModuleConfig: true, + }, + want: getExpectedModuleTemplate(t, "kyma-system", + map[string]string{shared.ModuleName: "template-operator"}, map[string]string{}, + []v1beta2.CustomStateCheck{}, false, true), wantErr: false, }, } @@ -191,7 +208,7 @@ func createOcmComponentVersionAccess(t *testing.T) ocm.ComponentVersionAccess { func getExpectedModuleTemplate(t *testing.T, namespace string, labels map[string]string, - annotations map[string]string, checks []v1beta2.CustomStateCheck, mandatory bool) []byte { + annotations map[string]string, checks []v1beta2.CustomStateCheck, mandatory bool, enableModuleConfig bool) []byte { cva, err := compdesc.Convert(accessVersion.GetDescriptor()) assert.Equal(t, nil, err) temp, err := template.New("modTemplate").Funcs(template.FuncMap{ @@ -200,15 +217,16 @@ func getExpectedModuleTemplate(t *testing.T, }).Parse(modTemplate) assert.Equal(t, nil, err) td := moduleTemplateData{ - ResourceName: "template-operator-regular", - Namespace: namespace, - Channel: "regular", - Annotations: annotations, - Labels: labels, - Data: "", - Descriptor: cva, - CustomStateChecks: checks, - Mandatory: mandatory, + ResourceName: "template-operator-regular", + Namespace: namespace, + Channel: "regular", + Annotations: annotations, + Labels: labels, + Data: "", + Descriptor: cva, + CustomStateChecks: checks, + Mandatory: mandatory, + EnableModuleConfig: enableModuleConfig, } w := &bytes.Buffer{} err = temp.Execute(w, td)