Skip to content

Commit 2eebaa4

Browse files
committed
Make the image policy name available to templates
Signed-off-by: Michael Bridgen <[email protected]>
1 parent df7d570 commit 2eebaa4

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

controllers/update_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Objects:
135135
136136
Images:
137137
{{ range .Updated.Images -}}
138-
- {{.}}
138+
- {{.}} ({{.Policy.Name}})
139139
{{ end -}}
140140
`
141141
commitMessageFmt = `Commit summary
@@ -147,13 +147,11 @@ Files:
147147
Objects:
148148
- Deployment test
149149
Images:
150-
- helloworld:v1.0.0
150+
- helloworld:v1.0.0 (%s)
151151
`
152152
)
153153

154154
BeforeEach(func() {
155-
commitMessage = fmt.Sprintf(commitMessageFmt, namespace.Name)
156-
157155
Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
158156
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
159157
var err error
@@ -207,6 +205,9 @@ Images:
207205
Expect(k8sClient.Create(context.Background(), policy)).To(Succeed())
208206
Expect(k8sClient.Status().Update(context.Background(), policy)).To(Succeed())
209207

208+
// Format the expected message given the generated values
209+
commitMessage = fmt.Sprintf(commitMessageFmt, namespace.Name, policyKey.Name)
210+
210211
// Insert a setter reference into the deployment file,
211212
// before creating the automation object itself.
212213
commitInRepo(repoURL, branch, "Install setter marker", func(tmp string) {

pkg/update/result.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package update
22

33
import (
44
"github.com/google/go-containerregistry/pkg/name"
5+
"k8s.io/apimachinery/pkg/types"
56
"sigs.k8s.io/kustomize/kyaml/yaml"
67
)
78

@@ -22,10 +23,20 @@ type ImageRef interface {
2223
// Name gives the fully-qualified reference name, e.g.,
2324
// "index.docker.io/library/helloworld:v1.0.1"
2425
Name() string
26+
// Policy gives the namespaced name of the image policy that led
27+
// to the update.
28+
Policy() types.NamespacedName
2529
}
2630

2731
type imageRef struct {
2832
name.Reference
33+
policy types.NamespacedName
34+
}
35+
36+
// Policy gives the namespaced name of the policy that led to the
37+
// update.
38+
func (i imageRef) Policy() types.NamespacedName {
39+
return i.policy
2940
}
3041

3142
// Repository gives the repository component of the image ref.

pkg/update/result_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import (
44
"github.com/google/go-containerregistry/pkg/name"
55
. "github.com/onsi/ginkgo"
66
. "github.com/onsi/gomega"
7+
"k8s.io/apimachinery/pkg/types"
78
"sigs.k8s.io/kustomize/kyaml/yaml"
89
)
910

11+
// mustRef creates an imageRef for use in tests. It panics if the ref
12+
// given is invalid.
1013
func mustRef(ref string) imageRef {
1114
r, err := name.ParseReference(ref)
1215
if err != nil {
1316
panic(err)
1417
}
15-
return imageRef{r}
18+
return imageRef{r, types.NamespacedName{}}
1619
}
1720

1821
var _ = Describe("image ref", func() {

pkg/update/setters.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/go-openapi/spec"
2323
"github.com/google/go-containerregistry/pkg/name"
24+
"k8s.io/apimachinery/pkg/types"
2425
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
2526
"sigs.k8s.io/kustomize/kyaml/kio"
2627
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
@@ -88,7 +89,7 @@ func UpdateWithSetters(inpath, outpath string, policies []imagev1alpha1_reflect.
8889

8990
var settersSchema spec.Schema
9091
var setters []*setters2.Set
91-
setterToImage := make(map[string]name.Reference)
92+
setterToImage := make(map[string]imageRef)
9293

9394
// collect setter defs and setters by going through all the image
9495
// policies available.
@@ -104,10 +105,18 @@ func UpdateWithSetters(inpath, outpath string, policies []imagev1alpha1_reflect.
104105
// being `latest` if empty in the input; but I'm assuming here
105106
// that the policy won't have a tagless ref.
106107
image := policy.Status.LatestImage
107-
ref, err := name.ParseReference(image, name.WeakValidation)
108+
r, err := name.ParseReference(image, name.WeakValidation)
108109
if err != nil {
109110
return Result{}, fmt.Errorf("encountered invalid image ref %q: %w", policy.Status.LatestImage, err)
110111
}
112+
ref := imageRef{
113+
Reference: r,
114+
policy: types.NamespacedName{
115+
Name: policy.Name,
116+
Namespace: policy.Namespace,
117+
},
118+
}
119+
111120
tag := ref.Identifier()
112121
// annoyingly, neither the library imported above, nor an
113122
// alternative, I found will yield the original image name;
@@ -181,7 +190,7 @@ type setAllRecorder struct {
181190
updates []update
182191
}
183192

184-
func (s *setAllRecorder) getResult(nameToImage map[string]name.Reference) Result {
193+
func (s *setAllRecorder) getResult(nameToImage map[string]imageRef) Result {
185194
result := Result{
186195
Files: make(map[string]FileResult),
187196
}
@@ -202,12 +211,11 @@ updates:
202211
}
203212
id := ObjectIdentifier{meta.GetIdentifier()}
204213

205-
name, ok := nameToImage[update.name]
214+
ref, ok := nameToImage[update.name]
206215
if !ok { // this means an update was made that wasn't recorded as being an image
207216
continue updates
208217
}
209218
// if the name and tag of an image are both used, we don't need to record it twice
210-
ref := imageRef{name}
211219
for _, n := range objects[id] {
212220
if n == ref {
213221
continue updates

pkg/update/update_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
. "github.com/onsi/ginkgo"
2626
. "github.com/onsi/gomega"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/types"
2829
"sigs.k8s.io/kustomize/kyaml/yaml"
2930

3031
"github.com/fluxcd/image-automation-controller/pkg/test"
@@ -96,7 +97,10 @@ var _ = Describe("Update image via kyaml setters2", func() {
9697
},
9798
}}
9899
r, _ := name.ParseReference("updated:v1.0.1")
99-
expectedImageRef := imageRef{r}
100+
expectedImageRef := imageRef{r, types.NamespacedName{
101+
Name: "policy",
102+
Namespace: "automation-ns",
103+
}}
100104

101105
expectedResult := Result{
102106
Files: map[string]FileResult{

0 commit comments

Comments
 (0)