Skip to content

Commit

Permalink
Merge pull request #113 from weaveworks/add-image-tag
Browse files Browse the repository at this point in the history
Update the ImagePolicy generator to add the image.
  • Loading branch information
bigkevmcd authored Aug 17, 2023
2 parents 0aed926 + 3e41174 commit b487430
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
21 changes: 19 additions & 2 deletions controllers/templates/generators/imagepolicy/image_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 21 additions & 3 deletions controllers/templates/generators/imagepolicy/image_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
},
},
},
Expand Down

0 comments on commit b487430

Please sign in to comment.