Skip to content

Commit

Permalink
Use UpdateWithSetters in controller
Browse files Browse the repository at this point in the history
(and update the test to suit)
  • Loading branch information
squaremo committed Oct 8, 2020
1 parent ed268fa commit 500036f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
8 changes: 4 additions & 4 deletions controllers/imageupdateautomation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions controllers/testdata/appconfig-setters-expected/deploy.yaml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 23 additions & 10 deletions controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 500036f

Please sign in to comment.