@@ -25,7 +25,6 @@ import (
2525 "github.com/go-logr/logr"
2626 "github.com/iancoleman/strcase"
2727 "github.com/imdario/mergo"
28- "github.com/presslabs/controller-util/pkg/mergo/transformers"
2928 "github.com/presslabs/controller-util/pkg/syncer"
3029
3130 appsv1 "k8s.io/api/apps/v1"
@@ -367,43 +366,46 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
367366
368367// mutate set the statefulset.
369368func (s * StatefulSetSyncer ) mutate () error {
370- s .sfs .Spec .ServiceName = s .GetNameForResource (utils .StatefulSet )
371- s .sfs .Spec .Replicas = s .Spec .Replicas
372- s .sfs .Spec .Selector = metav1 .SetAsLabelSelector (s .GetSelectorLabels ())
373- s .sfs .Spec .UpdateStrategy = appsv1.StatefulSetUpdateStrategy {
374- Type : appsv1 .OnDeleteStatefulSetStrategyType ,
375- }
376-
377- s .sfs .Spec .Template .ObjectMeta .Labels = s .GetLabels ()
369+ // build lables.
370+ podLables := s .GetLabels ()
378371 for k , v := range s .Spec .PodPolicy .Labels {
379- s . sfs . Spec . Template . ObjectMeta . Labels [k ] = v
372+ podLables [k ] = v
380373 }
381- s . sfs . Spec . Template . ObjectMeta . Labels ["role" ] = string (utils .Candidate )
382- s . sfs . Spec . Template . ObjectMeta . Labels ["healthy" ] = "no"
383-
384- s . sfs . Spec . Template . Annotations = s . Spec . PodPolicy . Annotations
385- if len (s .sfs . Spec .Template . ObjectMeta . Annotations ) == 0 {
386- s . sfs . Spec .Template . ObjectMeta . Annotations = make ( map [ string ] string )
374+ podLables ["role" ] = string (utils .Follower )
375+ podLables ["healthy" ] = "no"
376+ // build annotations.
377+ podAnnotations := make ( map [ string ] string )
378+ if len (s .Spec .PodPolicy . Annotations ) > 0 {
379+ podAnnotations = s . Spec .PodPolicy . Annotations
387380 }
388381 if s .Spec .MetricsOpts .Enabled {
389- s .sfs .Spec .Template .ObjectMeta .Annotations ["prometheus.io/scrape" ] = "true"
390- s .sfs .Spec .Template .ObjectMeta .Annotations ["prometheus.io/port" ] = fmt .Sprintf ("%d" , utils .MetricsPort )
391- }
392- s .sfs .Spec .Template .ObjectMeta .Annotations ["config_rev" ] = s .cmRev
393- s .sfs .Spec .Template .ObjectMeta .Annotations ["secret_rev" ] = s .sctRev
394-
395- err := mergo .Merge (& s .sfs .Spec .Template .Spec , s .ensurePodSpec (), mergo .WithTransformers (transformers .PodSpec ))
396- if err != nil {
397- return err
382+ podAnnotations ["prometheus.io/scrape" ] = "true"
383+ podAnnotations ["prometheus.io/port" ] = fmt .Sprintf ("%d" , utils .MetricsPort )
384+ }
385+ podAnnotations ["config_rev" ] = s .cmRev
386+ podAnnotations ["secret_rev" ] = s .sctRev
387+
388+ templateSpec := appsv1.StatefulSetSpec {
389+ Replicas : s .Spec .Replicas ,
390+ ServiceName : s .GetNameForResource (utils .StatefulSet ),
391+ Selector : metav1 .SetAsLabelSelector (s .GetSelectorLabels ()),
392+ UpdateStrategy : appsv1.StatefulSetUpdateStrategy {
393+ Type : appsv1 .OnDeleteStatefulSetStrategyType ,
394+ },
395+ Template : corev1.PodTemplateSpec {
396+ ObjectMeta : metav1.ObjectMeta {
397+ Labels : podLables ,
398+ Annotations : podAnnotations ,
399+ },
400+ Spec : ensurePodSpec (s .MysqlCluster ),
401+ },
398402 }
399- s .sfs .Spec .Template .Spec .Tolerations = s .Spec .PodPolicy .Tolerations
400-
401403 if s .Spec .Persistence .Enabled {
402- if s .sfs .Spec .VolumeClaimTemplates , err = s .EnsureVolumeClaimTemplates (s .cli .Scheme ()); err != nil {
404+ var err error
405+ if templateSpec .VolumeClaimTemplates , err = s .EnsureVolumeClaimTemplates (s .cli .Scheme ()); err != nil {
403406 return err
404407 }
405408 }
406-
407409 // Set owner reference only if owner resource is not being deleted, otherwise the owner
408410 // reference will be reset in case of deleting with cascade=false.
409411 if s .Unwrap ().GetDeletionTimestamp ().IsZero () {
@@ -415,38 +417,38 @@ func (s *StatefulSetSyncer) mutate() error {
415417 // will not delete it again because has no owner reference set.
416418 return fmt .Errorf ("owner is deleted" )
417419 }
418- return nil
420+ return mergo . Merge ( & s . sfs . Spec , templateSpec , mergo . WithTransformers ( utils . StsSpec ))
419421}
420422
421423// ensurePodSpec used to ensure the podspec.
422- func ( s * StatefulSetSyncer ) ensurePodSpec ( ) corev1.PodSpec {
423- initSidecar := container .EnsureContainer (utils .ContainerInitSidecarName , s . MysqlCluster )
424- initMysql := container .EnsureContainer (utils .ContainerInitMysqlName , s . MysqlCluster )
424+ func ensurePodSpec ( c * mysqlcluster. MysqlCluster ) corev1.PodSpec {
425+ initSidecar := container .EnsureContainer (utils .ContainerInitSidecarName , c )
426+ initMysql := container .EnsureContainer (utils .ContainerInitMysqlName , c )
425427 initContainers := []corev1.Container {initSidecar , initMysql }
426428
427- mysql := container .EnsureContainer (utils .ContainerMysqlName , s . MysqlCluster )
428- xenon := container .EnsureContainer (utils .ContainerXenonName , s . MysqlCluster )
429- backup := container .EnsureContainer (utils .ContainerBackupName , s . MysqlCluster )
429+ mysql := container .EnsureContainer (utils .ContainerMysqlName , c )
430+ xenon := container .EnsureContainer (utils .ContainerXenonName , c )
431+ backup := container .EnsureContainer (utils .ContainerBackupName , c )
430432 containers := []corev1.Container {mysql , xenon , backup }
431- if s .Spec .MetricsOpts .Enabled {
432- containers = append (containers , container .EnsureContainer (utils .ContainerMetricsName , s . MysqlCluster ))
433+ if c .Spec .MetricsOpts .Enabled {
434+ containers = append (containers , container .EnsureContainer (utils .ContainerMetricsName , c ))
433435 }
434- if s .Spec .PodPolicy .SlowLogTail {
435- containers = append (containers , container .EnsureContainer (utils .ContainerSlowLogName , s . MysqlCluster ))
436+ if c .Spec .PodPolicy .SlowLogTail {
437+ containers = append (containers , container .EnsureContainer (utils .ContainerSlowLogName , c ))
436438 }
437- if s .Spec .PodPolicy .AuditLogTail {
438- containers = append (containers , container .EnsureContainer (utils .ContainerAuditLogName , s . MysqlCluster ))
439+ if c .Spec .PodPolicy .AuditLogTail {
440+ containers = append (containers , container .EnsureContainer (utils .ContainerAuditLogName , c ))
439441 }
440442
441443 return corev1.PodSpec {
442444 InitContainers : initContainers ,
443445 Containers : containers ,
444- Volumes : s .EnsureVolumes (),
445- SchedulerName : s .Spec .PodPolicy .SchedulerName ,
446- ServiceAccountName : s .GetNameForResource (utils .ServiceAccount ),
447- Affinity : s .Spec .PodPolicy .Affinity ,
448- PriorityClassName : s .Spec .PodPolicy .PriorityClassName ,
449- Tolerations : s .Spec .PodPolicy .Tolerations ,
446+ Volumes : c .EnsureVolumes (),
447+ SchedulerName : c .Spec .PodPolicy .SchedulerName ,
448+ ServiceAccountName : c .GetNameForResource (utils .ServiceAccount ),
449+ Affinity : c .Spec .PodPolicy .Affinity ,
450+ PriorityClassName : c .Spec .PodPolicy .PriorityClassName ,
451+ Tolerations : c .Spec .PodPolicy .Tolerations ,
450452 }
451453}
452454
@@ -589,6 +591,9 @@ func (s *StatefulSetSyncer) backupIsRunning(ctx context.Context) (bool, error) {
589591
590592// Updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden.
591593func (s * StatefulSetSyncer ) sfsUpdated (existing * appsv1.StatefulSet ) bool {
594+ if s .sfs .Status .UpdateRevision != s .sfs .Status .CurrentRevision {
595+ return true
596+ }
592597 var resizeVolume = false
593598 // TODO: this is a temporary workaround until we figure out a better way to do this.
594599 if len (existing .Spec .VolumeClaimTemplates ) > 0 && len (s .sfs .Spec .VolumeClaimTemplates ) > 0 {
0 commit comments