@@ -374,8 +374,6 @@ func (r *PGClusterReconciler) reconcilePatroniVersionCheck(ctx context.Context,
374374 }
375375
376376 if patroniVersion , ok := cr .Annotations [pNaming .AnnotationCustomPatroniVersion ]; ok {
377- cr .Annotations [pNaming .AnnotationPatroniVersion ] = patroniVersion
378-
379377 patroniVersionUpdateFunc := func () error {
380378 cluster := & v2.PerconaPGCluster {}
381379 if err := r .Client .Get (ctx , types.NamespacedName {
@@ -388,10 +386,17 @@ func (r *PGClusterReconciler) reconcilePatroniVersionCheck(ctx context.Context,
388386 orig := cluster .DeepCopy ()
389387
390388 cluster .Status .Patroni .Version = patroniVersion
389+ cluster .Status .PatroniVersion = patroniVersion
391390
392391 if err := r .Client .Status ().Patch (ctx , cluster .DeepCopy (), client .MergeFrom (orig )); err != nil {
393392 return errors .Wrap (err , "failed to patch patroni version" )
394393 }
394+
395+ err := r .patchPatroniVersionAnnotation (ctx , cr , patroniVersion )
396+ if err != nil {
397+ return errors .Wrap (err , "failed to patch patroni version annotation" )
398+ }
399+
395400 return nil
396401 }
397402
@@ -403,40 +408,29 @@ func (r *PGClusterReconciler) reconcilePatroniVersionCheck(ctx context.Context,
403408 return nil
404409 }
405410
406- getImageIDFromPod := func (pod * corev1.Pod , containerName string ) string {
407- idx := slices .IndexFunc (pod .Status .ContainerStatuses , func (s corev1.ContainerStatus ) bool {
408- return s .Name == containerName
409- })
410- if idx == - 1 {
411- return ""
412- }
413- return pod .Status .ContainerStatuses [idx ].ImageID
414- }
415-
416- pods := new (corev1.PodList )
417- instances , err := naming .AsSelector (naming .ClusterInstances (cr .Name ))
411+ imageIDs , err := r .instanceImageIDs (ctx , cr )
418412 if err != nil {
419- return err
420- }
421- if err = r .Client .List (ctx , pods , client .InNamespace (cr .Namespace ), client.MatchingLabelsSelector {Selector : instances }); err != nil {
422- return err
423- }
424-
425- // Collecting all image IDs from instance pods. Under normal conditions, this slice will contain a single image ID, as all pods typically use the same image.
426- // During an image update, it may contain multiple different image IDs as the update progresses.
427- imageIDs := []string {}
428- for _ , pod := range pods .Items {
429- imageID := getImageIDFromPod (& pod , naming .ContainerDatabase )
430- if imageID != "" && ! slices .Contains (imageIDs , imageID ) {
431- imageIDs = append (imageIDs , imageID )
432- }
413+ return errors .Wrap (err , "get image IDs" )
433414 }
434415
435416 // If the imageIDs slice contains the imageID from the status, we skip checking the Patroni version.
436417 // This ensures that the Patroni version is only checked after all pods have been updated.
437- if (len (imageIDs ) == 0 || slices .Contains (imageIDs , cr .Status .Postgres .ImageID )) && cr .Status .Patroni .Version != "" {
438- cr .Annotations [pNaming .AnnotationPatroniVersion ] = cr .Status .Patroni .Version
439- return nil
418+ if cr .CompareVersion ("2.8.0" ) >= 0 {
419+ if (len (imageIDs ) == 0 || slices .Contains (imageIDs , cr .Status .Postgres .ImageID )) && cr .Status .Patroni .Version != "" {
420+ err = r .patchPatroniVersionAnnotation (ctx , cr , cr .Status .Patroni .Version )
421+ if err != nil {
422+ return errors .Wrap (err , "failed to patch patroni version annotation" )
423+ }
424+ return nil
425+ }
426+ } else {
427+ if (len (imageIDs ) == 0 || slices .Contains (imageIDs , cr .Status .Postgres .ImageID )) && cr .Status .PatroniVersion != "" {
428+ err = r .patchPatroniVersionAnnotation (ctx , cr , cr .Status .PatroniVersion )
429+ if err != nil {
430+ return errors .Wrap (err , "failed to patch patroni version annotation" )
431+ }
432+ return nil
433+ }
440434 }
441435
442436 meta := metav1.ObjectMeta {
@@ -535,21 +529,68 @@ func (r *PGClusterReconciler) reconcilePatroniVersionCheck(ctx context.Context,
535529 orig := cr .DeepCopy ()
536530
537531 cr .Status .Patroni .Version = patroniVersion
532+ cr .Status .PatroniVersion = patroniVersion
538533 cr .Status .Postgres .Version = cr .Spec .PostgresVersion
539534 cr .Status .Postgres .ImageID = getImageIDFromPod (p , pNaming .ContainerPatroniVersionCheck )
540535
541536 if err := r .Client .Status ().Patch (ctx , cr .DeepCopy (), client .MergeFrom (orig )); err != nil {
542537 return errors .Wrap (err , "failed to patch patroni version" )
543538 }
544539
540+ err = r .patchPatroniVersionAnnotation (ctx , cr , patroniVersion )
541+ if err != nil {
542+ return errors .Wrap (err , "failed to patch patroni version annotation" )
543+ }
544+
545545 if err := r .Client .Delete (ctx , p ); err != nil {
546546 return errors .Wrap (err , "failed to delete patroni version check pod" )
547547 }
548- cr .Annotations [pNaming .AnnotationPatroniVersion ] = patroniVersion
549548
550549 return nil
551550}
552551
552+ func (r * PGClusterReconciler ) patchPatroniVersionAnnotation (ctx context.Context , cr * v2.PerconaPGCluster , patroniVersion string ) error {
553+ orig := cr .DeepCopy ()
554+ cr .Annotations [pNaming .AnnotationPatroniVersion ] = patroniVersion
555+ if err := r .Client .Patch (ctx , cr .DeepCopy (), client .MergeFrom (orig )); err != nil {
556+ return errors .Wrap (err , "failed to patch the pg cluster" )
557+ }
558+ return nil
559+ }
560+
561+ func (r * PGClusterReconciler ) instanceImageIDs (ctx context.Context , cr * v2.PerconaPGCluster ) ([]string , error ) {
562+ pods := new (corev1.PodList )
563+ instances , err := naming .AsSelector (naming .ClusterInstances (cr .Name ))
564+ if err != nil {
565+ return nil , errors .Wrap (err , "failed to create a selector for instance pods" )
566+ }
567+ if err = r .Client .List (ctx , pods , client .InNamespace (cr .Namespace ), client.MatchingLabelsSelector {Selector : instances }); err != nil {
568+ return nil , errors .Wrap (err , "failed to list instances" )
569+ }
570+
571+ // Collecting all image IDs from instance pods. Under normal conditions, this slice will contain a single image ID, as all pods typically use the same image.
572+ // During an image update, it may contain multiple different image IDs as the update progresses.
573+ var imageIDs []string
574+ for _ , pod := range pods .Items {
575+ imageID := getImageIDFromPod (& pod , naming .ContainerDatabase )
576+ if imageID != "" && ! slices .Contains (imageIDs , imageID ) {
577+ imageIDs = append (imageIDs , imageID )
578+ }
579+ }
580+
581+ return imageIDs , nil
582+ }
583+
584+ func getImageIDFromPod (pod * corev1.Pod , containerName string ) string {
585+ idx := slices .IndexFunc (pod .Status .ContainerStatuses , func (s corev1.ContainerStatus ) bool {
586+ return s .Name == containerName
587+ })
588+ if idx == - 1 {
589+ return ""
590+ }
591+ return pod .Status .ContainerStatuses [idx ].ImageID
592+ }
593+
553594func (r * PGClusterReconciler ) reconcileTLS (ctx context.Context , cr * v2.PerconaPGCluster ) error {
554595 if err := r .validateTLS (ctx , cr ); err != nil {
555596 return errors .Wrap (err , "validate TLS" )
0 commit comments