From 3e411744dec81f356006def7ff1a0d56e4bcccf5 Mon Sep 17 00:00:00 2001 From: Kevin McDermott Date: Thu, 17 Aug 2023 08:33:36 +0100 Subject: [PATCH] Update the ImagePolicy generator to add the image. This adds the source image to the elements from the image policy generator. --- .../generators/imagepolicy/image_policy.go | 21 ++++++++++++++-- .../imagepolicy/image_policy_test.go | 24 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/controllers/templates/generators/imagepolicy/image_policy.go b/controllers/templates/generators/imagepolicy/image_policy.go index 7ae0f309..9fac5d85 100644 --- a/controllers/templates/generators/imagepolicy/image_policy.go +++ b/controllers/templates/generators/imagepolicy/image_policy.go @@ -67,11 +67,28 @@ func (g *ImagePolicyGenerator) Generate(ctx context.Context, sg *templatesv1.Git g.Logger.Info("image policy", "latestImage", repo.Status.LatestImage, "latestTag", latestTag.TagStr(), "previousImage", repo.Status.ObservedPreviousImage) - result = append(result, map[string]any{ + // This stores empty strings the for the previous tag if it's empty because + // that saves users having to check for the existence of the fields in their + // templates. + previousTag := "" + if repo.Status.ObservedPreviousImage != "" { + parsedTag, err := name.NewTag(repo.Status.ObservedPreviousImage) + if err != nil { + return nil, err + } + + previousTag = parsedTag.TagStr() + } + + generated := map[string]any{ "latestImage": repo.Status.LatestImage, + "image": latestTag.Repository.Name(), "latestTag": latestTag.TagStr(), "previousImage": repo.Status.ObservedPreviousImage, - }) + "previousTag": previousTag, + } + + result = append(result, generated) return result, nil } diff --git a/controllers/templates/generators/imagepolicy/image_policy_test.go b/controllers/templates/generators/imagepolicy/image_policy_test.go index bbaa9573..11528281 100644 --- a/controllers/templates/generators/imagepolicy/image_policy_test.go +++ b/controllers/templates/generators/imagepolicy/image_policy_test.go @@ -51,12 +51,30 @@ func TestGenerate(t *testing.T) { &templatesv1.ImagePolicyGenerator{ PolicyRef: "test-policy", }, - []runtime.Object{test.NewImagePolicy(withImages("testing/test:v0.30.0", "testing/test:v0.29.0"))}, + []runtime.Object{test.NewImagePolicy(withImages("ghcr.io/testing/test:v0.30.0", "ghcr.io/testing/test:v0.29.0"))}, []map[string]any{ { - "latestImage": "testing/test:v0.30.0", + "image": "ghcr.io/testing/test", + "latestImage": "ghcr.io/testing/test:v0.30.0", "latestTag": "v0.30.0", - "previousImage": "testing/test:v0.29.0", + "previousImage": "ghcr.io/testing/test:v0.29.0", + "previousTag": "v0.29.0", + }, + }, + }, + { + "no previous image", + &templatesv1.ImagePolicyGenerator{ + PolicyRef: "test-policy", + }, + []runtime.Object{test.NewImagePolicy(withImages("ghcr.io/testing/test:v0.30.0", ""))}, + []map[string]any{ + { + "image": "ghcr.io/testing/test", + "latestImage": "ghcr.io/testing/test:v0.30.0", + "latestTag": "v0.30.0", + "previousImage": "", + "previousTag": "", }, }, },