Skip to content

Commit 47d7746

Browse files
Merge pull request #6295 from k0sproject/backport-6243-to-release-1.31
[Backport release-1.31] Add "idempotency" to image repository override
2 parents e70bd88 + a7f0862 commit 47d7746

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

pkg/apis/k0s/v1beta1/images.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ func getHostName(imageName string) string {
244244
}
245245

246246
func overrideRepository(repository string, originalImage string) string {
247+
if repository == "" {
248+
return originalImage
249+
}
250+
251+
// "Idempotency": if the image already starts with the repository prefix, don't override
252+
// This is needed sinice in some cases we run the override multiple times, e.g. when
253+
// we unmarshal the config multiple times.
254+
if strings.HasPrefix(originalImage, repository) {
255+
return originalImage
256+
}
257+
247258
if host := getHostName(originalImage); host != "" {
248259
return strings.Replace(originalImage, host, repository, 1)
249260
}

pkg/apis/k0s/v1beta1/images_test.go

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,77 @@ func TestImagesRepoOverrideInConfiguration(t *testing.T) {
9696
}
9797

9898
func TestOverrideFunction(t *testing.T) {
99-
repository := "my.registry"
100-
testCases := []struct {
101-
Input string
102-
Output string
103-
}{
104-
{
105-
Input: "repo/image",
106-
Output: "my.registry/repo/image",
107-
},
108-
{
109-
Input: "registry.com/repo/image",
110-
Output: "my.registry/repo/image",
111-
},
112-
{
113-
Input: "image",
114-
Output: "my.registry/image",
115-
},
116-
}
99+
t.Run("overrideRepository without path", func(t *testing.T) {
100+
repository := "my.registry"
101+
testCases := []struct {
102+
Input string
103+
Output string
104+
}{
105+
{
106+
Input: "repo/image",
107+
Output: "my.registry/repo/image",
108+
},
109+
{
110+
Input: "registry.com/repo/image",
111+
Output: "my.registry/repo/image",
112+
},
113+
{
114+
Input: "image",
115+
Output: "my.registry/image",
116+
},
117+
}
118+
119+
for _, tc := range testCases {
120+
assert.Equal(t, tc.Output, overrideRepository(repository, tc.Input))
121+
}
122+
})
123+
t.Run("overrideRepository with path", func(t *testing.T) {
124+
repository := "my.registry/foo"
125+
testCases := []struct {
126+
Input string
127+
Output string
128+
}{
129+
{
130+
Input: "repo/image",
131+
Output: "my.registry/foo/repo/image",
132+
},
133+
{
134+
Input: "registry.com/repo/image",
135+
Output: "my.registry/foo/repo/image",
136+
},
137+
{
138+
Input: "image",
139+
Output: "my.registry/foo/image",
140+
},
141+
}
142+
for _, tc := range testCases {
143+
assert.Equal(t, tc.Output, overrideRepository(repository, tc.Input))
144+
}
145+
})
146+
t.Run("overrideRepository with repo path and double invocation", func(t *testing.T) {
147+
repository := "my.registry/foo"
148+
testCases := []struct {
149+
Input string
150+
Output string
151+
}{
152+
{
153+
Input: "repo/image",
154+
Output: "my.registry/foo/repo/image",
155+
},
156+
{
157+
Input: "registry.com/repo/image",
158+
Output: "my.registry/foo/repo/image",
159+
},
160+
{
161+
Input: "image",
162+
Output: "my.registry/foo/image",
163+
},
164+
}
165+
for _, tc := range testCases {
166+
assert.Equal(t, tc.Output, overrideRepository(repository, overrideRepository(repository, tc.Input)))
167+
}
168+
})
117169

118-
for _, tc := range testCases {
119-
assert.Equal(t, tc.Output, overrideRepository(repository, tc.Input))
120-
}
121170
}
122171

123172
func TestImageSpec_Validate(t *testing.T) {

0 commit comments

Comments
 (0)