Skip to content

Commit

Permalink
Make the image policy name available to templates
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Bridgen <michael@weave.works>
  • Loading branch information
squaremo committed Mar 3, 2021
1 parent df7d570 commit 2eebaa4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
9 changes: 5 additions & 4 deletions controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Objects:
Images:
{{ range .Updated.Images -}}
- {{.}}
- {{.}} ({{.Policy.Name}})
{{ end -}}
`
commitMessageFmt = `Commit summary
Expand All @@ -147,13 +147,11 @@ Files:
Objects:
- Deployment test
Images:
- helloworld:v1.0.0
- helloworld:v1.0.0 (%s)
`
)

BeforeEach(func() {
commitMessage = fmt.Sprintf(commitMessageFmt, namespace.Name)

Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
repoURL := gitServer.HTTPAddressWithCredentials() + repositoryPath
var err error
Expand Down Expand Up @@ -207,6 +205,9 @@ Images:
Expect(k8sClient.Create(context.Background(), policy)).To(Succeed())
Expect(k8sClient.Status().Update(context.Background(), policy)).To(Succeed())

// Format the expected message given the generated values
commitMessage = fmt.Sprintf(commitMessageFmt, namespace.Name, policyKey.Name)

// Insert a setter reference into the deployment file,
// before creating the automation object itself.
commitInRepo(repoURL, branch, "Install setter marker", func(tmp string) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/update/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package update

import (
"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

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

type imageRef struct {
name.Reference
policy types.NamespacedName
}

// Policy gives the namespaced name of the policy that led to the
// update.
func (i imageRef) Policy() types.NamespacedName {
return i.policy
}

// Repository gives the repository component of the image ref.
Expand Down
5 changes: 4 additions & 1 deletion pkg/update/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"github.com/google/go-containerregistry/pkg/name"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// mustRef creates an imageRef for use in tests. It panics if the ref
// given is invalid.
func mustRef(ref string) imageRef {
r, err := name.ParseReference(ref)
if err != nil {
panic(err)
}
return imageRef{r}
return imageRef{r, types.NamespacedName{}}
}

var _ = Describe("image ref", func() {
Expand Down
18 changes: 13 additions & 5 deletions pkg/update/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/go-openapi/spec"
"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
Expand Down Expand Up @@ -88,7 +89,7 @@ func UpdateWithSetters(inpath, outpath string, policies []imagev1alpha1_reflect.

var settersSchema spec.Schema
var setters []*setters2.Set
setterToImage := make(map[string]name.Reference)
setterToImage := make(map[string]imageRef)

// collect setter defs and setters by going through all the image
// policies available.
Expand All @@ -104,10 +105,18 @@ func UpdateWithSetters(inpath, outpath string, policies []imagev1alpha1_reflect.
// being `latest` if empty in the input; but I'm assuming here
// that the policy won't have a tagless ref.
image := policy.Status.LatestImage
ref, err := name.ParseReference(image, name.WeakValidation)
r, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return Result{}, fmt.Errorf("encountered invalid image ref %q: %w", policy.Status.LatestImage, err)
}
ref := imageRef{
Reference: r,
policy: types.NamespacedName{
Name: policy.Name,
Namespace: policy.Namespace,
},
}

tag := ref.Identifier()
// annoyingly, neither the library imported above, nor an
// alternative, I found will yield the original image name;
Expand Down Expand Up @@ -181,7 +190,7 @@ type setAllRecorder struct {
updates []update
}

func (s *setAllRecorder) getResult(nameToImage map[string]name.Reference) Result {
func (s *setAllRecorder) getResult(nameToImage map[string]imageRef) Result {
result := Result{
Files: make(map[string]FileResult),
}
Expand All @@ -202,12 +211,11 @@ updates:
}
id := ObjectIdentifier{meta.GetIdentifier()}

name, ok := nameToImage[update.name]
ref, ok := nameToImage[update.name]
if !ok { // this means an update was made that wasn't recorded as being an image
continue updates
}
// if the name and tag of an image are both used, we don't need to record it twice
ref := imageRef{name}
for _, n := range objects[id] {
if n == ref {
continue updates
Expand Down
6 changes: 5 additions & 1 deletion pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/kustomize/kyaml/yaml"

"github.com/fluxcd/image-automation-controller/pkg/test"
Expand Down Expand Up @@ -96,7 +97,10 @@ var _ = Describe("Update image via kyaml setters2", func() {
},
}}
r, _ := name.ParseReference("updated:v1.0.1")
expectedImageRef := imageRef{r}
expectedImageRef := imageRef{r, types.NamespacedName{
Name: "policy",
Namespace: "automation-ns",
}}

expectedResult := Result{
Files: map[string]FileResult{
Expand Down

0 comments on commit 2eebaa4

Please sign in to comment.