Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit kubectl annotations when managing statefulsets #1582

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions pkg/controller/common/statefulset/statefulset-reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package statefulset

import (
"context"
"context"
"strings"
"time"

apps "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -200,6 +201,27 @@ func (r *Reconciler) recreateStatefulSet(ctx context.Context, host *api.Host, re
return r.createStatefulSet(ctx, host, register, opts)
}

// copyKubectlAnnotationsBeforeUpdate copies kubectl annotations from old to new statefulset as they are set by external kubectl
// operations and should be preserved during statefulset update
func (r *Reconciler) copyKubectlAnnotationsBeforeUpdate(old, new *apps.StatefulSet) {
if old == nil || new == nil {
return
}

oldMeta := old.Spec.Template.ObjectMeta
newMeta := new.Spec.Template.ObjectMeta

for k, v := range oldMeta.Annotations {
if strings.HasPrefix(k, "kubectl.kubernetes.io/") {
if newMeta.Annotations == nil {
newMeta.Annotations = make(map[string]string)
}

newMeta.Annotations[k] = v
}
}
}

// updateStatefulSet
func (r *Reconciler) updateStatefulSet(ctx context.Context, host *api.Host, register bool, opts *ReconcileOptions) error {
if util.IsContextDone(ctx) {
Expand All @@ -208,9 +230,11 @@ func (r *Reconciler) updateStatefulSet(ctx context.Context, host *api.Host, regi
}

// Helpers
newStatefulSet := host.Runtime.DesiredStatefulSet
newStatefulSet := host.Runtime.DesiredStatefulSet.DeepCopy()
curStatefulSet := host.Runtime.CurStatefulSet

r.copyKubectlAnnotationsBeforeUpdate(curStatefulSet, newStatefulSet)

r.a.V(2).M(host).S().Info(newStatefulSet.Name)
defer r.a.V(2).M(host).E().Info(newStatefulSet.Name)

Expand Down
Loading