Skip to content

Commit 0585f72

Browse files
authored
do not add tag to path when mirroring (#12)
1 parent 41802d0 commit 0585f72

File tree

5 files changed

+79
-36
lines changed

5 files changed

+79
-36
lines changed

mirror/defs.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ load("@rules_gitops//gitops:provider.bzl", "GitopsPushInfo")
44
load("@com_adobe_rules_gitops//skylib:push.bzl", "K8sPushInfo")
55
load("@rules_gitops//skylib:runfile.bzl", "get_runfile_path")
66

7+
def _replace_colon_except_last_segment(input_string):
8+
segments = input_string.split("/")
9+
for i in range(len(segments) - 1):
10+
segments[i] = segments[i].replace(":", "")
11+
output_string = "/".join(segments)
12+
return output_string
13+
714
def _mirror_image_impl(ctx):
815
digest = ctx.attr.digest
916
src_image = ctx.attr.src_image
@@ -25,17 +32,11 @@ def _mirror_image_impl(ctx):
2532
if ctx.attr.dst:
2633
dst = ctx.expand_make_variables("dst", ctx.attr.dst, {})
2734
dst = dst.split("@", 1)[0]
28-
v = dst.split(":", 1)
29-
dst_without_hash = v[0]
30-
if len(v) > 1:
31-
fail("dst should not include a tag, only a repository")
35+
dst_without_hash = _replace_colon_except_last_segment(dst)
3236
else:
3337
if not ctx.attr.dst_prefix:
3438
fail("either dst or dst_prefix must be defined in mirror_image")
35-
v = s.split(":", 1)
36-
src_repository = v[0]
37-
if len(v) > 1:
38-
src_repository = src_repository + "/" + v[1]
39+
src_repository = _replace_colon_except_last_segment(s)
3940
dst_prefix = ctx.expand_make_variables("dst_prefix", ctx.attr.dst_prefix, {})
4041
dst_without_hash = dst_prefix.strip("/") + "/" + src_repository
4142

mirror/tests/BUILD.bazel

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ assert_contains(
3232
oci_push(
3333
name = "push_image",
3434
image = ":image",
35+
remote_tags = ["tag1"],
3536
repository = "localhost:1338/image",
3637
visibility = ["//visibility:public"],
3738
)
@@ -45,6 +46,15 @@ mirror_image(
4546
visibility = ["//visibility:public"],
4647
)
4748

49+
mirror_image(
50+
name = "image_mirror_with_tag",
51+
digest = "sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
52+
dst_prefix = "localhost:1338/mirror2",
53+
src_image = "localhost:1338/image:tag1",
54+
tags = ["manual"], # test should be tagged manual since it requires a running registry
55+
visibility = ["//visibility:public"],
56+
)
57+
4858
kustomize(
4959
name = "kustomize1",
5060
images = [
@@ -105,13 +115,13 @@ k8s_deploy_rg(
105115
assert_contains(
106116
name = "adobe_gitops_deploy_has_mirrored_image",
107117
actual = ":adobe_gitops_deploy",
108-
expected = "image: localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
118+
expected = "image: localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
109119
)
110120

111121
assert_contains(
112122
name = "fasterci_gitops_deploy_has_mirrored_image",
113123
actual = ":fasterci_gitops_deploy",
114-
expected = "image: localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
124+
expected = "image: localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
115125
)
116126

117127
exports_files([
@@ -159,7 +169,7 @@ sh_test(
159169
name = "adobe_gitops_apply_test",
160170
srcs = ["gitops_setup_test.sh"],
161171
args = [
162-
"$(location :adobe_gitops_deploy.apply) localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
172+
"$(location :adobe_gitops_deploy.apply) localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
163173
],
164174
data = [
165175
":adobe_gitops_deploy.apply",
@@ -195,7 +205,7 @@ sh_test(
195205
name = "fasterci_gitops_setup_test",
196206
srcs = ["gitops_setup_test.sh"],
197207
args = [
198-
"$(location :fasterci_gitops.setup) localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
208+
"$(location :fasterci_gitops.setup) localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
199209
],
200210
data = [
201211
":fasterci_gitops.setup",
@@ -220,7 +230,7 @@ sh_test(
220230
name = "fasterci_gitops_apply_test",
221231
srcs = ["gitops_setup_test.sh"],
222232
args = [
223-
"$(location :fasterci_gitops_deploy.apply) localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
233+
"$(location :fasterci_gitops_deploy.apply) localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
224234
],
225235
data = [
226236
":fasterci_gitops_deploy.apply",
@@ -237,3 +247,47 @@ sh_test(
237247
},
238248
tags = ["exclusive"], # this test starts a registry on fixed port 1338
239249
)
250+
251+
sh_test(
252+
name = "image_mirror_with_tag_test",
253+
srcs = ["gitops_setup_test.sh"],
254+
args = [
255+
"$(location :image_mirror_with_tag) localhost:1338/mirror2/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc",
256+
],
257+
data = [
258+
":image_mirror_with_tag",
259+
":kubectl",
260+
":push_image",
261+
"@com_github_google_go_containerregistry//cmd/crane",
262+
"@com_github_google_go_containerregistry//cmd/registry",
263+
],
264+
env = {
265+
"KUBECTL": "$(location :kubectl)",
266+
"PUSH_IMAGE": "$(location :push_image)",
267+
"CRANE_BIN": "$(location @com_github_google_go_containerregistry//cmd/crane)",
268+
"REGISTRY_BIN": "$(location @com_github_google_go_containerregistry//cmd/registry)",
269+
},
270+
tags = ["exclusive"], # this test starts a registry on fixed port 1338
271+
)
272+
273+
sh_test(
274+
name = "image_mirror_with_tag_test2",
275+
srcs = ["gitops_setup_test.sh"],
276+
args = [
277+
"$(location :image_mirror_with_tag) localhost:1338/mirror2/localhost1338/image:tag1",
278+
],
279+
data = [
280+
":image_mirror_with_tag",
281+
":kubectl",
282+
":push_image",
283+
"@com_github_google_go_containerregistry//cmd/crane",
284+
"@com_github_google_go_containerregistry//cmd/registry",
285+
],
286+
env = {
287+
"KUBECTL": "$(location :kubectl)",
288+
"PUSH_IMAGE": "$(location :push_image)",
289+
"CRANE_BIN": "$(location @com_github_google_go_containerregistry//cmd/crane)",
290+
"REGISTRY_BIN": "$(location @com_github_google_go_containerregistry//cmd/registry)",
291+
},
292+
tags = ["exclusive"], # this test starts a registry on fixed port 1338
293+
)

mirror/tests/kustomize1.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
app: myapp
1313
spec:
1414
containers:
15-
- image: localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc
15+
- image: localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc
1616
name: myapp
1717
ports:
1818
- containerPort: 8080

mirror/tests/kustomize2.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ spec:
1212
app: myapp
1313
spec:
1414
containers:
15-
- image: localhost:1338/mirror/localhost/1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc
15+
- image: localhost:1338/mirror/localhost1338/image@sha256:b812c0570a7c369b2863c64e22760dc1b1dbc025a739f02db376bac62862f4cc
1616
name: myapp
1717
ports:
1818
- containerPort: 8080

pkg/mirror/mirror.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,16 @@ func ExecuteContext(ctx context.Context, fromLocation, to, digest string) error
3737
if err != nil {
3838
return err
3939
}
40-
// check if dst exists already
41-
if _, ok := dstRef.(name.Tag); ok {
42-
logs.Progress.Printf("fetching manifest for %s", dstRef)
43-
dst, err := remote.Get(dstRef, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
44-
if err == nil {
45-
// if the dst manifest exists, check if it's the same as the src
46-
if dst.Digest.String() == hash.String() {
47-
logs.Progress.Printf("found manifest for %s digest %s", dstRef, dst.Digest)
48-
return nil
49-
}
50-
logs.Progress.Printf("manifest digest for %s expected %s got %s", shaDstRef, hash, dst.Digest)
51-
}
52-
} else {
53-
logs.Progress.Printf("fetching manifest for %s", shaDstRef)
54-
_, err = remote.Head(shaDstRef, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
55-
if err == nil {
56-
// if the dst manifest exists, check if it's the same as the src
57-
logs.Progress.Printf("found manifest for %s", shaDstRef)
58-
return nil
59-
}
60-
40+
// check if dst exists already.
41+
// We are not verifying if the dst has the same tag
42+
logs.Progress.Printf("fetching manifest for %s", shaDstRef)
43+
_, err = remote.Head(shaDstRef, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
44+
if err == nil {
45+
// if the dst manifest exists, check if it's the same as the src
46+
logs.Progress.Printf("found manifest for %s", shaDstRef)
47+
return nil
6148
}
49+
6250
logs.Progress.Printf("fetching manifest for %s", ref)
6351
src, err := remote.Get(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
6452
if err != nil {

0 commit comments

Comments
 (0)