Skip to content

Commit 31c496e

Browse files
horsvalmiranogueira
authored andcommitted
K8SPS-598 refine default init image retagging to honor CR version for Percona images (#1157)
1 parent 6e30576 commit 31c496e

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

pkg/k8s/init_image.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package k8s
33
import (
44
"context"
55
"os"
6+
"strings"
67

78
"github.com/pkg/errors"
89
corev1 "k8s.io/api/core/v1"
910
"k8s.io/apimachinery/pkg/types"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112

1213
apiv1 "github.com/percona/percona-server-mysql-operator/api/v1"
14+
operatorversion "github.com/percona/percona-server-mysql-operator/pkg/version"
1315
)
1416

1517
type ComponentWithInit interface {
@@ -73,7 +75,12 @@ func InitImage(ctx context.Context, cl client.Reader, cr *apiv1.PerconaServerMyS
7375
if cr.CompareVersion("0.12.0") < 0 && cr.Spec.InitImage == "" { //nolint:staticcheck
7476
return cr.Spec.InitImage, nil //nolint:staticcheck
7577
}
76-
return OperatorImage(ctx, cl)
78+
imageName, err := OperatorImage(ctx, cl)
79+
if err != nil {
80+
return "", err
81+
}
82+
83+
return adjustInitImageWithCRVersion(cr, imageName), nil
7784
}
7885

7986
func OperatorImage(ctx context.Context, cl client.Reader) (string, error) {
@@ -108,3 +115,50 @@ func operatorPod(ctx context.Context, cl client.Reader) (*corev1.Pod, error) {
108115

109116
return pod, nil
110117
}
118+
119+
func adjustInitImageWithCRVersion(cr *apiv1.PerconaServerMySQL, imageName string) string {
120+
if cr == nil {
121+
return imageName
122+
}
123+
124+
if cr.Spec.CRVersion == "" || cr.CompareVersion(operatorversion.Version()) == 0 {
125+
return imageName
126+
}
127+
128+
imageName = stripDigest(imageName)
129+
if !hasPerconaNamespace(imageName) {
130+
return imageName
131+
}
132+
133+
imageBase := stripTag(imageName)
134+
if imageBase == "" {
135+
return imageName
136+
}
137+
138+
return imageBase + ":" + cr.Spec.CRVersion
139+
}
140+
141+
func stripDigest(image string) string {
142+
if idx := strings.Index(image, "@"); idx != -1 {
143+
return image[:idx]
144+
}
145+
return image
146+
}
147+
148+
func stripTag(image string) string {
149+
lastSlash := strings.LastIndex(image, "/")
150+
lastColon := strings.LastIndex(image, ":")
151+
if lastColon > lastSlash {
152+
return image[:lastColon]
153+
}
154+
return image
155+
}
156+
157+
func hasPerconaNamespace(image string) bool {
158+
if image == "" {
159+
return false
160+
}
161+
162+
normalized := "/" + strings.Trim(image, "/") + "/"
163+
return strings.Contains(normalized, "/percona/")
164+
}

pkg/k8s/init_image_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"k8s.io/utils/ptr"
1010

1111
apiv1 "github.com/percona/percona-server-mysql-operator/api/v1"
12+
operatorversion "github.com/percona/percona-server-mysql-operator/pkg/version"
1213
)
1314

1415
func TestInitContainer(t *testing.T) {
@@ -154,3 +155,49 @@ func TestInitContainer(t *testing.T) {
154155
})
155156
}
156157
}
158+
159+
func TestAdjustInitImageWithCRVersion(t *testing.T) {
160+
currentVersion := operatorversion.Version()
161+
162+
tests := map[string]struct {
163+
crVersion string
164+
image string
165+
expected string
166+
}{
167+
"percona image retagged": {
168+
crVersion: "9.9.9",
169+
image: "percona/percona-server-mysql-operator:1.0.0",
170+
expected: "percona/percona-server-mysql-operator:9.9.9",
171+
},
172+
"percona image with registry port retagged": {
173+
crVersion: "2.2.2",
174+
image: "registry.example.com:5000/percona/percona-server-mysql-operator:1.0.0",
175+
expected: "registry.example.com:5000/percona/percona-server-mysql-operator:2.2.2",
176+
},
177+
"percona image with digest retagged": {
178+
crVersion: "3.3.3",
179+
image: "percona/percona-server-mysql-operator@sha256:abcdef",
180+
expected: "percona/percona-server-mysql-operator:3.3.3",
181+
},
182+
"non percona image unchanged": {
183+
crVersion: "4.4.4",
184+
image: "custom/repo:1.0.0",
185+
expected: "custom/repo:1.0.0",
186+
},
187+
"matching operator version unchanged": {
188+
crVersion: currentVersion,
189+
image: "percona/percona-server-mysql-operator:1.0.0",
190+
expected: "percona/percona-server-mysql-operator:1.0.0",
191+
},
192+
}
193+
194+
for name, tt := range tests {
195+
t.Run(name, func(t *testing.T) {
196+
cr := &apiv1.PerconaServerMySQL{}
197+
cr.Spec.CRVersion = tt.crVersion
198+
199+
result := adjustInitImageWithCRVersion(cr, tt.image)
200+
assert.Equal(t, tt.expected, result)
201+
})
202+
}
203+
}

0 commit comments

Comments
 (0)