Skip to content

Commit b487430

Browse files
authored
Merge pull request #113 from weaveworks/add-image-tag
Update the ImagePolicy generator to add the image.
2 parents 0aed926 + 3e41174 commit b487430

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

controllers/templates/generators/imagepolicy/image_policy.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,28 @@ func (g *ImagePolicyGenerator) Generate(ctx context.Context, sg *templatesv1.Git
6767

6868
g.Logger.Info("image policy", "latestImage", repo.Status.LatestImage, "latestTag", latestTag.TagStr(), "previousImage", repo.Status.ObservedPreviousImage)
6969

70-
result = append(result, map[string]any{
70+
// This stores empty strings the for the previous tag if it's empty because
71+
// that saves users having to check for the existence of the fields in their
72+
// templates.
73+
previousTag := ""
74+
if repo.Status.ObservedPreviousImage != "" {
75+
parsedTag, err := name.NewTag(repo.Status.ObservedPreviousImage)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
previousTag = parsedTag.TagStr()
81+
}
82+
83+
generated := map[string]any{
7184
"latestImage": repo.Status.LatestImage,
85+
"image": latestTag.Repository.Name(),
7286
"latestTag": latestTag.TagStr(),
7387
"previousImage": repo.Status.ObservedPreviousImage,
74-
})
88+
"previousTag": previousTag,
89+
}
90+
91+
result = append(result, generated)
7592

7693
return result, nil
7794
}

controllers/templates/generators/imagepolicy/image_policy_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,30 @@ func TestGenerate(t *testing.T) {
5151
&templatesv1.ImagePolicyGenerator{
5252
PolicyRef: "test-policy",
5353
},
54-
[]runtime.Object{test.NewImagePolicy(withImages("testing/test:v0.30.0", "testing/test:v0.29.0"))},
54+
[]runtime.Object{test.NewImagePolicy(withImages("ghcr.io/testing/test:v0.30.0", "ghcr.io/testing/test:v0.29.0"))},
5555
[]map[string]any{
5656
{
57-
"latestImage": "testing/test:v0.30.0",
57+
"image": "ghcr.io/testing/test",
58+
"latestImage": "ghcr.io/testing/test:v0.30.0",
5859
"latestTag": "v0.30.0",
59-
"previousImage": "testing/test:v0.29.0",
60+
"previousImage": "ghcr.io/testing/test:v0.29.0",
61+
"previousTag": "v0.29.0",
62+
},
63+
},
64+
},
65+
{
66+
"no previous image",
67+
&templatesv1.ImagePolicyGenerator{
68+
PolicyRef: "test-policy",
69+
},
70+
[]runtime.Object{test.NewImagePolicy(withImages("ghcr.io/testing/test:v0.30.0", ""))},
71+
[]map[string]any{
72+
{
73+
"image": "ghcr.io/testing/test",
74+
"latestImage": "ghcr.io/testing/test:v0.30.0",
75+
"latestTag": "v0.30.0",
76+
"previousImage": "",
77+
"previousTag": "",
6078
},
6179
},
6280
},

0 commit comments

Comments
 (0)