Skip to content

Commit da41261

Browse files
Kajot-devhorsgkech
authored
K8SPG-792 Make patroni check check respect default image settings (#1167)
* K8SPG-792 Make patroni check check respect default image settings Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> * K8SPG-792 Add unit tests for default image behaviour Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> * K8SPG-792 Add comments regarding changed upstream func Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> * K8SPG-792 Use table for tests Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> * K8SPG-792 Fix gofmt warning Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> --------- Signed-off-by: Jakub Jaruszewski <jjaruszewski@man.poznan.pl> Co-authored-by: Viacheslav Sarzhan <slava.sarzhan@percona.com> Co-authored-by: George Kechagias <geo.kechagias@gmail.com>
1 parent c502fea commit da41261

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

internal/config/config.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,28 @@ func PGExporterContainerImage(cluster *v1beta1.PostgresCluster) string {
9999
return defaultFromEnv(image, "RELATED_IMAGE_PGEXPORTER")
100100
}
101101

102-
// PostgresContainerImage returns the container image to use for PostgreSQL.
103-
func PostgresContainerImage(cluster *v1beta1.PostgresCluster) string {
104-
image := cluster.Spec.Image
105-
key := "RELATED_IMAGE_POSTGRES_" + fmt.Sprint(cluster.Spec.PostgresVersion)
102+
// PostgresContainerImageString returns the container image to use for PostgreSQL (from string params).
103+
// This func copies logic from original PostgresContainerImage as is, leaving PostgresContainerImage as a wrapper for upstream compatibility
104+
func PostgresContainerImageString(image string, postgresVersion int, postGISVersion string) string {
105+
key := "RELATED_IMAGE_POSTGRES_" + fmt.Sprint(postgresVersion)
106106

107-
if version := cluster.Spec.PostGISVersion; version != "" {
108-
key += "_GIS_" + version
107+
if postGISVersion != "" {
108+
key += "_GIS_" + postGISVersion
109109
}
110110

111111
return defaultFromEnv(image, key)
112112
}
113113

114+
// PostgresContainerImage returns the container image to use for PostgreSQL.
115+
// Made as a wrapper of PostgresContainerImageString for compat reasons
116+
func PostgresContainerImage(cluster *v1beta1.PostgresCluster) string {
117+
image := cluster.Spec.Image
118+
postgresVersion := cluster.Spec.PostgresVersion
119+
postGISVersion := cluster.Spec.PostGISVersion
120+
121+
return PostgresContainerImageString(image, postgresVersion, postGISVersion)
122+
}
123+
114124
// PGONamespace returns the namespace where the PGO is running,
115125
// based on the env var from the DownwardAPI
116126
// If no env var is found, returns ""

percona/controller/pgcluster/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func (r *PGClusterReconciler) reconcilePatroniVersionCheck(ctx context.Context,
424424
Containers: []corev1.Container{
425425
{
426426
Name: pNaming.ContainerPatroniVersionCheck,
427-
Image: cr.Spec.Image,
427+
Image: cr.PostgresImage(),
428428
Command: []string{
429429
"bash",
430430
},
@@ -807,7 +807,7 @@ func (r *PGClusterReconciler) reconcileCustomExtensions(ctx context.Context, cr
807807
for i := 0; i < len(cr.Spec.InstanceSets); i++ {
808808
set := &cr.Spec.InstanceSets[i]
809809
set.InitContainers = append(set.InitContainers, extensions.ExtensionRelocatorContainer(
810-
cr, cr.Spec.Image, cr.Spec.ImagePullPolicy, cr.Spec.PostgresVersion,
810+
cr, cr.PostgresImage(), cr.Spec.ImagePullPolicy, cr.Spec.PostgresVersion,
811811
))
812812
set.InitContainers = append(set.InitContainers, extensions.ExtensionInstallerContainer(
813813
cr,

pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/apimachinery/pkg/util/intstr"
1111
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1212

13+
"github.com/percona/percona-postgresql-operator/internal/config"
1314
"github.com/percona/percona-postgresql-operator/internal/logging"
1415
"github.com/percona/percona-postgresql-operator/internal/naming"
1516
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
@@ -245,6 +246,12 @@ func (cr *PerconaPGCluster) Default() {
245246
}
246247
}
247248

249+
func (cr *PerconaPGCluster) PostgresImage() string {
250+
image := cr.Spec.Image
251+
postgresVersion := cr.Spec.PostgresVersion
252+
return config.PostgresContainerImageString(image, postgresVersion, "")
253+
}
254+
248255
func (cr *PerconaPGCluster) ToCrunchy(ctx context.Context, postgresCluster *crunchyv1beta1.PostgresCluster, scheme *runtime.Scheme) (*crunchyv1beta1.PostgresCluster, error) {
249256
log := logging.FromContext(ctx)
250257

pkg/apis/pgv2.percona.com/v2/perconapgcluster_types_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v2
22

33
import (
4+
"fmt"
5+
"os"
46
"testing"
57

68
"gotest.tools/v3/assert"
@@ -41,3 +43,61 @@ func TestPerconaPGCluster_BackupsEnabled(t *testing.T) {
4143
})
4244
}
4345
}
46+
47+
func TestPerconaPGCluster_PostgresImage(t *testing.T) {
48+
cluster := new(PerconaPGCluster)
49+
cluster.Default()
50+
51+
postgresVersion := 16
52+
testDefaultImage := fmt.Sprintf("test_default_image:%d", postgresVersion)
53+
testSpecificImage := fmt.Sprintf("test_defined_image:%d", postgresVersion)
54+
testEnv := fmt.Sprintf("RELATED_IMAGE_POSTGRES_%d", postgresVersion)
55+
56+
cluster.Spec.PostgresVersion = postgresVersion
57+
58+
tests := map[string]struct {
59+
expectedImage string
60+
setImage string
61+
envImage string
62+
}{
63+
"Spec.Image should be empty by default": {
64+
expectedImage: "",
65+
setImage: "",
66+
envImage: "",
67+
},
68+
"Spec.Image should use env variables if present": {
69+
expectedImage: testDefaultImage,
70+
setImage: "",
71+
envImage: testDefaultImage,
72+
},
73+
"Spec.Image should use defined variable": {
74+
expectedImage: testSpecificImage,
75+
setImage: testSpecificImage,
76+
envImage: testDefaultImage,
77+
},
78+
}
79+
80+
for name, tt := range tests {
81+
t.Run(name, func(t *testing.T) {
82+
83+
cluster.Spec.Image = tt.setImage
84+
85+
if tt.envImage != "" {
86+
err := os.Setenv(testEnv, tt.envImage)
87+
88+
if err != nil {
89+
t.Fatalf("Failed to set %s env variable: %v", testEnv, err)
90+
}
91+
92+
defer func() {
93+
err := os.Unsetenv(testEnv)
94+
if err != nil {
95+
t.Errorf("Failed to unset %s env variable: %v", testEnv, err)
96+
}
97+
}()
98+
}
99+
100+
assert.Equal(t, cluster.PostgresImage(), tt.expectedImage)
101+
})
102+
}
103+
}

0 commit comments

Comments
 (0)