From 7f8e043eb30b89ae75ac7acb95ca5231120e9453 Mon Sep 17 00:00:00 2001 From: Nesma Badr Date: Tue, 1 Oct 2024 10:38:07 +0200 Subject: [PATCH] chore: Make managed-by label value transparent (#1894) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change managedBy label to kyma instead of lifecycle-manager or declarative-v2 * test change * Revert test change * Add managed by label to skr-webhook resources and kyma cr * Add managed by label to default CR * Add managed by label to CRDs and moduletemplate crds * fix linting issues * fix coverage * Rename kymalabelvalue variable * Add label to default CR * Add label to resources * Add a helper function for the merging of maps * Fix linting issues --------- Co-authored-by: Christoph Schwägerl --- api/shared/operator_labels.go | 6 ++- api/v1beta2/kyma_types.go | 6 +-- api/v1beta2/moduletemplate_types.go | 1 - internal/controller/manifest/setup.go | 4 +- internal/declarative/v2/default_transforms.go | 49 ++++++------------- .../declarative/v2/default_transforms_test.go | 15 ++++-- internal/declarative/v2/options.go | 8 ++- internal/manifest/custom_resource.go | 10 +++- internal/remote/crd_upgrade.go | 20 ++++++-- internal/remote/remote_catalog.go | 12 ++++- internal/remote/skr_context.go | 3 +- internal/util/collections/collections.go | 15 ++++++ internal/util/collections/collections_test.go | 49 +++++++++++++++++++ pkg/watcher/skr_webhook_manifest_manager.go | 5 ++ pkg/watcher/skr_webhook_resources.go | 11 +++++ tests/e2e/manifest_reconciliation_test.go | 8 ++- tests/e2e/utils_test.go | 29 +++++++++-- .../deletion/controller_test.go | 3 +- unit-test-coverage.yaml | 8 +-- 19 files changed, 194 insertions(+), 68 deletions(-) create mode 100644 internal/util/collections/collections.go create mode 100644 internal/util/collections/collections_test.go diff --git a/api/shared/operator_labels.go b/api/shared/operator_labels.go index 428cdb48cc..d8df87a81b 100644 --- a/api/shared/operator_labels.go +++ b/api/shared/operator_labels.go @@ -8,7 +8,9 @@ const ( ControllerName = OperatorGroup + Separator + "controller-name" ChannelLabel = OperatorGroup + Separator + "channel" // ManagedBy defines the controller managing the resource. - ManagedBy = OperatorGroup + Separator + "managed-by" + ManagedBy = OperatorGroup + Separator + "managed-by" + ManagedByLabelValue = kymaValue + kymaValue = "kyma" IstioInjectionLabel = "istio-injection" WardenLabel = "namespaces.warden.kyma-project.io/validate" @@ -24,7 +26,7 @@ const ( // WatchedByLabel defines a redirect to a controller that should be getting a notification // if this resource is changed. WatchedByLabel = OperatorGroup + Separator + "watched-by" - WatchedByLabelValue = "kyma" + WatchedByLabelValue = kymaValue // PurposeLabel defines the purpose of the resource, i.e. Secrets which will be used to certificate management. PurposeLabel = OperatorGroup + Separator + "purpose" CertManager = "klm-watcher-cert-manager" diff --git a/api/v1beta2/kyma_types.go b/api/v1beta2/kyma_types.go index 319a80d1f5..cb74b14a18 100644 --- a/api/v1beta2/kyma_types.go +++ b/api/v1beta2/kyma_types.go @@ -78,9 +78,9 @@ type Module struct { // ModuleTemplate based on this specific version. // The Version and Channel are mutually exclusive options. // The regular expression come from here: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string - // json:"-" to disable installation of specific versions until decided to roll this out - // see https://github.com/kyma-project/lifecycle-manager/issues/1847 - Version string `json:"-"` + // json:"-" to disable installation of specific versions until decided to roll this out + // see https://github.com/kyma-project/lifecycle-manager/issues/1847 + Version string `json:"-"` // RemoteModuleTemplateRef is deprecated and will no longer have any functionality. // It will be removed in the upcoming API version. diff --git a/api/v1beta2/moduletemplate_types.go b/api/v1beta2/moduletemplate_types.go index 63346aa746..8e5b5206d3 100644 --- a/api/v1beta2/moduletemplate_types.go +++ b/api/v1beta2/moduletemplate_types.go @@ -213,7 +213,6 @@ func (m *ModuleTemplate) GetVersion() (*semver.Version, error) { } } else { versionValue = m.Spec.Version - } version, err := semver.NewVersion(versionValue) diff --git a/internal/controller/manifest/setup.go b/internal/controller/manifest/setup.go index 42f9b64b3d..a5b7a83b4f 100644 --- a/internal/controller/manifest/setup.go +++ b/internal/controller/manifest/setup.go @@ -20,8 +20,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/source" + "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" - declarativev2 "github.com/kyma-project/lifecycle-manager/internal/declarative/v2" "github.com/kyma-project/lifecycle-manager/internal/pkg/metrics" "github.com/kyma-project/lifecycle-manager/pkg/queue" "github.com/kyma-project/lifecycle-manager/pkg/security" @@ -52,7 +52,7 @@ func SetupWithManager(mgr manager.Manager, } runnableListener := watcherevent.NewSKREventListener( - settings.ListenerAddr, strings.ToLower(declarativev2.OperatorName), + settings.ListenerAddr, strings.ToLower(shared.OperatorName), verifyFunc, ) diff --git a/internal/declarative/v2/default_transforms.go b/internal/declarative/v2/default_transforms.go index 1d24a294d4..62cf0d18f5 100644 --- a/internal/declarative/v2/default_transforms.go +++ b/internal/declarative/v2/default_transforms.go @@ -7,11 +7,10 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/kyma-project/lifecycle-manager/api/shared" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" ) const ( - OperatorName = "module-manager" - ManagedByLabelValue = "declarative-v2" DisclaimerAnnotation = shared.OperatorGroup + shared.Separator + "managed-by-reconciler-disclaimer" DisclaimerAnnotationValue = "DO NOT EDIT - This resource is managed by Kyma.\n" + "Any modifications are discarded and the resource is reverted to the original state." @@ -32,44 +31,24 @@ func DisclaimerTransform(_ context.Context, _ Object, resources []*unstructured. func KymaComponentTransform(_ context.Context, obj Object, resources []*unstructured.Unstructured) error { for _, resource := range resources { - lbls := resource.GetLabels() - if lbls == nil { - lbls = make(map[string]string) - } - lbls["app.kubernetes.io/component"] = obj.GetName() - lbls["app.kubernetes.io/part-of"] = "Kyma" - resource.SetLabels(lbls) - } - return nil -} - -func ManagedByDeclarativeV2(_ context.Context, _ Object, resources []*unstructured.Unstructured) error { - for _, resource := range resources { - lbls := resource.GetLabels() - if lbls == nil { - lbls = make(map[string]string) - } - // legacy managed by value - lbls[shared.ManagedBy] = ManagedByLabelValue - resource.SetLabels(lbls) + resource.SetLabels(collections.MergeMaps(resource.GetLabels(), map[string]string{ + "app.kubernetes.io/component": obj.GetName(), + "app.kubernetes.io/part-of": "Kyma", + })) } return nil } -func watchedByOwnedBy(_ context.Context, obj Object, resources []*unstructured.Unstructured) error { +func WatchedByManagedByOwnedBy(_ context.Context, obj Object, resources []*unstructured.Unstructured) error { for _, resource := range resources { - lbls := resource.GetLabels() - if lbls == nil { - lbls = make(map[string]string) - } - lbls[shared.WatchedByLabel] = shared.WatchedByLabelValue - - annotations := resource.GetAnnotations() - if annotations == nil { - annotations = make(map[string]string) - } - annotations[shared.OwnedByAnnotation] = fmt.Sprintf(OwnedByFormat, obj.GetNamespace(), obj.GetName()) - resource.SetLabels(lbls) + resource.SetLabels(collections.MergeMaps(resource.GetLabels(), map[string]string{ + shared.WatchedByLabel: shared.WatchedByLabelValue, + shared.ManagedBy: shared.ManagedByLabelValue, + })) + + resource.SetAnnotations(collections.MergeMaps(resource.GetAnnotations(), map[string]string{ + shared.OwnedByAnnotation: fmt.Sprintf(OwnedByFormat, obj.GetNamespace(), obj.GetName()), + })) } return nil } diff --git a/internal/declarative/v2/default_transforms_test.go b/internal/declarative/v2/default_transforms_test.go index 3750ff84eb..d75ab73ff5 100644 --- a/internal/declarative/v2/default_transforms_test.go +++ b/internal/declarative/v2/default_transforms_test.go @@ -44,8 +44,8 @@ func Test_defaultTransforms(t *testing.T) { }, }, { - "empty ManagedByDeclarativeV2", - declarativev2.ManagedByDeclarativeV2, + "empty WatchedByManagedByOwnedBy", + declarativev2.WatchedByManagedByOwnedBy, []*unstructured.Unstructured{}, func(testingT assert.TestingT, err error, i ...interface{}) bool { require.NoError(t, err) @@ -87,8 +87,8 @@ func Test_defaultTransforms(t *testing.T) { }, }, { - "simple ManagedByDeclarativeV2", - declarativev2.ManagedByDeclarativeV2, + "simple WatchedByManagedByOwnedBy", + declarativev2.WatchedByManagedByOwnedBy, []*unstructured.Unstructured{{Object: map[string]any{}}}, func(testingT assert.TestingT, err error, i ...interface{}) bool { require.NoError(t, err) @@ -97,9 +97,14 @@ func Test_defaultTransforms(t *testing.T) { unstruct := unstructs[0] assert.NotEmpty(testingT, unstruct) assert.NotNil(testingT, unstruct.GetLabels()) + assert.NotNil(testingT, unstruct.GetAnnotations()) assert.Contains(testingT, unstruct.GetLabels(), shared.ManagedBy) - assert.Equal(testingT, declarativev2.ManagedByLabelValue, + assert.Contains(testingT, unstruct.GetLabels(), shared.WatchedByLabel) + assert.Contains(testingT, unstruct.GetAnnotations(), shared.OwnedByAnnotation) + assert.Equal(testingT, shared.ManagedByLabelValue, unstruct.GetLabels()[shared.ManagedBy]) + assert.Equal(testingT, shared.WatchedByLabelValue, + unstruct.GetLabels()[shared.WatchedByLabel]) return true }, }, diff --git a/internal/declarative/v2/options.go b/internal/declarative/v2/options.go index 3937666e0b..cc77da9a53 100644 --- a/internal/declarative/v2/options.go +++ b/internal/declarative/v2/options.go @@ -11,6 +11,8 @@ import ( "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/manager" + + "github.com/kyma-project/lifecycle-manager/api/shared" ) const ( @@ -21,14 +23,16 @@ const ( func DefaultOptions() *Options { return (&Options{}).Apply( WithPostRenderTransform( - ManagedByDeclarativeV2, - watchedByOwnedBy, + WatchedByManagedByOwnedBy, KymaComponentTransform, DisclaimerTransform, ), WithSingletonClientCache(NewMemoryClientCache()), WithManifestCache(os.TempDir()), WithManifestParser(NewInMemoryCachedManifestParser(DefaultInMemoryParseTTL)), + WithCustomResourceLabels{ + shared.ManagedBy: shared.ManagedByLabelValue, + }, ) } diff --git a/internal/manifest/custom_resource.go b/internal/manifest/custom_resource.go index b97f9f2323..d4968862b4 100644 --- a/internal/manifest/custom_resource.go +++ b/internal/manifest/custom_resource.go @@ -9,8 +9,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" declarativev2 "github.com/kyma-project/lifecycle-manager/internal/declarative/v2" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" "github.com/kyma-project/lifecycle-manager/pkg/util" ) @@ -31,6 +33,10 @@ func PostRunCreateCR( } resource := manifest.Spec.Resource.DeepCopy() + resource.SetLabels(collections.MergeMaps(resource.GetLabels(), map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + })) + err := skr.Create(ctx, resource, client.FieldOwner(declarativev2.CustomResourceManagerFinalizer)) if err != nil && !apierrors.IsAlreadyExists(err) { return fmt.Errorf("failed to create resource: %w", err) @@ -41,9 +47,11 @@ func PostRunCreateCR( oMeta.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind()) oMeta.SetNamespace(obj.GetNamespace()) oMeta.SetFinalizers(obj.GetFinalizers()) + if added := controllerutil.AddFinalizer(oMeta, declarativev2.CustomResourceManagerFinalizer); added { if err := kcp.Patch( - ctx, oMeta, client.Apply, client.ForceOwnership, client.FieldOwner(declarativev2.CustomResourceManagerFinalizer), + ctx, oMeta, client.Apply, client.ForceOwnership, + client.FieldOwner(declarativev2.CustomResourceManagerFinalizer), ); err != nil { return fmt.Errorf("failed to patch resource: %w", err) } diff --git a/internal/remote/crd_upgrade.go b/internal/remote/crd_upgrade.go index 0709f28574..ee7ba4043f 100644 --- a/internal/remote/crd_upgrade.go +++ b/internal/remote/crd_upgrade.go @@ -16,6 +16,7 @@ import ( "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" "github.com/kyma-project/lifecycle-manager/internal/crd" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" ) type SyncCrdsUseCase struct { @@ -24,7 +25,9 @@ type SyncCrdsUseCase struct { crdCache *crd.Cache } -func NewSyncCrdsUseCase(kcpClient client.Client, skrContextFactory SkrContextProvider, cache *crd.Cache) SyncCrdsUseCase { +func NewSyncCrdsUseCase(kcpClient client.Client, skrContextFactory SkrContextProvider, + cache *crd.Cache, +) SyncCrdsUseCase { if cache == nil { return SyncCrdsUseCase{ kcpClient: kcpClient, @@ -52,7 +55,8 @@ func (s *SyncCrdsUseCase) Execute(ctx context.Context, kyma *v1beta2.Kyma) (bool } } - moduleTemplateCrdUpdated, err := s.fetchCrdsAndUpdateKymaAnnotations(ctx, skrContext.Client, kyma, shared.ModuleTemplateKind.Plural()) + moduleTemplateCrdUpdated, err := s.fetchCrdsAndUpdateKymaAnnotations(ctx, skrContext.Client, kyma, + shared.ModuleTemplateKind.Plural()) if err != nil { err = client.IgnoreNotFound(err) if err != nil { @@ -70,6 +74,11 @@ func PatchCRD(ctx context.Context, clnt client.Client, crd *apiextensionsv1.Cust crdToApply.Spec = crd.Spec crdToApply.Spec.Conversion.Strategy = apiextensionsv1.NoneConverter crdToApply.Spec.Conversion.Webhook = nil + + crdToApply.SetLabels(collections.MergeMaps(crdToApply.GetLabels(), map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + })) + err := clnt.Patch(ctx, crdToApply, client.Apply, client.ForceOwnership, @@ -127,7 +136,8 @@ func getAnnotation(crd *apiextensionsv1.CustomResourceDefinition, crdType CrdTyp return fmt.Sprintf("%s-%s-crd-generation", strings.ToLower(crd.Spec.Names.Kind), strings.ToLower(string(crdType))) } -func (s *SyncCrdsUseCase) fetchCrdsAndUpdateKymaAnnotations(ctx context.Context, skrClient Client, kyma *v1beta2.Kyma, plural string, +func (s *SyncCrdsUseCase) fetchCrdsAndUpdateKymaAnnotations(ctx context.Context, skrClient Client, kyma *v1beta2.Kyma, + plural string, ) (bool, error) { kcpCrd, skrCrd, err := s.fetchCrds(ctx, skrClient, plural) if err != nil { @@ -153,7 +163,9 @@ func (s *SyncCrdsUseCase) fetchCrdsAndUpdateKymaAnnotations(ctx context.Context, return crdUpdated, nil } -func (s *SyncCrdsUseCase) fetchCrds(ctx context.Context, skrClient Client, plural string) (*apiextensionsv1.CustomResourceDefinition, *apiextensionsv1.CustomResourceDefinition, error) { +func (s *SyncCrdsUseCase) fetchCrds(ctx context.Context, skrClient Client, + plural string, +) (*apiextensionsv1.CustomResourceDefinition, *apiextensionsv1.CustomResourceDefinition, error) { kcpCrdName := fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group) kcpCrd, ok := s.crdCache.Get(kcpCrdName) if !ok { diff --git a/internal/remote/remote_catalog.go b/internal/remote/remote_catalog.go index 1ab827e30b..cf1a7442c6 100644 --- a/internal/remote/remote_catalog.go +++ b/internal/remote/remote_catalog.go @@ -13,6 +13,7 @@ import ( "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" "github.com/kyma-project/lifecycle-manager/pkg/util" ) @@ -37,7 +38,9 @@ type Catalog interface { Delete(ctx context.Context) error } -func NewRemoteCatalogFromKyma(kcpClient client.Client, skrContextFactory SkrContextProvider, remoteSyncNamespace string) *RemoteCatalog { +func NewRemoteCatalogFromKyma(kcpClient client.Client, skrContextFactory SkrContextProvider, + remoteSyncNamespace string, +) *RemoteCatalog { force := true return NewRemoteCatalog(kcpClient, skrContextFactory, Settings{ @@ -212,6 +215,9 @@ func (c *RemoteCatalog) prepareForSSA(moduleTemplate *v1beta2.ModuleTemplate) { moduleTemplate.SetResourceVersion("") moduleTemplate.SetUID("") moduleTemplate.SetManagedFields([]apimetav1.ManagedFieldsEntry{}) + moduleTemplate.SetLabels(collections.MergeMaps(moduleTemplate.GetLabels(), map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + })) if c.settings.Namespace != "" { moduleTemplate.SetNamespace(c.settings.Namespace) @@ -250,7 +256,9 @@ func (c *RemoteCatalog) Delete( func (c *RemoteCatalog) createModuleTemplateCRDInRuntime(ctx context.Context, kyma types.NamespacedName) error { kcpCrd := &apiextensionsv1.CustomResourceDefinition{} skrCrd := &apiextensionsv1.CustomResourceDefinition{} - objKey := client.ObjectKey{Name: fmt.Sprintf("%s.%s", shared.ModuleTemplateKind.Plural(), v1beta2.GroupVersion.Group)} + objKey := client.ObjectKey{ + Name: fmt.Sprintf("%s.%s", shared.ModuleTemplateKind.Plural(), v1beta2.GroupVersion.Group), + } err := c.kcpClient.Get(ctx, objKey, kcpCrd) if err != nil { return fmt.Errorf("failed to get ModuleTemplate CRD from KCP: %w", err) diff --git a/internal/remote/skr_context.go b/internal/remote/skr_context.go index 5c8dc095f9..2bf3dc42a3 100644 --- a/internal/remote/skr_context.go +++ b/internal/remote/skr_context.go @@ -76,7 +76,7 @@ func (s *SkrContext) CreateKymaNamespace(ctx context.Context) error { ObjectMeta: apimetav1.ObjectMeta{ Name: shared.DefaultRemoteNamespace, Labels: map[string]string{ - shared.ManagedBy: shared.OperatorName, + shared.ManagedBy: shared.ManagedByLabelValue, shared.IstioInjectionLabel: shared.EnabledValue, shared.WardenLabel: shared.EnabledValue, }, @@ -210,6 +210,7 @@ func (s *SkrContext) syncWatcherLabelsAnnotations(controlPlaneKyma, remoteKyma * } remoteKyma.Labels[shared.WatchedByLabel] = shared.WatchedByLabelValue + remoteKyma.Labels[shared.ManagedBy] = shared.ManagedByLabelValue if remoteKyma.Annotations == nil { remoteKyma.Annotations = make(map[string]string) diff --git a/internal/util/collections/collections.go b/internal/util/collections/collections.go new file mode 100644 index 0000000000..6edb0259e6 --- /dev/null +++ b/internal/util/collections/collections.go @@ -0,0 +1,15 @@ +package collections + +func MergeMaps(map1, map2 map[string]string) map[string]string { + mergedMap := make(map[string]string) + + for k, v := range map1 { + mergedMap[k] = v + } + + for k, v := range map2 { + mergedMap[k] = v + } + + return mergedMap +} diff --git a/internal/util/collections/collections_test.go b/internal/util/collections/collections_test.go new file mode 100644 index 0000000000..5632ad24a4 --- /dev/null +++ b/internal/util/collections/collections_test.go @@ -0,0 +1,49 @@ +package collections_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/kyma-project/lifecycle-manager/internal/util/collections" +) + +func TestMergeMaps_WhenMapIsEmpty(t *testing.T) { + firstMap := map[string]string{} + secondMap := map[string]string{ + "key": "value", + } + + mergedMap := collections.MergeMaps(firstMap, secondMap) + require.Len(t, mergedMap, 1) + require.Contains(t, mergedMap, "key") + require.Equal(t, "value", mergedMap["key"]) +} + +func TestMergeMaps_WhenMapIsNil(t *testing.T) { + var firstMap map[string]string + secondMap := map[string]string{ + "key": "value", + } + + mergedMap := collections.MergeMaps(firstMap, secondMap) + require.Len(t, mergedMap, 1) + require.Contains(t, mergedMap, "key") + require.Equal(t, "value", mergedMap["key"]) +} + +func TestMergeMaps_WhenBothMapsHaveValues(t *testing.T) { + firstMap := map[string]string{ + "key1": "value1", + } + secondMap := map[string]string{ + "key2": "value2", + } + + mergedMap := collections.MergeMaps(firstMap, secondMap) + require.Len(t, mergedMap, 2) + require.Contains(t, mergedMap, "key1") + require.Equal(t, "value1", mergedMap["key1"]) + require.Contains(t, mergedMap, "key2") + require.Equal(t, "value2", mergedMap["key2"]) +} diff --git a/pkg/watcher/skr_webhook_manifest_manager.go b/pkg/watcher/skr_webhook_manifest_manager.go index 2dc1b97a43..06d313a2fc 100644 --- a/pkg/watcher/skr_webhook_manifest_manager.go +++ b/pkg/watcher/skr_webhook_manifest_manager.go @@ -14,9 +14,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" "github.com/kyma-project/lifecycle-manager/internal/pkg/metrics" "github.com/kyma-project/lifecycle-manager/internal/remote" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" "github.com/kyma-project/lifecycle-manager/pkg/log" "github.com/kyma-project/lifecycle-manager/pkg/util" ) @@ -200,6 +202,9 @@ func (m *SKRWebhookManifestManager) getRawManifestClientObjects(cfg *unstructure resources := make([]client.Object, 0) for _, baseRes := range m.baseResources { resource := baseRes.DeepCopy() + resource.SetLabels(collections.MergeMaps(resource.GetLabels(), map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + })) configuredResource, err := configureUnstructuredObject(cfg, resource) if err != nil { return nil, fmt.Errorf("failed to configure %s resource: %w", resource.GetKind(), err) diff --git a/pkg/watcher/skr_webhook_resources.go b/pkg/watcher/skr_webhook_resources.go index 264c2b5257..49d685f25a 100644 --- a/pkg/watcher/skr_webhook_resources.go +++ b/pkg/watcher/skr_webhook_resources.go @@ -19,6 +19,7 @@ import ( "github.com/kyma-project/lifecycle-manager/api/shared" "github.com/kyma-project/lifecycle-manager/api/v1beta2" + "github.com/kyma-project/lifecycle-manager/internal/util/collections" "github.com/kyma-project/lifecycle-manager/pkg/log" ) @@ -59,6 +60,9 @@ func createSKRSecret(cfg *unstructuredResourcesConfig, secretObjKey client.Objec ObjectMeta: apimetav1.ObjectMeta{ Name: secretObjKey.Name, Namespace: secretObjKey.Namespace, + Labels: map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + }, }, Immutable: nil, Data: map[string][]byte{ @@ -128,6 +132,9 @@ func generateValidatingWebhookConfigFromWatchers(webhookObjKey, ObjectMeta: apimetav1.ObjectMeta{ Name: webhookObjKey.Name, Namespace: webhookObjKey.Namespace, + Labels: map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + }, }, Webhooks: webhooks, } @@ -187,6 +194,10 @@ func configureDeployment(cfg *unstructuredResourcesConfig, obj *unstructured.Uns } deployment.Spec.Template.Spec.Containers[0] = serverContainer + deployment.SetLabels(collections.MergeMaps(deployment.GetLabels(), map[string]string{ + shared.ManagedBy: shared.ManagedByLabelValue, + })) + return deployment, nil } diff --git a/tests/e2e/manifest_reconciliation_test.go b/tests/e2e/manifest_reconciliation_test.go index 68148f12c0..16f109911a 100644 --- a/tests/e2e/manifest_reconciliation_test.go +++ b/tests/e2e/manifest_reconciliation_test.go @@ -12,7 +12,7 @@ import ( . "github.com/kyma-project/lifecycle-manager/pkg/testutils" ) -var _ = Describe("Manifest Skip Reconciliation Label", Ordered, func() { +var _ = FDescribe("Manifest Skip Reconciliation Label", Ordered, func() { kyma := NewKymaWithSyncLabel("kyma-sample", ControlPlaneNamespace, v1beta2.DefaultChannel) module := NewTemplateOperator(v1beta2.DefaultChannel) waitingForFinalizersOperationMsg := "waiting as other finalizers are present" @@ -37,6 +37,12 @@ var _ = Describe("Manifest Skip Reconciliation Label", Ordered, func() { WithContext(ctx). WithArguments(TestModuleCRName, RemoteNamespace, skrClient, shared.StateReady). Should(Succeed()) + By("And the SKR Module Default CR has the Managed-by Label") + Eventually(CheckSampleCRHasExpectedLabel). + WithContext(ctx). + WithArguments(TestModuleCRName, RemoteNamespace, skrClient, shared.ManagedBy, + shared.ManagedByLabelValue). + Should(Succeed()) By("And the KCP Kyma CR is in a \"Ready\" State") Eventually(KymaIsInState). WithContext(ctx). diff --git a/tests/e2e/utils_test.go b/tests/e2e/utils_test.go index c18acb4437..ee79bb0095 100644 --- a/tests/e2e/utils_test.go +++ b/tests/e2e/utils_test.go @@ -25,9 +25,10 @@ import ( ) var ( - errKymaNotInExpectedState = errors.New("kyma CR not in expected state") - errModuleNotExisting = errors.New("module does not exists in KymaCR") - errLabelNotExist = errors.New("label does not exist on namespace") + errKymaNotInExpectedState = errors.New("kyma CR not in expected state") + errModuleNotExisting = errors.New("module does not exists in KymaCR") + errLabelNotExistOnNamespace = errors.New("label does not exist on namespace") + errLabelNotExistOnSampleCR = errors.New("label does not exist on CustomResource") ) const ( @@ -172,7 +173,7 @@ func EnsureNamespaceHasCorrectLabels(ctx context.Context, clnt client.Client, ky } if namespace.Labels == nil { - return errLabelNotExist + return errLabelNotExistOnNamespace } for k, v := range labels { @@ -220,3 +221,23 @@ func CheckSampleCRIsInState(ctx context.Context, name, namespace string, clnt cl clnt, expectedState) } + +func CheckSampleCRHasExpectedLabel(ctx context.Context, name, namespace string, clnt client.Client, + labelKey, labelValue string, +) error { + customResource, err := GetCR(ctx, clnt, client.ObjectKey{Name: name, Namespace: namespace}, schema.GroupVersionKind{ + Group: "operator.kyma-project.io", + Version: "v1alpha1", + Kind: string(templatev1alpha1.SampleKind), + }) + if err != nil { + return err + } + + labels := customResource.GetLabels() + if labels == nil || labels[labelKey] != labelValue { + return errLabelNotExistOnSampleCR + } + + return nil +} diff --git a/tests/integration/controller/mandatorymodule/deletion/controller_test.go b/tests/integration/controller/mandatorymodule/deletion/controller_test.go index 6cd52188da..2d7239727d 100644 --- a/tests/integration/controller/mandatorymodule/deletion/controller_test.go +++ b/tests/integration/controller/mandatorymodule/deletion/controller_test.go @@ -102,7 +102,8 @@ func registerControlPlaneLifecycleForKyma(kyma *v1beta2.Kyma) { installName := filepath.Join("main-dir", "installs") mandatoryManifest.Annotations = map[string]string{shared.FQDN: "kyma-project.io/template-operator"} - validImageSpec, err := CreateOCIImageSpecFromFile(installName, server.Listener.Addr().String(), manifestFilePath, + validImageSpec, err := CreateOCIImageSpecFromFile(installName, server.Listener.Addr().String(), + manifestFilePath, false) Expect(err).NotTo(HaveOccurred()) imageSpecByte, err := json.Marshal(validImageSpec) diff --git a/unit-test-coverage.yaml b/unit-test-coverage.yaml index 0200d746e2..50122cafa0 100644 --- a/unit-test-coverage.yaml +++ b/unit-test-coverage.yaml @@ -3,11 +3,11 @@ packages: internal/descriptor/cache: 92.3 internal/descriptor/provider: 66.7 internal/event: 100 - internal/manifest/statecheck: 66 + internal/manifest/statecheck: 71 internal/manifest/filemutex: 100 - internal/manifest: 7 + internal/manifest: 10 internal/istio: 63.3 internal/pkg/resources: 91.7 - internal/remote: 6.6 - pkg/templatelookup: 72 + internal/remote: 6.4 + pkg/templatelookup: 76