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: Introduce mandatory module Label in module create #116

Merged
merged 7 commits into from
Dec 3, 2024
Merged
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: 4 additions & 0 deletions internal/service/templategenerator/templategenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func generateLabels(config *contentprovider.ModuleConfig) map[string]string {
labels[shared.InternalLabel] = shared.EnableLabelValue
}

if config.Mandatory {
labels[shared.IsMandatoryModule] = shared.EnableLabelValue
}

return labels
}

Expand Down
72 changes: 72 additions & 0 deletions internal/service/templategenerator/templategenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,78 @@ func TestGenerateModuleTemplateWithManagerWithoutNamespace_Success(t *testing.T)
require.Equal(t, 1, strings.Count(mockFS.writtenTemplate, "namespace"))
}

func TestGenerateModuleTemplateWithMandatoryTrue_Success(t *testing.T) {
mockFS := &mockFileSystem{}
svc, _ := templategenerator.NewService(mockFS)

moduleConfig := &contentprovider.ModuleConfig{
Namespace: "default",
Version: "1.0.0",
Labels: map[string]string{"key": "value"},
Annotations: map[string]string{"annotation": "value"},
Mandatory: true,
Manifest: "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml",
Resources: contentprovider.Resources{"someResource": "https://some.other/location/template-operator.yaml"},
}
descriptor := testutils.CreateComponentDescriptor("example.com/component", "1.0.0")
data := []byte("test-data")

err := svc.GenerateModuleTemplate(moduleConfig, descriptor, data, true, "output.yaml")

require.NoError(t, err)
require.Equal(t, "output.yaml", mockFS.path)
require.Contains(t, mockFS.writtenTemplate, "version: 1.0.0")
require.Contains(t, mockFS.writtenTemplate, "moduleName: component")
require.Contains(t, mockFS.writtenTemplate, "component-1.0.0")
require.Contains(t, mockFS.writtenTemplate, "default")
require.Contains(t, mockFS.writtenTemplate, "test-data")
require.Contains(t, mockFS.writtenTemplate, "example.com/component")
require.Contains(t, mockFS.writtenTemplate, "someResource")
require.Contains(t, mockFS.writtenTemplate, "https://some.other/location/template-operator.yaml")
require.Contains(t, mockFS.writtenTemplate, "rawManifest")
require.Contains(t, mockFS.writtenTemplate,
"https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml")
require.Contains(t, mockFS.writtenTemplate, "mandatory: true")
require.Contains(t, mockFS.writtenTemplate,
"\"operator.kyma-project.io/mandatory-module\": \"true\"")
medmes marked this conversation as resolved.
Show resolved Hide resolved
}

func TestGenerateModuleTemplateWithMandatoryFalse_Success(t *testing.T) {
mockFS := &mockFileSystem{}
svc, _ := templategenerator.NewService(mockFS)

moduleConfig := &contentprovider.ModuleConfig{
Namespace: "default",
Version: "1.0.0",
Labels: map[string]string{"key": "value"},
Annotations: map[string]string{"annotation": "value"},
Mandatory: false,
Manifest: "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml",
Resources: contentprovider.Resources{"someResource": "https://some.other/location/template-operator.yaml"},
}
descriptor := testutils.CreateComponentDescriptor("example.com/component", "1.0.0")
data := []byte("test-data")

err := svc.GenerateModuleTemplate(moduleConfig, descriptor, data, true, "output.yaml")

require.NoError(t, err)
require.Equal(t, "output.yaml", mockFS.path)
require.Contains(t, mockFS.writtenTemplate, "version: 1.0.0")
require.Contains(t, mockFS.writtenTemplate, "moduleName: component")
require.Contains(t, mockFS.writtenTemplate, "component-1.0.0")
require.Contains(t, mockFS.writtenTemplate, "default")
require.Contains(t, mockFS.writtenTemplate, "test-data")
require.Contains(t, mockFS.writtenTemplate, "example.com/component")
require.Contains(t, mockFS.writtenTemplate, "someResource")
require.Contains(t, mockFS.writtenTemplate, "https://some.other/location/template-operator.yaml")
require.Contains(t, mockFS.writtenTemplate, "rawManifest")
require.Contains(t, mockFS.writtenTemplate,
"https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml")
require.Contains(t, mockFS.writtenTemplate, "mandatory: false")
require.NotContains(t, mockFS.writtenTemplate,
"\"operator.kyma-project.io/mandatory-module\"")
}

type mockFileSystem struct {
path, writtenTemplate string
}
Expand Down
13 changes: 10 additions & 3 deletions tests/e2e/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,11 @@ var _ = Describe("Test 'create' command", Ordered, func() {
Expect(github.Type).To(Equal(githubAccessSpec.Type))
Expect(githubAccessSpec.RepoURL).To(Equal("https://github.com/kyma-project/template-operator"))

By("And spec.mandatory should be false")
By("And module template should not marked as mandatory")
Expect(template.Spec.Mandatory).To(BeFalse())
val, ok := template.Labels[shared.IsMandatoryModule]
Expect(val).To(BeEmpty())
Expect(ok).To(BeFalse())

By("And spec.associatedResources should be empty")
Expect(template.Spec.AssociatedResources).To(BeEmpty())
Expand Down Expand Up @@ -540,8 +543,11 @@ var _ = Describe("Test 'create' command", Ordered, func() {
Expect(github.Type).To(Equal(githubAccessSpec.Type))
Expect(githubAccessSpec.RepoURL).To(Equal("https://github.com/kyma-project/template-operator"))

By("And spec.mandatory should be false")
By("And module template should not marked as mandatory")
Expect(template.Spec.Mandatory).To(BeFalse())
val, ok := template.Labels[shared.IsMandatoryModule]
Expect(val).To(BeEmpty())
Expect(ok).To(BeFalse())

By("And security scan labels should be correct")
secScanLabels := flatten(descriptor.Sources[0].Labels)
Expand Down Expand Up @@ -599,8 +605,9 @@ var _ = Describe("Test 'create' command", Ordered, func() {
Expect(template.Spec.ModuleName).To(Equal("template-operator"))
Expect(template.Spec.Version).To(Equal("1.0.4"))

By("And spec.mandatory should be true")
By("And module template should be marked as mandatory")
Expect(template.Spec.Mandatory).To(BeTrue())
Expect(template.Labels[shared.IsMandatoryModule]).To(Equal("true"))
})
})

Expand Down
Loading