Skip to content

Commit ebb2d6a

Browse files
committed
Use sourcev1.GitRepositoryRef
This changes the API so that the checkout field has a ref, the same as GItRepository. This means you can check out a branch or a tag or a particular commit. Most of these won't work unless you supply a branch to push to as well. An addtional change is that you can leave out the checkout altogether, and the ref will default to that given in the GitRepository, or its default. In the latter case, again you will need to provide a push branch. Signed-off-by: Michael Bridgen <[email protected]>
1 parent b28c5cc commit ebb2d6a

File tree

7 files changed

+97
-45
lines changed

7 files changed

+97
-45
lines changed

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
github.com/fluxcd/pkg/apis/meta v0.8.0
7+
github.com/fluxcd/source-controller/api v0.10.0
78
k8s.io/apimachinery v0.20.2
89
sigs.k8s.io/controller-runtime v0.8.3
910
)

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
9090
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
9191
github.com/fluxcd/pkg/apis/meta v0.8.0 h1:wqWpUsxhKHB1ZztcvOz+vnyhdKW9cWmjFp8Vci/XOdk=
9292
github.com/fluxcd/pkg/apis/meta v0.8.0/go.mod h1:yHuY8kyGHYz22I0jQzqMMGCcHViuzC/WPdo9Gisk8Po=
93+
github.com/fluxcd/source-controller/api v0.10.0 h1:Mu4cAXtZ7yq/rIrab81q1jbbhWwUxxAZ2R5bZ1m8AxE=
94+
github.com/fluxcd/source-controller/api v0.10.0/go.mod h1:Vuw+7UqEUUOdkKBfTUPHwaQgbn6LL2FwqPDx2UAk7NE=
9395
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
9496
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
9597
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=

api/v1alpha2/git.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,32 @@ package v1alpha2
1818

1919
import (
2020
"github.com/fluxcd/pkg/apis/meta"
21+
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
2122
)
2223

2324
type GitSpec struct {
2425
// Checkout gives the parameters for cloning the git repository,
25-
// ready to make changes.
26-
// +required
27-
Checkout GitCheckoutSpec `json:"checkout"`
26+
// ready to make changes. If not present, the `spec.ref` field from the
27+
// referenced `GitRepository` or its default will be used.
28+
// +optional
29+
Checkout *GitCheckoutSpec `json:"checkout,omitempty"`
2830

2931
// Commit specifies how to commit to the git repository.
3032
// +required
3133
Commit CommitSpec `json:"commit"`
3234

3335
// Push specifies how and where to push commits made by the
3436
// automation. If missing, commits are pushed (back) to
35-
// `.spec.checkout.branch`.
37+
// `.spec.checkout.branch` or its default.
3638
// +optional
3739
Push *PushSpec `json:"push,omitempty"`
3840
}
3941

4042
type GitCheckoutSpec struct {
41-
// Ref gives the branch to clone from the git repository. If
42-
// `.spec.push` is not supplied, commits will also be pushed to
43-
// this branch.
43+
// Reference gives a branch, tag or commit to clone from the Git
44+
// repository.
4445
// +required
45-
Branch string `json:"branch"`
46+
Reference sourcev1.GitRepositoryRef `json:"ref"`
4647
}
4748

4849
// CommitSpec specifies how to commit changes to the git repository

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,33 @@ spec:
279279
properties:
280280
checkout:
281281
description: Checkout gives the parameters for cloning the git
282-
repository, ready to make changes.
282+
repository, ready to make changes. If not present, the `spec.ref`
283+
field from the referenced `GitRepository` or its default will
284+
be used.
283285
properties:
284-
branch:
285-
description: Ref gives the branch to clone from the git repository.
286-
If `.spec.push` is not supplied, commits will also be pushed
287-
to this branch.
288-
type: string
286+
ref:
287+
description: Reference gives a branch, tag or commit to clone
288+
from the Git repository.
289+
properties:
290+
branch:
291+
default: master
292+
description: The Git branch to checkout, defaults to master.
293+
type: string
294+
commit:
295+
description: The Git commit SHA to checkout, if specified
296+
Tag filters will be ignored.
297+
type: string
298+
semver:
299+
description: The Git tag semver expression, takes precedence
300+
over Tag.
301+
type: string
302+
tag:
303+
description: The Git tag to checkout, takes precedence
304+
over Branch.
305+
type: string
306+
type: object
289307
required:
290-
- branch
308+
- ref
291309
type: object
292310
commit:
293311
description: Commit specifies how to commit to the git repository.
@@ -335,7 +353,7 @@ spec:
335353
push:
336354
description: Push specifies how and where to push commits made
337355
by the automation. If missing, commits are pushed (back) to
338-
`.spec.checkout.branch`.
356+
`.spec.checkout.branch` or its default.
339357
properties:
340358
branch:
341359
description: Branch specifies that commits should be pushed
@@ -346,7 +364,6 @@ spec:
346364
- branch
347365
type: object
348366
required:
349-
- checkout
350367
- commit
351368
type: object
352369
interval:

controllers/imageupdateautomation_controller.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
179179

180180
log.V(debug).Info("found git repository", "gitrepository", originName)
181181

182+
// validate the git spec and default any values needed later, before proceeding
183+
var ref *sourcev1.GitRepositoryRef
184+
if gitSpec.Checkout != nil {
185+
ref = &gitSpec.Checkout.Reference
186+
} else if r := origin.Spec.Reference; r != nil {
187+
ref = r
188+
} // else remain as `nil`, which is an acceptable value for cloneInto, later.
189+
190+
var pushBranch string
191+
if gitSpec.Push != nil {
192+
pushBranch = gitSpec.Push.Branch
193+
} else {
194+
// Here's where it gets constrained. If there's no push branch
195+
// given, then the checkout ref must include a branch, and
196+
// that can be used.
197+
if ref.Branch == "" {
198+
failWithError(fmt.Errorf("Push branch not given explicitly, and cannot be inferred from .spec.git.checkout.ref or GitRepository .spec.ref"))
199+
}
200+
pushBranch = ref.Branch
201+
}
202+
182203
tmp, err := ioutil.TempDir("", fmt.Sprintf("%s-%s", originName.Namespace, originName.Name))
183204
if err != nil {
184205
return failWithError(err)
@@ -193,15 +214,14 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
193214
}
194215

195216
var repo *gogit.Repository
196-
if repo, err = cloneInto(ctx, access, gitSpec.Checkout.Branch, tmp, origin.Spec.GitImplementation); err != nil {
217+
if repo, err = cloneInto(ctx, access, ref, tmp, origin.Spec.GitImplementation); err != nil {
197218
return failWithError(err)
198219
}
199220

200221
// When there's a push spec, the pushed-to branch is where commits
201222
// shall be made
202223

203224
if gitSpec.Push != nil {
204-
pushBranch := gitSpec.Push.Branch
205225
if err := fetch(ctx, tmp, repo, pushBranch, access, origin.Spec.GitImplementation); err != nil && err != errRemoteBranchMissing {
206226
return failWithError(err)
207227
}
@@ -210,7 +230,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
210230
}
211231
}
212232

213-
log.V(debug).Info("cloned git repository", "gitrepository", originName, "branch", gitSpec.Checkout.Branch, "working", tmp)
233+
log.V(debug).Info("cloned git repository", "gitrepository", originName, "ref", ref, "working", tmp)
214234

215235
manifestsPath := tmp
216236
if auto.Spec.Update.Path != "" {
@@ -287,10 +307,6 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
287307
return failWithError(err)
288308
}
289309
} else {
290-
pushBranch := gitSpec.Checkout.Branch
291-
if gitSpec.Push != nil {
292-
pushBranch = gitSpec.Push.Branch
293-
}
294310
if err := push(ctx, tmp, repo, pushBranch, access, origin.Spec.GitImplementation); err != nil {
295311
return failWithError(err)
296312
}
@@ -451,14 +467,12 @@ func (r repoAccess) remoteCallbacks() libgit2.RemoteCallbacks {
451467
}
452468
}
453469

454-
// cloneInto clones the upstream repository at the `branch` given,
455-
// using the git library indicated by `impl`. It returns a
456-
// `*gogit.Repository` regardless of the git library, since that is
457-
// used for committing changes.
458-
func cloneInto(ctx context.Context, access repoAccess, branch, path, impl string) (*gogit.Repository, error) {
459-
checkoutStrat, err := gitstrat.CheckoutStrategyForRef(&sourcev1.GitRepositoryRef{
460-
Branch: branch,
461-
}, impl)
470+
// cloneInto clones the upstream repository at the `ref` given (which
471+
// can be `nil`), using the git library indicated by `impl`. It
472+
// returns a `*gogit.Repository` regardless of the git library, since
473+
// that is used for committing changes.
474+
func cloneInto(ctx context.Context, access repoAccess, ref *sourcev1.GitRepositoryRef, path, impl string) (*gogit.Repository, error) {
475+
checkoutStrat, err := gitstrat.CheckoutStrategyForRef(ref, impl)
462476
if err == nil {
463477
_, _, err = checkoutStrat.Checkout(ctx, path, access.url, access.auth)
464478
}

controllers/update_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ Images:
242242
Name: gitRepoKey.Name,
243243
},
244244
GitSpec: &imagev1.GitSpec{
245-
Checkout: imagev1.GitCheckoutSpec{
246-
Branch: branch,
245+
Checkout: &imagev1.GitCheckoutSpec{
246+
Reference: sourcev1.GitRepositoryRef{
247+
Branch: branch,
248+
},
247249
},
248250
Commit: imagev1.CommitSpec{
249251
MessageTemplate: commitTemplate,
@@ -384,8 +386,10 @@ Images:
384386
Name: gitRepoKey.Name,
385387
},
386388
GitSpec: &imagev1.GitSpec{
387-
Checkout: imagev1.GitCheckoutSpec{
388-
Branch: branch,
389+
Checkout: &imagev1.GitCheckoutSpec{
390+
Reference: sourcev1.GitRepositoryRef{
391+
Branch: branch,
392+
},
389393
},
390394
Commit: imagev1.CommitSpec{
391395
Author: imagev1.CommitUser{
@@ -529,8 +533,10 @@ Images:
529533
},
530534
Interval: metav1.Duration{Duration: 2 * time.Hour}, // this is to ensure any subsequent run should be outside the scope of the testing
531535
GitSpec: &imagev1.GitSpec{
532-
Checkout: imagev1.GitCheckoutSpec{
533-
Branch: branch,
536+
Checkout: &imagev1.GitCheckoutSpec{
537+
Reference: sourcev1.GitRepositoryRef{
538+
Branch: branch,
539+
},
534540
},
535541
Commit: imagev1.CommitSpec{
536542
SigningKey: &imagev1.SigningKey{
@@ -728,8 +734,10 @@ Images:
728734
},
729735
Interval: metav1.Duration{Duration: 2 * time.Hour},
730736
GitSpec: &imagev1.GitSpec{
731-
Checkout: imagev1.GitCheckoutSpec{
732-
Branch: branch,
737+
Checkout: &imagev1.GitCheckoutSpec{
738+
Reference: sourcev1.GitRepositoryRef{
739+
Branch: branch,
740+
},
733741
},
734742
Commit: imagev1.CommitSpec{
735743
Author: imagev1.CommitUser{
@@ -820,8 +828,10 @@ Images:
820828
Strategy: imagev1.UpdateStrategySetters,
821829
},
822830
GitSpec: &imagev1.GitSpec{
823-
Checkout: imagev1.GitCheckoutSpec{
824-
Branch: branch,
831+
Checkout: &imagev1.GitCheckoutSpec{
832+
Reference: sourcev1.GitRepositoryRef{
833+
Branch: branch,
834+
},
825835
},
826836
Commit: imagev1.CommitSpec{
827837
Author: imagev1.CommitUser{
@@ -938,8 +948,10 @@ Images:
938948
},
939949
Interval: metav1.Duration{Duration: 2 * time.Hour}, // this is to ensure any subsequent run should be outside the scope of the testing
940950
GitSpec: &imagev1.GitSpec{
941-
Checkout: imagev1.GitCheckoutSpec{
942-
Branch: branch,
951+
Checkout: &imagev1.GitCheckoutSpec{
952+
Reference: sourcev1.GitRepositoryRef{
953+
Branch: branch,
954+
},
943955
},
944956
// leave Update field out
945957
Commit: imagev1.CommitSpec{

0 commit comments

Comments
 (0)