From 500036f21aa8c1d73ab929997c930893a76a45fa Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 6 Oct 2020 13:04:28 +0100 Subject: [PATCH] Use UpdateWithSetters in controller (and update the test to suit) --- .../imageupdateautomation_controller.go | 8 ++--- .../appconfig-setters-expected/deploy.yaml | 10 ++++++ controllers/update_test.go | 33 +++++++++++++------ go.mod | 1 + go.sum | 8 +++++ 5 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 controllers/testdata/appconfig-setters-expected/deploy.yaml diff --git a/controllers/imageupdateautomation_controller.go b/controllers/imageupdateautomation_controller.go index 2ea0bc30..03bf370a 100644 --- a/controllers/imageupdateautomation_controller.go +++ b/controllers/imageupdateautomation_controller.go @@ -145,8 +145,8 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Resu } case updateStrat.Setters != nil: // For setters we first want to compile a list of _all_ the - // policies (maybe in the future this could be filtered by the - // automation object). + // policies in the same namespace (maybe in the future this + // could be filtered by the automation object). var policies imagev1alpha1_reflect.ImagePolicyList if err := r.List(ctx, &policies, &client.ListOptions{Namespace: req.NamespacedName.Namespace}); err != nil { return ctrl.Result{}, err @@ -407,6 +407,6 @@ func updateAccordingToImagePolicy(ctx context.Context, path string, policy *imag // updateAccordingToSetters updates files under the root by treating // the given image policies as kyaml setters. -func updateAccordingToSetters(ctx context.Context, root string, policies []imagev1alpha1_reflect.ImagePolicy) error { - return nil +func updateAccordingToSetters(ctx context.Context, path string, policies []imagev1alpha1_reflect.ImagePolicy) error { + return update.UpdateWithSetters(path, path, policies) } diff --git a/controllers/testdata/appconfig-setters-expected/deploy.yaml b/controllers/testdata/appconfig-setters-expected/deploy.yaml new file mode 100644 index 00000000..924875cf --- /dev/null +++ b/controllers/testdata/appconfig-setters-expected/deploy.yaml @@ -0,0 +1,10 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test +spec: + template: + spec: + containers: + - name: hello + image: helloworld:1.0.1 # SETTER_SITE diff --git a/controllers/update_test.go b/controllers/update_test.go index 8e7e0594..cbe531b8 100644 --- a/controllers/update_test.go +++ b/controllers/update_test.go @@ -35,12 +35,14 @@ import ( "github.com/go-git/go-git/v5/storage/memory" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/otiai10/copy" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" imagev1alpha1 "github.com/fluxcd/image-automation-controller/api/v1alpha1" "github.com/fluxcd/image-automation-controller/pkg/test" + "github.com/fluxcd/image-automation-controller/pkg/update" imagev1alpha1_reflect "github.com/fluxcd/image-reflector-controller/api/v1alpha1" sourcev1alpha1 "github.com/fluxcd/source-controller/api/v1alpha1" ) @@ -279,13 +281,8 @@ var _ = Describe("ImageUpdateAutomation", func() { ReferenceName: plumbing.NewBranchReferenceName(defaultBranch), }) Expect(err).ToNot(HaveOccurred()) - // NB this requires knowledge of what's in the git - // repo, so a little brittle - deployment := filepath.Join(tmp, "deploy.yaml") - filebytes, err := ioutil.ReadFile(deployment) - Expect(err).NotTo(HaveOccurred()) - newfilebytes := bytes.ReplaceAll(filebytes, []byte("SETTER_SITE"), []byte(setterName(policyKey))) - Expect(ioutil.WriteFile(deployment, newfilebytes, os.FileMode(0666))).To(Succeed()) + + replaceMarker(tmp, policyKey) worktree, err := repo.Worktree() Expect(err).ToNot(HaveOccurred()) _, err = worktree.Add("deploy.yaml") @@ -351,19 +348,35 @@ var _ = Describe("ImageUpdateAutomation", func() { Expect(err).ToNot(HaveOccurred()) defer os.RemoveAll(tmp) + expected, err := ioutil.TempDir("", "gotest-imageauto-expected") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(expected) + copy.Copy("testdata/appconfig-setters-expected", expected) + replaceMarker(expected, policyKey) + _, err = git.PlainClone(tmp, false, &git.CloneOptions{ URL: repoURL, ReferenceName: plumbing.NewBranchReferenceName(defaultBranch), }) Expect(err).ToNot(HaveOccurred()) - test.ExpectMatchingDirectories(tmp, "testdata/appconfig-setters-expected") + test.ExpectMatchingDirectories(tmp, expected) }) }) }) }) -func setterName(name types.NamespacedName) string { - return fmt.Sprintf("#/image/%s/%s", name.Namespace, name.Name) +func replaceMarker(path string, policyKey types.NamespacedName) { + // NB this requires knowledge of what's in the git + // repo, so a little brittle + deployment := filepath.Join(path, "deploy.yaml") + filebytes, err := ioutil.ReadFile(deployment) + Expect(err).NotTo(HaveOccurred()) + newfilebytes := bytes.ReplaceAll(filebytes, []byte("SETTER_SITE"), []byte(setterRef(policyKey))) + Expect(ioutil.WriteFile(deployment, newfilebytes, os.FileMode(0666))).To(Succeed()) +} + +func setterRef(name types.NamespacedName) string { + return fmt.Sprintf(`{"%s": "%s:%s"}`, update.SetterShortHand, name.Namespace, name.Name) } func waitForNewHead(repo *git.Repository) { diff --git a/go.mod b/go.mod index cc6a3887..0bcf9f06 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/google/go-containerregistry v0.1.1 github.com/onsi/ginkgo v1.12.1 github.com/onsi/gomega v1.10.1 + github.com/otiai10/copy v1.2.0 k8s.io/api v0.18.6 k8s.io/apimachinery v0.18.6 k8s.io/client-go v0.18.6 diff --git a/go.sum b/go.sum index f379d722..26a849ef 100644 --- a/go.sum +++ b/go.sum @@ -800,6 +800,14 @@ github.com/opencontainers/runc v0.1.1 h1:GlxAyO6x8rfZYN9Tt0Kti5a/cP41iuiO2yYT0IJ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs= +github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= +github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI= +github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc= +github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/paulmach/orb v0.1.3/go.mod h1:VFlX/8C+IQ1p6FTRRKzKoOPJnvEtA5G0Veuqwbu//Vk= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=