@@ -27,59 +27,72 @@ import (
2727 v2 "github.com/percona/percona-postgresql-operator/v2/pkg/apis/pgv2.percona.com/v2"
2828)
2929
30- var errPatroniVersionCheckWait = errors .New ("waiting for pod to initialize" )
30+ func (r * PGClusterReconciler ) reconcilePatroniVersionFromCluster (ctx context.Context , cr * v2.PerconaPGCluster ) error {
31+ if cr .CompareVersion ("2.8.0" ) < 0 {
32+ return nil
33+ }
3134
32- func ( r * PGClusterReconciler ) reconcilePatroniVersion (ctx context. Context , cr * v2. PerconaPGCluster ) error {
33- if cr . Annotations = = nil {
34- cr . Annotations = make ( map [ string ] string )
35+ pods , err := r . getInstancePods (ctx , cr )
36+ if err ! = nil {
37+ return errors . Wrap ( err , "failed to get instance pods" )
3538 }
36- if cr .CompareVersion ("2.7.0" ) <= 0 {
37- if patroniVersion , ok := cr .Annotations [pNaming .AnnotationCustomPatroniVersion ]; ok {
38- err := r .handleCustomPatroniVersionAnnotation (ctx , cr , patroniVersion )
39- if err != nil {
40- return errors .Wrap (err , "handle patroni annotation" )
41- }
42- return nil
43- }
39+ if len (pods .Items ) == 0 || pods .Items [0 ].Status .Phase != corev1 .PodRunning {
40+ return errPatroniVersionCheckWait
4441 }
4542
46- // Starting from version 2.8.0, the patroni version check pod should not be executed.
47- if cr .CompareVersion ("2.8.0" ) >= 0 {
48- pods , err := r .getInstancePods (ctx , cr )
49- if err != nil {
50- return errors .Wrap (err , "failed to get instance pods" )
51- }
52- if len (pods .Items ) == 0 {
53- return errors .Wrap (err , "instance pods not available" )
54- }
43+ p := pods .Items [0 ]
44+ imageID := getImageIDFromPod (& p , naming .ContainerDatabase )
45+ pgVersion := cr .Spec .PostgresVersion
5546
56- p := pods .Items [0 ]
47+ // If patroni version is set, and neither imageID nor PG version have changed,
48+ // we don't need to check the patroni version again.
49+ if cr .Status .Patroni .Version != "" &&
50+ cr .Status .Postgres .ImageID == imageID &&
51+ cr .Status .Postgres .Version == pgVersion {
52+ return nil
53+ }
5754
58- if p .Status .Phase != corev1 .PodRunning {
59- return errPatroniVersionCheckWait
60- }
55+ patroniVersion , err := r .getPatroniVersion (ctx , & p , naming .ContainerDatabase )
56+ if err != nil {
57+ return errors .Wrap (err , "failed to get patroni version" )
58+ }
6159
62- patroniVersion , err := r .getPatroniVersion (ctx , & p , naming .ContainerDatabase )
63- if err != nil {
64- return errors .Wrap (err , "failed to get patroni version" )
65- }
60+ orig := cr .DeepCopy ()
6661
67- orig := cr .DeepCopy ()
62+ cr .Status .Patroni .Version = patroniVersion
63+ cr .Status .PatroniVersion = patroniVersion
64+ cr .Status .Postgres .Version = pgVersion
65+ cr .Status .Postgres .ImageID = imageID
6866
69- cr .Status .Patroni .Version = patroniVersion
70- cr .Status .PatroniVersion = patroniVersion
71- cr .Status .Postgres .Version = cr .Spec .PostgresVersion
72- cr .Status .Postgres .ImageID = getImageIDFromPod (& p , naming .ContainerDatabase )
67+ if err := r .Client .Status ().Patch (ctx , cr .DeepCopy (), client .MergeFrom (orig )); err != nil {
68+ return errors .Wrap (err , "failed to patch patroni version" )
69+ }
7370
74- if err := r .Client .Status ().Patch (ctx , cr .DeepCopy (), client .MergeFrom (orig )); err != nil {
75- return errors .Wrap (err , "failed to patch patroni version" )
76- }
71+ err = r .patchPatroniVersionAnnotation (ctx , cr , patroniVersion )
72+ if err != nil {
73+ return errors .Wrap (err , "failed to patch patroni version annotation" )
74+ }
75+
76+ return nil
77+ }
7778
78- err = r .patchPatroniVersionAnnotation (ctx , cr , patroniVersion )
79+ var errPatroniVersionCheckWait = errors .New ("waiting for pod to initialize" )
80+
81+ func (r * PGClusterReconciler ) reconcilePatroniVersionCheckPod (ctx context.Context , cr * v2.PerconaPGCluster ) error {
82+ // Starting from version 2.8.0, the patroni version check pod should not be executed.
83+ if cr .CompareVersion ("2.8.0" ) >= 0 {
84+ return nil
85+ }
86+
87+ if cr .Annotations == nil {
88+ cr .Annotations = make (map [string ]string )
89+ }
90+
91+ if patroniVersion , ok := cr .Annotations [pNaming .AnnotationCustomPatroniVersion ]; ok {
92+ err := r .handleCustomPatroniVersionAnnotation (ctx , cr , patroniVersion )
7993 if err != nil {
80- return errors .Wrap (err , "failed to patch patroni version annotation" )
94+ return errors .Wrap (err , "handle patroni annotation" )
8195 }
82-
8396 return nil
8497 }
8598
0 commit comments