Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add new EnableModuleConfig flag #2007

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 15 additions & 14 deletions cmd/kyma/alpha/create/module/moduleconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
amritanshusikdar marked this conversation as resolved.
Show resolved Hide resolved
}

const (
Expand Down
40 changes: 21 additions & 19 deletions pkg/module/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -87,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{
Expand Down
137 changes: 81 additions & 56 deletions pkg/module/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestTemplate(t *testing.T) {
annotations map[string]string
checks []v1beta2.CustomStateCheck
mandatory bool
enableModuleConfig bool
}
tests := []struct {
name string
Expand All @@ -52,109 +53,132 @@ 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{
shared.ModuleName: "template-operator",
}, map[string]string{},
noCustomStateCheck, false),
noCustomStateCheck, false, false),
wantErr: false,
},
{
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{
shared.ModuleName: "template-operator",
}, map[string]string{},
noCustomStateCheck, false),
noCustomStateCheck, false, false),
wantErr: false,
},
{
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{
shared.ModuleName: "template-operator", "is-custom-label": "true",
},
map[string]string{}, noCustomStateCheck, false),
map[string]string{}, noCustomStateCheck, false, false),
wantErr: false,
},
{
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{
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,
},
{
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{},
defaultCustomStateCheck, false),
defaultCustomStateCheck, false, false),
wantErr: false,
},
{
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{},
[]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,
},
}
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
Expand Down Expand Up @@ -184,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{
Expand All @@ -193,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)
Expand Down
Loading