@@ -3,13 +3,15 @@ package k8s
33import (
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
1517type 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
7986func 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+ }
0 commit comments