Skip to content

Commit

Permalink
Add managed by label to skr-webhook resources and kyma cr
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Sep 26, 2024
1 parent ec2bd89 commit 9c71592
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions internal/controller/manifest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
)

Expand Down
18 changes: 3 additions & 15 deletions internal/declarative/v2/default_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

const (
OperatorName = "module-manager"
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."
Expand Down Expand Up @@ -42,33 +41,22 @@ func KymaComponentTransform(_ context.Context, obj Object, resources []*unstruct
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] = shared.KymaLabelValue
resource.SetLabels(lbls)
}
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
lbls[shared.ManagedBy] = shared.KymaLabelValue

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.SetAnnotations(annotations)
}
return nil
}
13 changes: 9 additions & 4 deletions internal/declarative/v2/default_transforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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.Contains(testingT, unstruct.GetLabels(), shared.WatchedByLabel)
assert.Contains(testingT, unstruct.GetAnnotations(), shared.OwnedByAnnotation)
assert.Equal(testingT, shared.KymaLabelValue,
unstruct.GetLabels()[shared.ManagedBy])
assert.Equal(testingT, shared.WatchedByLabelValue,
unstruct.GetLabels()[shared.WatchedByLabel])
return true
},
},
Expand Down
3 changes: 1 addition & 2 deletions internal/declarative/v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const (
func DefaultOptions() *Options {
return (&Options{}).Apply(
WithPostRenderTransform(
ManagedByDeclarativeV2,
watchedByOwnedBy,
WatchedByManagedByOwnedBy,
KymaComponentTransform,
DisclaimerTransform,
),
Expand Down
1 change: 1 addition & 0 deletions internal/remote/skr_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (s *SkrContext) syncWatcherLabelsAnnotations(controlPlaneKyma, remoteKyma *
}

remoteKyma.Labels[shared.WatchedByLabel] = shared.WatchedByLabelValue
remoteKyma.Labels[shared.ManagedBy] = shared.KymaLabelValue

if remoteKyma.Annotations == nil {
remoteKyma.Annotations = make(map[string]string)
Expand Down
13 changes: 13 additions & 0 deletions pkg/watcher/skr_webhook_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func createSKRSecret(cfg *unstructuredResourcesConfig, secretObjKey client.Objec
ObjectMeta: apimetav1.ObjectMeta{
Name: secretObjKey.Name,
Namespace: secretObjKey.Namespace,
Labels: map[string]string{
shared.ManagedBy: shared.KymaLabelValue,
},
},
Immutable: nil,
Data: map[string][]byte{
Expand Down Expand Up @@ -128,6 +131,9 @@ func generateValidatingWebhookConfigFromWatchers(webhookObjKey,
ObjectMeta: apimetav1.ObjectMeta{
Name: webhookObjKey.Name,
Namespace: webhookObjKey.Namespace,
Labels: map[string]string{
shared.ManagedBy: shared.KymaLabelValue,
},
},
Webhooks: webhooks,
}
Expand Down Expand Up @@ -187,6 +193,13 @@ func configureDeployment(cfg *unstructuredResourcesConfig, obj *unstructured.Uns
}
deployment.Spec.Template.Spec.Containers[0] = serverContainer

labels := deployment.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[shared.ManagedBy] = shared.KymaLabelValue
deployment.SetLabels(labels)

return deployment, nil
}

Expand Down

0 comments on commit 9c71592

Please sign in to comment.