diff --git a/config/samples/component-integration-installed/operator_v1beta2_moduletemplate_kcp-module_updated.yaml b/config/samples/component-integration-installed/operator_v1beta2_moduletemplate_kcp-module_updated.yaml new file mode 100644 index 0000000000..653e7001b5 --- /dev/null +++ b/config/samples/component-integration-installed/operator_v1beta2_moduletemplate_kcp-module_updated.yaml @@ -0,0 +1,56 @@ +apiVersion: operator.kyma-project.io/v1beta2 +kind: ModuleTemplate +metadata: + name: moduletemplate-template-operator + namespace: default + labels: + "operator.kyma-project.io/module-name": "template-operator" +spec: + channel: regular + data: + apiVersion: operator.kyma-project.io/v1alpha1 + kind: Sample + metadata: + name: sample-yaml + spec: + initKey: valueUpdated + resourceFilePath: "./module-data/yaml" + descriptor: + component: + componentReferences: [] + name: kyma-project.io/template-operator + provider: internal + repositoryContexts: + - baseUrl: registry.docker.io/kyma-project/sap-kyma-jellyfish-dev/template-operator + componentNameMapping: urlPath + type: ociRegistry + resources: + - access: + digest: sha256:db86408caca4c94250d8291aa79655b84146f9cc45e0da49f05a52b3722d74a0 + type: localOciBlob + name: config + relation: local + type: yaml + version: v1.7.2 + - access: + digest: sha256:1735cfa45bf07b63427c8e11717278f8847e352a66af7633611db902386d18ed + type: localOciBlob + name: raw-manifest + relation: local + type: yaml + version: v1.7.2 + sources: + - access: + commit: 4e4b9d47cb655ca23e5c706462485ff7605e8d71 + repoUrl: github.com/kyma-project/template-operator + type: gitHub + labels: + - name: git.kyma-project.io/ref + value: refs/heads/main + version: v1 + name: module-sources + type: git + version: v1.7.2 + version: v1.7.2 + meta: + schemaVersion: v2 diff --git a/internal/descriptor/provider/provider.go b/internal/descriptor/provider/provider.go index 4d36bd06fb..138d10d3db 100644 --- a/internal/descriptor/provider/provider.go +++ b/internal/descriptor/provider/provider.go @@ -12,7 +12,6 @@ import ( var ( ErrTypeAssert = errors.New("failed to convert to v1beta2.Descriptor") ErrDecode = errors.New("failed to decode to descriptor target") - ErrEncode = errors.New("failed to encode the descriptor") ErrTemplateNil = errors.New("module template is nil") ErrDescriptorNil = errors.New("module template contains nil descriptor") ) @@ -71,20 +70,6 @@ func (c *CachedDescriptorProvider) GetDescriptor(template *v1beta2.ModuleTemplat return descriptor, nil } -func (c *CachedDescriptorProvider) SetDescriptor(template *v1beta2.ModuleTemplate, descriptor *v1beta2.Descriptor, -) error { - if template == nil { - return ErrTemplateNil - } - template.Spec.Descriptor.Object = descriptor - encoded, err := compdesc.Encode(descriptor.ComponentDescriptor, compdesc.DefaultJSONCodec) - template.Spec.Descriptor.Raw = encoded - if err != nil { - return errors.Join(ErrEncode, err) - } - return nil -} - func (c *CachedDescriptorProvider) Add(template *v1beta2.ModuleTemplate) error { if template == nil { return ErrTemplateNil diff --git a/internal/descriptor/provider/provider_test.go b/internal/descriptor/provider/provider_test.go index 04bd2cee70..3925d74772 100644 --- a/internal/descriptor/provider/provider_test.go +++ b/internal/descriptor/provider/provider_test.go @@ -106,28 +106,3 @@ func TestGetDescriptor_OnEmptyCache_AddsDescriptorFromTemplate(t *testing.T) { assert.NotNil(t, entry) assert.Equal(t, expected.Name, entry.Name) } - -func TestSetDescriptor_OnEmptyTemplate_ReturnsErrTemplateNil(t *testing.T) { - descriptorProvider := provider.NewCachedDescriptorProvider(nil) - - err := descriptorProvider.SetDescriptor(nil, nil) - - require.Error(t, err) - require.ErrorIs(t, err, provider.ErrTemplateNil) -} - -func TestSetDescriptor_OnProvidedDescriptor_ReturnsNoError(t *testing.T) { - descriptorProvider := provider.NewCachedDescriptorProvider(nil) - descriptorBefore := &v1beta2.Descriptor{ - ComponentDescriptor: &compdesc.ComponentDescriptor{Metadata: compdesc.Metadata{ConfiguredVersion: "v1"}}, - } - template := builder.NewModuleTemplateBuilder().WithDescriptor(descriptorBefore).Build() - - descriptorAfter := &v1beta2.Descriptor{ - ComponentDescriptor: &compdesc.ComponentDescriptor{Metadata: compdesc.Metadata{ConfiguredVersion: "v2"}}, - } - err := descriptorProvider.SetDescriptor(template, descriptorAfter) - - require.NoError(t, err) - assert.Equal(t, descriptorAfter, template.Spec.Descriptor.Object) -} diff --git a/pkg/testutils/builder/moduletemplate.go b/pkg/testutils/builder/moduletemplate.go index a73f32a6de..f7904e3248 100644 --- a/pkg/testutils/builder/moduletemplate.go +++ b/pkg/testutils/builder/moduletemplate.go @@ -143,6 +143,11 @@ func ComponentDescriptorFactoryFromSchema(schemaVersion compdesc.SchemaVersion) return moduleTemplate.Spec.Descriptor } +func ReadComponentDescriptorFromFile(template string, moduleTemplate *v1beta2.ModuleTemplate) { + // needs to be encapsulated in an outside call to make the runtime.Caller(1) find the proper path + readComponentDescriptorFromYaml(template, moduleTemplate) +} + func readComponentDescriptorFromYaml(template string, moduleTemplate *v1beta2.ModuleTemplate) { _, filename, _, ok := runtime.Caller(1) if !ok { diff --git a/pkg/testutils/moduletemplate.go b/pkg/testutils/moduletemplate.go index 1425a44bf0..91f82fea18 100644 --- a/pkg/testutils/moduletemplate.go +++ b/pkg/testutils/moduletemplate.go @@ -27,16 +27,6 @@ func GetModuleTemplate(ctx context.Context, return templateInfo.ModuleTemplate, nil } -func UpdateModuleTemplate(ctx context.Context, - clnt client.Client, - moduleTemplate *v1beta2.ModuleTemplate, -) error { - if err := clnt.Update(ctx, moduleTemplate); err != nil { - return fmt.Errorf("update module tempate: %w", err) - } - return nil -} - func ModuleTemplateExists(ctx context.Context, clnt client.Client, module v1beta2.Module, @@ -75,8 +65,8 @@ func UpdateModuleTemplateSpec(ctx context.Context, return ErrManifestResourceIsNil } moduleTemplate.Spec.Data.Object["spec"] = map[string]any{key: newValue} - if err := UpdateModuleTemplate(ctx, clnt, moduleTemplate); err != nil { - return err + if err := clnt.Update(ctx, moduleTemplate); err != nil { + return fmt.Errorf("update module tempate: %w", err) } return nil } @@ -112,18 +102,3 @@ func ReadModuleVersionFromModuleTemplate(ctx context.Context, clnt client.Client return ocmDesc.Version, nil } - -func ModifyModuleTemplateVersion(moduleTemplate *v1beta2.ModuleTemplate, newVersion string) error { - descriptorProvider := provider.NewCachedDescriptorProvider(nil) - ocmDesc, err := descriptorProvider.GetDescriptor(moduleTemplate) - if err != nil { - return fmt.Errorf("failed to get descriptor: %w", err) - } - ocmDesc.Version = newVersion - err = descriptorProvider.SetDescriptor(moduleTemplate, ocmDesc) - if err != nil { - return fmt.Errorf("failed to set descriptor: %w", err) - } - - return nil -} diff --git a/tests/integration/controller/kyma/kyma_test.go b/tests/integration/controller/kyma/kyma_test.go index 7fc9bb6cf9..3e9b01f66b 100644 --- a/tests/integration/controller/kyma/kyma_test.go +++ b/tests/integration/controller/kyma/kyma_test.go @@ -514,33 +514,9 @@ func updateKCPModuleTemplateSpecData(kymaName, valueUpdated string) func() error return err } for _, activeModule := range createdKyma.Spec.Modules { - err := UpdateModuleTemplateSpec(ctx, kcpClient, + return UpdateModuleTemplateSpec(ctx, kcpClient, activeModule, InitSpecKey, valueUpdated, createdKyma.Spec.Channel) - if err != nil { - return err - } - err = changeKCPModuleTemplateVersion(ctx, activeModule, createdKyma.Spec.Channel, "v1.7.2") // required to allow SSA for manifest - if err != nil { - return err - } } return nil } } - -func changeKCPModuleTemplateVersion(ctx context.Context, - module v1beta2.Module, channel, version string, -) error { - moduleTemplate, err := GetModuleTemplate(ctx, kcpClient, module, channel) - if err != nil { - return err - } - err = ModifyModuleTemplateVersion(moduleTemplate, version) - if err != nil { - return err - } - if err := UpdateModuleTemplate(ctx, kcpClient, moduleTemplate); err != nil { - return err - } - return nil -} diff --git a/tests/integration/controller/kyma/manifest_test.go b/tests/integration/controller/kyma/manifest_test.go index c30471a323..89c506abc4 100644 --- a/tests/integration/controller/kyma/manifest_test.go +++ b/tests/integration/controller/kyma/manifest_test.go @@ -16,6 +16,7 @@ import ( "github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/genericocireg/componentmapping" "github.com/open-component-model/ocm/pkg/runtime" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "sigs.k8s.io/controller-runtime/pkg/client" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -25,6 +26,7 @@ import ( "github.com/kyma-project/lifecycle-manager/internal/pkg/flags" . "github.com/kyma-project/lifecycle-manager/pkg/testutils" + "github.com/kyma-project/lifecycle-manager/pkg/testutils/builder" ) const ( @@ -91,42 +93,25 @@ var _ = Describe("Update Manifest CR", Ordered, func() { WithArguments(kyma.GetName(), kyma.GetNamespace(), kcpClient, shared.StateReady). Should(Succeed()) - By("Update Module Template spec.data.spec field") - valueUpdated := "valueUpdated" - Eventually(updateKCPModuleTemplateSpecData(kyma.Name, valueUpdated), Timeout, Interval).Should(Succeed()) + By("Update Module Template spec") + var moduleTemplateFromFile v1beta2.ModuleTemplate + builder.ReadComponentDescriptorFromFile("operator_v1beta2_moduletemplate_kcp-module_updated.yaml", &moduleTemplateFromFile) - By("CR updated with new value in spec.resource.spec") - Eventually(expectManifestSpecDataEquals(kyma.Name, valueUpdated), Timeout, Interval).Should(Succeed()) - - By("Update Module Template spec.descriptor.component values") - { - newComponentDescriptorRepositoryURL := func(moduleTemplate *v1beta2.ModuleTemplate) error { - descriptor, err := descriptorProvider.GetDescriptor(moduleTemplate) - if err != nil { - return err - } - - repositoryContext := descriptor.GetEffectiveRepositoryContext().Object - _, ok := repositoryContext["baseUrl"].(string) - if !ok { - Fail("Can't find \"baseUrl\" property in ModuleTemplate spec") - } - repositoryContext["baseUrl"] = updateRepositoryURL - descriptor.Version = "v1.7.3" // required to allow for SSA of manifest + moduleTemplateInCluster := &v1beta2.ModuleTemplate{} + err := kcpClient.Get(ctx, client.ObjectKey{ + Name: createModuleTemplateName(module), + Namespace: kyma.GetNamespace()}, moduleTemplateInCluster) + Expect(err).ToNot(HaveOccurred()) - newDescriptorRaw, err := compdesc.Encode(descriptor.ComponentDescriptor, compdesc.DefaultJSONCodec) - Expect(err).ToNot(HaveOccurred()) - moduleTemplate.Spec.Descriptor.Raw = newDescriptorRaw + moduleTemplateInCluster.Spec = moduleTemplateFromFile.Spec - return nil - } + Eventually(kcpClient.Update, Timeout, Interval). + WithContext(ctx). + WithArguments(moduleTemplateInCluster). + Should(Succeed()) - updateKCPModuleTemplateWith := updateKCPModuleTemplate(module, kyma.Spec.Channel) - update := func() error { - return updateKCPModuleTemplateWith(newComponentDescriptorRepositoryURL) - } - Eventually(update, Timeout, Interval).Should(Succeed()) - } + By("CR updated with new value in spec.resource.spec") + Eventually(expectManifestSpecDataEquals(kyma.Name, "valueUpdated"), Timeout, Interval).Should(Succeed()) By("Manifest is updated with new value in spec.install.source") {