Skip to content

Commit e070313

Browse files
committed
small improvements
1 parent ed540fd commit e070313

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

internal/controller/postgrescluster/instance.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313
"time"
1414

15-
gover "github.com/hashicorp/go-version"
1615
"github.com/pkg/errors"
1716
"go.opentelemetry.io/otel/attribute"
1817
"go.opentelemetry.io/otel/trace"
@@ -38,7 +37,6 @@ import (
3837
"github.com/percona/percona-postgresql-operator/internal/pgbackrest"
3938
"github.com/percona/percona-postgresql-operator/internal/pki"
4039
"github.com/percona/percona-postgresql-operator/internal/postgres"
41-
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
4240
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4341
)
4442

@@ -811,15 +809,10 @@ func (r *Reconciler) rolloutInstance(
811809
ctx, span = r.Tracer.Start(ctx, "patroni-change-primary")
812810
defer span.End()
813811

814-
patroniVerStr, ok := cluster.Annotations[pNaming.ToCrunchyAnnotation(pNaming.AnnotationPatroniVersion)]
815-
if !ok {
816-
return errors.New("patroni version annotation was not found")
817-
}
818-
patroniVer, err := gover.NewVersion(patroniVerStr)
812+
patroniVer4, err := cluster.IsPatroniVer4()
819813
if err != nil {
820-
return errors.Wrap(err, "failed to get patroni ver")
814+
return errors.Wrap(err, "failed to check if patroni v4 is used")
821815
}
822-
patroniVer4 := patroniVer.Compare(gover.Must(gover.NewVersion("4.0.0"))) >= 0
823816

824817
success, err := patroni.Executor(exec).ChangePrimaryAndWait(ctx, pod.Name, "", patroniVer4)
825818
if err = errors.WithStack(err); err == nil && !success {

internal/controller/postgrescluster/patroni.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"time"
1212

13-
gover "github.com/hashicorp/go-version"
1413
"github.com/pkg/errors"
1514
corev1 "k8s.io/api/core/v1"
1615
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -23,7 +22,6 @@ import (
2322
"github.com/percona/percona-postgresql-operator/internal/patroni"
2423
"github.com/percona/percona-postgresql-operator/internal/pki"
2524
"github.com/percona/percona-postgresql-operator/internal/postgres"
26-
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
2725
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2826
)
2927

@@ -96,15 +94,10 @@ func (r *Reconciler) handlePatroniRestarts(
9694
return r.PodExec(ctx, pod.Namespace, pod.Name, container, stdin, stdout, stderr, command...)
9795
})
9896

99-
patroniVerStr, ok := cluster.Annotations[pNaming.ToCrunchyAnnotation(pNaming.AnnotationPatroniVersion)]
100-
if !ok {
101-
return errors.New("patroni version annotation was not found")
102-
}
103-
patroniVer, err := gover.NewVersion(patroniVerStr)
97+
patroniVer4, err := cluster.IsPatroniVer4()
10498
if err != nil {
105-
return errors.Wrap(err, "failed to get patroni ver")
99+
return errors.Wrap(err, "failed to check if patroni v4 is used")
106100
}
107-
patroniVer4 := patroniVer.Compare(gover.Must(gover.NewVersion("4.0.0"))) >= 0
108101

109102
// K8SPG-648: patroni v4.0.0 deprecated "master" role.
110103
// We should use "primary" instead

internal/patroni/api.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ func (exec Executor) ChangePrimaryAndWait(
4545

4646
// K8SPG-648: patroni v4.0.0 deprecated "master" role.
4747
// We should use "primary" instead
48-
cmd := []string{"patronictl", "switchover", "--scheduled=now", "--force", "--primary=" + current, "--candidate=" + next}
49-
if !patroniVer4 {
50-
cmd = []string{"patronictl", "switchover", "--scheduled=now", "--force", "--master=" + current, "--candidate=" + next}
48+
cmd := []string{"patronictl", "switchover", "--scheduled=now", "--force", "--candidate=" + next}
49+
if patroniVer4 {
50+
cmd = append(cmd, "--primary="+current)
51+
} else {
52+
cmd = append(cmd, "--master="+current)
5153
}
54+
5255
err := exec(ctx, nil, &stdout, &stderr, cmd...)
5356

5457
log := logging.FromContext(ctx)

internal/patroni/config.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
package patroni
66

77
import (
8-
"errors"
98
"fmt"
109
"path"
1110
"strings"
1211

13-
gover "github.com/hashicorp/go-version"
1412
corev1 "k8s.io/api/core/v1"
1513
"sigs.k8s.io/yaml"
1614

1715
"github.com/percona/percona-postgresql-operator/internal/config"
1816
"github.com/percona/percona-postgresql-operator/internal/naming"
1917
"github.com/percona/percona-postgresql-operator/internal/postgres"
20-
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
2118
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2219
)
2320

@@ -573,13 +570,10 @@ func instanceYAML(
573570
}
574571
// K8SPG-648: patroni v4.0.0 deprecated "no_master".
575572
// We should use "no_leader" instead
576-
577-
patroniVerStr, ok := cluster.Annotations[pNaming.ToCrunchyAnnotation(pNaming.AnnotationPatroniVersion)]
578-
if !ok {
579-
return "", errors.New("patroni version annotation was not found")
573+
patroniVer4, err := cluster.IsPatroniVer4()
574+
if err != nil {
575+
return "", fmt.Errorf("failed to check if patroni v4 is used: %w", err)
580576
}
581-
patroniVer := gover.Must(gover.NewVersion(patroniVerStr))
582-
patroniVer4 := patroniVer.Compare(gover.Must(gover.NewVersion("4.0.0"))) >= 0
583577
if !patroniVer4 {
584578
postgresql[pgBackRestCreateReplicaMethod] = map[string]any{
585579
"command": strings.Join(quoted, " "),

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"fmt"
99

1010
gover "github.com/hashicorp/go-version"
11+
"github.com/pkg/errors"
1112
corev1 "k8s.io/api/core/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/apimachinery/pkg/util/intstr"
15+
16+
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
1417
)
1518

1619
// PostgresClusterSpec defines the desired state of PostgresCluster
@@ -747,3 +750,16 @@ func (cr *PostgresCluster) CompareVersion(ver string) int {
747750
}
748751
return crVersion.Compare(gover.Must(gover.NewVersion(ver)))
749752
}
753+
754+
// K8SPG-692
755+
func (cr *PostgresCluster) IsPatroniVer4() (bool, error) {
756+
patroniVerStr, ok := cr.Annotations[pNaming.ToCrunchyAnnotation(pNaming.AnnotationPatroniVersion)]
757+
if !ok {
758+
return false, errors.New("patroni version annotation was not found")
759+
}
760+
patroniVer, err := gover.NewVersion(patroniVerStr)
761+
if err != nil {
762+
return false, errors.Wrap(err, "failed to get patroni ver")
763+
}
764+
return patroniVer.Compare(gover.Must(gover.NewVersion("4.0.0"))) >= 0, nil
765+
}

0 commit comments

Comments
 (0)