@@ -25,7 +25,6 @@ import (
25
25
"github.com/go-logr/logr"
26
26
"github.com/iancoleman/strcase"
27
27
"github.com/imdario/mergo"
28
- "github.com/presslabs/controller-util/pkg/mergo/transformers"
29
28
"github.com/presslabs/controller-util/pkg/syncer"
30
29
31
30
appsv1 "k8s.io/api/apps/v1"
@@ -367,43 +366,46 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
367
366
368
367
// mutate set the statefulset.
369
368
func (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 ()
378
371
for k , v := range s .Spec .PodPolicy .Labels {
379
- s . sfs . Spec . Template . ObjectMeta . Labels [k ] = v
372
+ podLables [k ] = v
380
373
}
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
387
380
}
388
381
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
+ },
398
402
}
399
- s .sfs .Spec .Template .Spec .Tolerations = s .Spec .PodPolicy .Tolerations
400
-
401
403
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 {
403
406
return err
404
407
}
405
408
}
406
-
407
409
// Set owner reference only if owner resource is not being deleted, otherwise the owner
408
410
// reference will be reset in case of deleting with cascade=false.
409
411
if s .Unwrap ().GetDeletionTimestamp ().IsZero () {
@@ -415,38 +417,38 @@ func (s *StatefulSetSyncer) mutate() error {
415
417
// will not delete it again because has no owner reference set.
416
418
return fmt .Errorf ("owner is deleted" )
417
419
}
418
- return nil
420
+ return mergo . Merge ( & s . sfs . Spec , templateSpec , mergo . WithTransformers ( utils . StsSpec ))
419
421
}
420
422
421
423
// 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 )
425
427
initContainers := []corev1.Container {initSidecar , initMysql }
426
428
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 )
430
432
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 ))
433
435
}
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 ))
436
438
}
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 ))
439
441
}
440
442
441
443
return corev1.PodSpec {
442
444
InitContainers : initContainers ,
443
445
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 ,
450
452
}
451
453
}
452
454
@@ -589,6 +591,9 @@ func (s *StatefulSetSyncer) backupIsRunning(ctx context.Context) (bool, error) {
589
591
590
592
// Updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden.
591
593
func (s * StatefulSetSyncer ) sfsUpdated (existing * appsv1.StatefulSet ) bool {
594
+ if s .sfs .Status .UpdateRevision != s .sfs .Status .CurrentRevision {
595
+ return true
596
+ }
592
597
var resizeVolume = false
593
598
// TODO: this is a temporary workaround until we figure out a better way to do this.
594
599
if len (existing .Spec .VolumeClaimTemplates ) > 0 && len (s .sfs .Spec .VolumeClaimTemplates ) > 0 {
0 commit comments