Skip to content

Commit b5165f5

Browse files
K8SPG-933: standby cannot be created (#1410)
Signed-off-by: Mayank Shah <mayank.shah@percona.com>
1 parent e3561ef commit b5165f5

4 files changed

Lines changed: 69 additions & 43 deletions

File tree

percona/controller/pgcluster/controller.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
265265
return reconcile.Result{}, errors.Wrap(err, "ensure finalizers")
266266
}
267267

268-
if err := r.reconcilePatroniVersion(ctx, cr); err != nil {
268+
if err := r.reconcilePatroniVersionCheckPod(ctx, cr); err != nil {
269269
if errors.Is(err, errPatroniVersionCheckWait) {
270270
return reconcile.Result{
271271
RequeueAfter: 5 * time.Second,
272272
}, nil
273273
}
274-
return reconcile.Result{}, errors.Wrap(err, "check patroni version")
274+
return reconcile.Result{}, errors.Wrap(err, "check patroni version pod")
275275
}
276276

277277
if err := r.reconcileTLS(ctx, cr); err != nil {
@@ -356,6 +356,15 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
356356
return ctrl.Result{}, errors.Wrap(err, "update status")
357357
}
358358

359+
if err := r.reconcilePatroniVersionFromCluster(ctx, cr); err != nil {
360+
if errors.Is(err, errPatroniVersionCheckWait) {
361+
return reconcile.Result{
362+
RequeueAfter: 5 * time.Second,
363+
}, nil
364+
}
365+
return reconcile.Result{}, errors.Wrap(err, "check patroni version from instance pods")
366+
}
367+
359368
return ctrl.Result{}, nil
360369
}
361370

percona/controller/pgcluster/patroniversion.go

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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

percona/controller/pgcluster/patroniversion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var _ = Describe("patroni version check", Ordered, func() {
114114

115115
It("should create patroni version check pod and return errPatroniVersionCheckWait", func() {
116116
reconcilerInstance := reconciler(cr2)
117-
err := reconcilerInstance.reconcilePatroniVersion(ctx, cr2)
117+
err := reconcilerInstance.reconcilePatroniVersionCheckPod(ctx, cr2)
118118
Expect(err).To(HaveOccurred())
119119
Expect(err.Error()).To(ContainSubstring("waiting for pod to initialize"))
120120
})

pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ func (cr *PostgresCluster) CompareVersion(ver string) int {
791791
func (cr *PostgresCluster) IsPatroniVer4() (bool, error) {
792792
patroniVerStr, ok := cr.Annotations[pNaming.ToCrunchyAnnotation(pNaming.AnnotationPatroniVersion)]
793793
if !ok {
794+
// Assume Patroni v4 for CR versions greater than 2.7.0
795+
if cr.CompareVersion("2.7.0") >= 0 {
796+
return true, nil
797+
}
794798
return false, errors.New("patroni version annotation was not found")
795799
}
796800
patroniVer, err := gover.NewVersion(patroniVerStr)

0 commit comments

Comments
 (0)