Skip to content

Commit

Permalink
Recreate EEC statefulset for PVC changes only (#4131)
Browse files Browse the repository at this point in the history
  • Loading branch information
aorcholski authored Dec 2, 2024
1 parent 02c26a2 commit 3a1dd4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/dynakube/extension/eec/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
Expand All @@ -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"])
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/kubeobjects/statefulset/builder.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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]
Expand All @@ -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...)
}

Expand Down Expand Up @@ -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)
}
}
}
7 changes: 4 additions & 3 deletions pkg/util/kubeobjects/statefulset/query.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 3a1dd4f

Please sign in to comment.