From e13b375f60d99880393d0377f951a33648a315f4 Mon Sep 17 00:00:00 2001 From: Linas Naginionis Date: Tue, 2 Mar 2021 12:05:50 +0200 Subject: [PATCH 1/3] Make sure rollout restarts are performed for stateful sets when update annotation is set. --- pkg/controller/controller.go | 12 ++++++++++++ pkg/k8sops/annotations/annotations.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 11809831..73edc13f 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -27,6 +27,7 @@ import ( "reflect" "sort" "sync" + "time" "github.com/m3db/m3db-operator/pkg/apis/m3dboperator" myspec "github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1" @@ -69,6 +70,7 @@ const ( controllerName = "m3db-controller" clusterWorkQueueName = "m3dbcluster-work-queue" podWorkQueueName = "pods-work-queue" + rolloutTimeFormat = "2006-01-02T150405" ) var ( @@ -1071,6 +1073,7 @@ func updatedStatefulSet( // annotation. This ensures that users can always set the update annotation and then // wait for it to be removed to know if the operator has processed a StatefulSet. if !update { + updateSpecTemplate(actual) delete(actual.Annotations, annotations.Update) return actual, true, nil } @@ -1083,9 +1086,18 @@ func updatedStatefulSet( expected.Status = actual.DeepCopy().Status copyAnnotations(expected, actual) + updateSpecTemplate(expected) return expected, true, nil } +// updates spec template so that rollout update is always performed. +func updateSpecTemplate(sts *appsv1.StatefulSet) { + if sts.Spec.Template.Annotations == nil { + sts.Spec.Template.Annotations = map[string]string{} + } + sts.Spec.Template.Annotations[annotations.Rollout] = time.Now().Format(rolloutTimeFormat) +} + func copyAnnotations(expected, actual *appsv1.StatefulSet) { // It's okay for users to add annotations to a StatefulSet after it has been created so // we'll want to copy over any that exist on the actual StatefulSet but not the expected diff --git a/pkg/k8sops/annotations/annotations.go b/pkg/k8sops/annotations/annotations.go index b625f64d..bb680d23 100644 --- a/pkg/k8sops/annotations/annotations.go +++ b/pkg/k8sops/annotations/annotations.go @@ -37,6 +37,8 @@ const ( // Update is the annotation used by the operator to determine if a StatefulSet is // allowed to be updated. Update = "operator.m3db.io/update" + // Rollout is the annotation used by the operator to force rollout update of a StatefulSet. + Rollout = "operator.m3db.io/rollout" // EnabledVal is the value that indicates an annotation is enabled. EnabledVal = "enabled" From 47a548b8850bc3007cf30e8d12f6ea9d284dd3f1 Mon Sep 17 00:00:00 2001 From: Linas Naginionis Date: Wed, 3 Mar 2021 10:17:01 +0200 Subject: [PATCH 2/3] Use time now from k8s api. --- pkg/controller/controller.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 73edc13f..8247980f 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -27,7 +27,6 @@ import ( "reflect" "sort" "sync" - "time" "github.com/m3db/m3db-operator/pkg/apis/m3dboperator" myspec "github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1" @@ -70,7 +69,6 @@ const ( controllerName = "m3db-controller" clusterWorkQueueName = "m3dbcluster-work-queue" podWorkQueueName = "pods-work-queue" - rolloutTimeFormat = "2006-01-02T150405" ) var ( @@ -1095,7 +1093,8 @@ func updateSpecTemplate(sts *appsv1.StatefulSet) { if sts.Spec.Template.Annotations == nil { sts.Spec.Template.Annotations = map[string]string{} } - sts.Spec.Template.Annotations[annotations.Rollout] = time.Now().Format(rolloutTimeFormat) + + sts.Spec.Template.Annotations[annotations.Rollout] = metav1.Now().String() } func copyAnnotations(expected, actual *appsv1.StatefulSet) { From 1042a2eccf2020c3880e972e440e40e81090d40b Mon Sep 17 00:00:00 2001 From: Linas Naginionis Date: Wed, 3 Mar 2021 10:42:25 +0200 Subject: [PATCH 3/3] Use time now and MarshalQueryParameter from k8s api. --- pkg/controller/controller.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 8247980f..d539893c 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -1094,7 +1094,8 @@ func updateSpecTemplate(sts *appsv1.StatefulSet) { sts.Spec.Template.Annotations = map[string]string{} } - sts.Spec.Template.Annotations[annotations.Rollout] = metav1.Now().String() + now, _ := metav1.Now().MarshalQueryParameter() + sts.Spec.Template.Annotations[annotations.Rollout] = now } func copyAnnotations(expected, actual *appsv1.StatefulSet) {