Skip to content

Commit a45a216

Browse files
authored
Merge pull request #74 from fluxcd/branch-mandatory
Make the branch field mandatory
2 parents 825645d + 308748f commit a45a216

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

api/v1alpha1/imageupdateautomation_types.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ type GitCheckoutSpec struct {
5454
// to a git repository to update files in.
5555
// +required
5656
GitRepositoryRef corev1.LocalObjectReference `json:"gitRepositoryRef"`
57-
// Branch gives the branch to clone from the git repository. If
58-
// missing, it will be left to default; be aware this may give
59-
// indeterminate results.
60-
// +optional
61-
Branch string `json:"branch,omitempty"`
57+
// Branch gives the branch to clone from the git repository.
58+
// +required
59+
Branch string `json:"branch"`
6260
}
6361

6462
// UpdateStrategy is a union of the various strategies for updating

config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ spec:
4747
properties:
4848
branch:
4949
description: Branch gives the branch to clone from the git repository.
50-
If missing, it will be left to default; be aware this may give
51-
indeterminate results.
5250
type: string
5351
gitRepositoryRef:
5452
description: GitRepositoryRef refers to the resource giving access
@@ -60,6 +58,7 @@ spec:
6058
type: string
6159
type: object
6260
required:
61+
- branch
6362
- gitRepositoryRef
6463
type: object
6564
commit:

controllers/update_test.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ import (
5454

5555
const timeout = 10 * time.Second
5656

57-
const defaultBranch = "test-main"
58-
5957
// Copied from
6058
// https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
6159
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
@@ -70,6 +68,7 @@ func randStringRunes(n int) string {
7068

7169
var _ = Describe("ImageUpdateAutomation", func() {
7270
var (
71+
branch string
7372
repositoryPath string
7473
repoURL string
7574
namespace *corev1.Namespace
@@ -80,6 +79,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
8079

8180
// Start the git server
8281
BeforeEach(func() {
82+
branch = randStringRunes(8)
8383
repositoryPath = "/config-" + randStringRunes(5) + ".git"
8484

8585
namespace = &corev1.Namespace{}
@@ -119,7 +119,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
119119
})
120120

121121
It("Initialises git OK", func() {
122-
Expect(initGitRepo(gitServer, "testdata/appconfig", repositoryPath)).To(Succeed())
122+
Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
123123
})
124124

125125
Context("with ImagePolicy", func() {
@@ -135,13 +135,13 @@ var _ = Describe("ImageUpdateAutomation", func() {
135135
BeforeEach(func() {
136136
commitMessage = "Commit a difference " + randStringRunes(5)
137137

138-
Expect(initGitRepo(gitServer, "testdata/appconfig", repositoryPath)).To(Succeed())
138+
Expect(initGitRepo(gitServer, "testdata/appconfig", branch, repositoryPath)).To(Succeed())
139139

140140
var err error
141141
localRepo, err = git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
142142
URL: repoURL,
143143
RemoteName: "origin",
144-
ReferenceName: plumbing.NewBranchReferenceName(defaultBranch),
144+
ReferenceName: plumbing.NewBranchReferenceName(branch),
145145
})
146146
Expect(err).ToNot(HaveOccurred())
147147

@@ -189,14 +189,14 @@ var _ = Describe("ImageUpdateAutomation", func() {
189189
BeforeEach(func() {
190190
// Insert a setter reference into the deployment file,
191191
// before creating the automation object itself.
192-
commitInRepo(repoURL, "Install setter marker", func(tmp string) {
192+
commitInRepo(repoURL, branch, "Install setter marker", func(tmp string) {
193193
replaceMarker(tmp, policyKey)
194194
})
195195

196196
// pull the head commit we just pushed, so it's not
197197
// considered a new commit when checking for a commit
198198
// made by automation.
199-
waitForNewHead(localRepo)
199+
waitForNewHead(localRepo, branch)
200200

201201
// now create the automation object, and let it (one
202202
// hopes!) make a commit itself.
@@ -215,7 +215,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
215215
GitRepositoryRef: corev1.LocalObjectReference{
216216
Name: gitRepoKey.Name,
217217
},
218-
Branch: defaultBranch,
218+
Branch: branch,
219219
},
220220
Update: imagev1.UpdateStrategy{
221221
Setters: &imagev1.SettersStrategy{},
@@ -227,7 +227,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
227227
}
228228
Expect(k8sClient.Create(context.Background(), updateBySetters)).To(Succeed())
229229
// wait for a new commit to be made by the controller
230-
waitForNewHead(localRepo)
230+
waitForNewHead(localRepo, branch)
231231
})
232232

233233
AfterEach(func() {
@@ -241,7 +241,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
241241
Expect(err).ToNot(HaveOccurred())
242242
Expect(commit.Message).To(Equal(commitMessage))
243243

244-
compareRepoWithExpected(repoURL, "testdata/appconfig-setters-expected", func(tmp string) {
244+
compareRepoWithExpected(repoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
245245
replaceMarker(tmp, policyKey)
246246
})
247247
})
@@ -285,20 +285,21 @@ var _ = Describe("ImageUpdateAutomation", func() {
285285
Expect(updateBySetters.Status.LastAutomationRunTime).ToNot(BeNil())
286286
lastRunTime := updateBySetters.Status.LastAutomationRunTime.Time
287287

288-
commitInRepo(repoURL, "Revert image update", func(tmp string) {
288+
commitInRepo(repoURL, branch, "Revert image update", func(tmp string) {
289289
// revert the change made by copying the old version
290290
// of the file back over then restoring the setter
291291
// marker
292292
copy.Copy("testdata/appconfig/deploy.yaml", filepath.Join(tmp, "deploy.yaml"))
293293
replaceMarker(tmp, policyKey)
294294
})
295295
// check that it was reverted correctly
296-
compareRepoWithExpected(repoURL, "testdata/appconfig", func(tmp string) {
296+
compareRepoWithExpected(repoURL, branch, "testdata/appconfig", func(tmp string) {
297297
replaceMarker(tmp, policyKey)
298298
})
299299

300300
ts := time.Now().String()
301301
var updatePatch imagev1.ImageUpdateAutomation
302+
updatePatch.Spec = updateBySetters.Spec // otherwise the patch will blank some fields
302303
updatePatch.Name = updateKey.Name
303304
updatePatch.Namespace = updateKey.Namespace
304305
updatePatch.ObjectMeta.Annotations = map[string]string{
@@ -320,7 +321,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
320321
Expect(newUpdate.Status.LastHandledReconcileAt).To(Equal(ts))
321322

322323
// check that a new commit was made
323-
compareRepoWithExpected(repoURL, "testdata/appconfig-setters-expected", func(tmp string) {
324+
compareRepoWithExpected(repoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
324325
replaceMarker(tmp, policyKey)
325326
})
326327
})
@@ -342,14 +343,14 @@ func setterRef(name types.NamespacedName) string {
342343
return fmt.Sprintf(`{"%s": "%s:%s"}`, update.SetterShortHand, name.Namespace, name.Name)
343344
}
344345

345-
func waitForNewHead(repo *git.Repository) {
346+
func waitForNewHead(repo *git.Repository, branch string) {
346347
head, _ := repo.Head()
347348
headHash := head.Hash().String()
348349
working, err := repo.Worktree()
349350
Expect(err).ToNot(HaveOccurred())
350351
Eventually(func() bool {
351352
if working.Pull(&git.PullOptions{
352-
ReferenceName: plumbing.NewBranchReferenceName(defaultBranch),
353+
ReferenceName: plumbing.NewBranchReferenceName(branch),
353354
}); err != nil {
354355
return false
355356
}
@@ -358,7 +359,7 @@ func waitForNewHead(repo *git.Repository) {
358359
}, timeout, time.Second).Should(BeTrue())
359360
}
360361

361-
func compareRepoWithExpected(repoURL, fixture string, changeFixture func(tmp string)) {
362+
func compareRepoWithExpected(repoURL, branch, fixture string, changeFixture func(tmp string)) {
362363
expected, err := ioutil.TempDir("", "gotest-imageauto-expected")
363364
Expect(err).ToNot(HaveOccurred())
364365
defer os.RemoveAll(expected)
@@ -370,19 +371,19 @@ func compareRepoWithExpected(repoURL, fixture string, changeFixture func(tmp str
370371
defer os.RemoveAll(tmp)
371372
_, err = git.PlainClone(tmp, false, &git.CloneOptions{
372373
URL: repoURL,
373-
ReferenceName: plumbing.NewBranchReferenceName(defaultBranch),
374+
ReferenceName: plumbing.NewBranchReferenceName(branch),
374375
})
375376
Expect(err).ToNot(HaveOccurred())
376377
test.ExpectMatchingDirectories(tmp, expected)
377378
}
378379

379-
func commitInRepo(repoURL, msg string, changeFiles func(path string)) {
380+
func commitInRepo(repoURL, branch, msg string, changeFiles func(path string)) {
380381
tmp, err := ioutil.TempDir("", "gotest-imageauto")
381382
Expect(err).ToNot(HaveOccurred())
382383
defer os.RemoveAll(tmp)
383384
repo, err := git.PlainClone(tmp, false, &git.CloneOptions{
384385
URL: repoURL,
385-
ReferenceName: plumbing.NewBranchReferenceName(defaultBranch),
386+
ReferenceName: plumbing.NewBranchReferenceName(branch),
386387
})
387388
Expect(err).ToNot(HaveOccurred())
388389

@@ -404,7 +405,7 @@ func commitInRepo(repoURL, msg string, changeFiles func(path string)) {
404405
}
405406

406407
// Initialise a git server with a repo including the files in dir.
407-
func initGitRepo(gitServer *gittestserver.GitServer, fixture, repositoryPath string) error {
408+
func initGitRepo(gitServer *gittestserver.GitServer, fixture, branch, repositoryPath string) error {
408409
fs := memfs.New()
409410
repo, err := git.Init(memory.NewStorage(), fs)
410411
if err != nil {
@@ -457,7 +458,7 @@ func initGitRepo(gitServer *gittestserver.GitServer, fixture, repositoryPath str
457458
}
458459

459460
if err = working.Checkout(&git.CheckoutOptions{
460-
Branch: plumbing.NewBranchReferenceName(defaultBranch),
461+
Branch: plumbing.NewBranchReferenceName(branch),
461462
Create: true,
462463
}); err != nil {
463464
return err

0 commit comments

Comments
 (0)