From 3a1dd4f900b040e064e2a2e0562f8270905f0f49 Mon Sep 17 00:00:00 2001 From: aorcholski <84514340+aorcholski@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:31:39 +0100 Subject: [PATCH] Recreate EEC statefulset for PVC changes only (#4131) --- .../dynakube/extension/eec/reconciler_test.go | 4 ++-- pkg/util/kubeobjects/statefulset/builder.go | 20 +++++++++++++++++++ pkg/util/kubeobjects/statefulset/query.go | 7 ++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pkg/controllers/dynakube/extension/eec/reconciler_test.go b/pkg/controllers/dynakube/extension/eec/reconciler_test.go index 90c16302e0..d527949b22 100644 --- a/pkg/controllers/dynakube/extension/eec/reconciler_test.go +++ b/pkg/controllers/dynakube/extension/eec/reconciler_test.go @@ -545,7 +545,7 @@ func TestAnnotations(t *testing.T) { t.Run("the default annotations", func(t *testing.T) { statefulSet := getStatefulset(t, getTestDynakube()) - assert.Len(t, statefulSet.ObjectMeta.Annotations, 1) + assert.Len(t, statefulSet.ObjectMeta.Annotations, 2) require.Len(t, statefulSet.Spec.Template.ObjectMeta.Annotations, 1) assert.NotEmpty(t, statefulSet.Spec.Template.ObjectMeta.Annotations[consts.ExtensionsAnnotationSecretHash]) }) @@ -559,7 +559,7 @@ func TestAnnotations(t *testing.T) { statefulSet := getStatefulset(t, dk) - assert.Len(t, statefulSet.ObjectMeta.Annotations, 1) + assert.Len(t, statefulSet.ObjectMeta.Annotations, 2) assert.Empty(t, statefulSet.ObjectMeta.Annotations["a"]) require.Len(t, statefulSet.Spec.Template.ObjectMeta.Annotations, 2) assert.Equal(t, "b", statefulSet.Spec.Template.ObjectMeta.Annotations["a"]) diff --git a/pkg/util/kubeobjects/statefulset/builder.go b/pkg/util/kubeobjects/statefulset/builder.go index 9356a64687..1665926cbe 100644 --- a/pkg/util/kubeobjects/statefulset/builder.go +++ b/pkg/util/kubeobjects/statefulset/builder.go @@ -1,6 +1,8 @@ package statefulset import ( + "github.com/Dynatrace/dynatrace-operator/pkg/api" + "github.com/Dynatrace/dynatrace-operator/pkg/util/hasher" "github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/internal/builder" maputils "github.com/Dynatrace/dynatrace-operator/pkg/util/map" appsv1 "k8s.io/api/apps/v1" @@ -9,6 +11,10 @@ import ( "k8s.io/utils/ptr" ) +const ( + pvcAnnotationHash = api.InternalFlagPrefix + "pvc-hash" +) + var ( // Mandatory fields, provided in constructor as named params setName = builder.SetName[*appsv1.StatefulSet] @@ -26,6 +32,8 @@ func Build(owner metav1.Object, name string, container corev1.Container, options } neededOpts = append(neededOpts, options...) + neededOpts = append(neededOpts, setPVCAnnotation()) + return builder.Build(owner, &appsv1.StatefulSet{}, neededOpts...) } @@ -111,3 +119,15 @@ func SetUpdateStrategy(updateStartegy appsv1.StatefulSetUpdateStrategy) builder. s.Spec.UpdateStrategy = updateStartegy } } + +func setPVCAnnotation() builder.Option[*appsv1.StatefulSet] { + return func(s *appsv1.StatefulSet) { + if s.Spec.VolumeClaimTemplates != nil { + if s.ObjectMeta.Annotations == nil { + s.ObjectMeta.Annotations = map[string]string{} + } + + s.ObjectMeta.Annotations[pvcAnnotationHash], _ = hasher.GenerateHash(s.Spec.VolumeClaimTemplates) + } + } +} diff --git a/pkg/util/kubeobjects/statefulset/query.go b/pkg/util/kubeobjects/statefulset/query.go index 65568feff7..39a841f4d8 100644 --- a/pkg/util/kubeobjects/statefulset/query.go +++ b/pkg/util/kubeobjects/statefulset/query.go @@ -1,8 +1,6 @@ package statefulset import ( - "reflect" - "github.com/Dynatrace/dynatrace-operator/pkg/logd" "github.com/Dynatrace/dynatrace-operator/pkg/util/hasher" "github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/internal/query" @@ -37,5 +35,8 @@ func isEqual(current, desired *appsv1.StatefulSet) bool { } func mustRecreate(current, desired *appsv1.StatefulSet) bool { - return labels.NotEqual(current.Spec.Selector.MatchLabels, desired.Spec.Selector.MatchLabels) || !reflect.DeepEqual(current.Spec.VolumeClaimTemplates, desired.Spec.VolumeClaimTemplates) + currentHash := current.Annotations[pvcAnnotationHash] + desiredHash := desired.Annotations[pvcAnnotationHash] + + return labels.NotEqual(current.Spec.Selector.MatchLabels, desired.Spec.Selector.MatchLabels) || currentHash != desiredHash }