Skip to content

Commit b299a9d

Browse files
committed
Pass context to libgit2.RemoteCallbacks
Pass cancellable context to libgit2.RemoteCallbacks to be able to cancel the remote operations when the context is cancelled. For git clone, fetch and push, a context is created with the timeout of the target GitRepository. Signed-off-by: Sunny <[email protected]>
1 parent cb53e35 commit b299a9d

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

api/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.16
44

55
require (
66
github.com/fluxcd/pkg/apis/meta v0.10.0
7-
github.com/fluxcd/source-controller/api v0.17.0
7+
github.com/fluxcd/source-controller/api v0.17.2
88
k8s.io/apimachinery v0.21.3
99
sigs.k8s.io/controller-runtime v0.9.5
1010
)

api/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/evanphx/json-patch v4.11.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQL
9393
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
9494
github.com/fluxcd/pkg/apis/meta v0.10.0 h1:N7wVGHC1cyPdT87hrDC7UwCwRwnZdQM46PBSLjG2rlE=
9595
github.com/fluxcd/pkg/apis/meta v0.10.0/go.mod h1:CW9X9ijMTpNe7BwnokiUOrLl/h13miwVr/3abEQLbKE=
96-
github.com/fluxcd/source-controller/api v0.17.0 h1:skXx2H5SeziUTwJrp9MPJNwTtYTctJMQ7ZIJfLmg9b0=
97-
github.com/fluxcd/source-controller/api v0.17.0/go.mod h1:guUCCapjzE2kocwFreQTM/IGvtAglIJc4L97mokairo=
96+
github.com/fluxcd/source-controller/api v0.17.2 h1:noePJGsevuvxWols6ErbowujuAHGWb/ZO8irtRHcVAc=
97+
github.com/fluxcd/source-controller/api v0.17.2/go.mod h1:guUCCapjzE2kocwFreQTM/IGvtAglIJc4L97mokairo=
9898
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
9999
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
100100
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ spec:
220220
description: Reference gives a branch, tag or commit to clone from the Git repository.
221221
properties:
222222
branch:
223-
default: master
224223
description: The Git branch to checkout, defaults to master.
225224
type: string
226225
commit:
@@ -427,7 +426,6 @@ spec:
427426
description: Reference gives a branch, tag or commit to clone from the Git repository.
428427
properties:
429428
branch:
430-
default: master
431429
description: The Git branch to checkout, defaults to master.
432430
type: string
433431
commit:

controllers/imageupdateautomation_controller.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,22 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
227227
return failWithError(err)
228228
}
229229

230+
// Use the git operations timeout for the repo.
231+
cloneCtx, cancel := context.WithTimeout(ctx, origin.Spec.Timeout.Duration)
232+
defer cancel()
230233
var repo *gogit.Repository
231-
if repo, err = cloneInto(ctx, access, ref, tmp); err != nil {
234+
if repo, err = cloneInto(cloneCtx, access, ref, tmp); err != nil {
232235
return failWithError(err)
233236
}
234237

235238
// When there's a push spec, the pushed-to branch is where commits
236239
// shall be made
237240

238241
if gitSpec.Push != nil {
239-
if err := fetch(ctx, tmp, pushBranch, access); err != nil && err != errRemoteBranchMissing {
242+
// Use the git operations timeout for the repo.
243+
fetchCtx, cancel := context.WithTimeout(ctx, origin.Spec.Timeout.Duration)
244+
defer cancel()
245+
if err := fetch(fetchCtx, tmp, pushBranch, access); err != nil && err != errRemoteBranchMissing {
240246
return failWithError(err)
241247
}
242248
if err = switchBranch(repo, pushBranch); err != nil {
@@ -320,7 +326,10 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
320326
return failWithError(err)
321327
}
322328
} else {
323-
if err := push(ctx, tmp, pushBranch, access); err != nil {
329+
// Use the git operations timeout for the repo.
330+
pushCtx, cancel := context.WithTimeout(ctx, origin.Spec.Timeout.Duration)
331+
defer cancel()
332+
if err := push(pushCtx, tmp, pushBranch, access); err != nil {
324333
return failWithError(err)
325334
}
326335

@@ -475,8 +484,8 @@ func (r *ImageUpdateAutomationReconciler) getRepoAccess(ctx context.Context, rep
475484
return access, nil
476485
}
477486

478-
func (r repoAccess) remoteCallbacks() libgit2.RemoteCallbacks {
479-
return gitlibgit2.RemoteCallbacks(r.auth)
487+
func (r repoAccess) remoteCallbacks(ctx context.Context) libgit2.RemoteCallbacks {
488+
return gitlibgit2.RemoteCallbacks(ctx, r.auth)
480489
}
481490

482491
// cloneInto clones the upstream repository at the `ref` given (which
@@ -637,7 +646,7 @@ func fetch(ctx context.Context, path string, branch string, access repoAccess) e
637646
err = origin.Fetch(
638647
[]string{refspec},
639648
&libgit2.FetchOptions{
640-
RemoteCallbacks: access.remoteCallbacks(),
649+
RemoteCallbacks: access.remoteCallbacks(ctx),
641650
}, "",
642651
)
643652
if err != nil && libgit2.IsErrorCode(err, libgit2.ErrorCodeNotFound) {
@@ -662,7 +671,7 @@ func push(ctx context.Context, path, branch string, access repoAccess) error {
662671
}
663672
defer origin.Free()
664673

665-
callbacks := access.remoteCallbacks()
674+
callbacks := access.remoteCallbacks(ctx)
666675

667676
// calling repo.Push will succeed even if a reference update is
668677
// rejected; to detect this case, this callback is supplied.

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ require (
1212
// If you bump this, change REFLECTOR_VER in the Makefile to match
1313
github.com/fluxcd/image-reflector-controller/api v0.12.0
1414
github.com/fluxcd/pkg/apis/meta v0.10.1
15-
github.com/fluxcd/pkg/gittestserver v0.4.1
15+
github.com/fluxcd/pkg/gittestserver v0.4.2
1616
github.com/fluxcd/pkg/runtime v0.12.1
1717
github.com/fluxcd/pkg/ssh v0.1.0
1818
// If you bump this, change SOURCE_VER in the Makefile to match
19-
github.com/fluxcd/source-controller v0.17.0
20-
github.com/fluxcd/source-controller/api v0.17.0
19+
github.com/fluxcd/source-controller v0.17.2
20+
github.com/fluxcd/source-controller/api v0.17.2
2121
github.com/go-git/go-billy/v5 v5.3.1
2222
github.com/go-git/go-git/v5 v5.4.2
2323
github.com/go-logr/logr v0.4.0

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ github.com/fluxcd/image-reflector-controller/api v0.12.0/go.mod h1:lgQHGFz29OHmD
351351
github.com/fluxcd/pkg/apis/meta v0.10.0/go.mod h1:CW9X9ijMTpNe7BwnokiUOrLl/h13miwVr/3abEQLbKE=
352352
github.com/fluxcd/pkg/apis/meta v0.10.1 h1:zISenRlqNG7WK8TP3HxZTvv+1Z7JZOUIQvZrOr6pQ2w=
353353
github.com/fluxcd/pkg/apis/meta v0.10.1/go.mod h1:yUblM2vg+X8TE3A2VvJfdhkGmg+uqBlSPkLk7dxi0UM=
354-
github.com/fluxcd/pkg/gittestserver v0.4.1 h1:knghRrVEEPnpO0VJYjoz0H2YMc4fnKAVt5hDGsB1IHc=
355-
github.com/fluxcd/pkg/gittestserver v0.4.1/go.mod h1:hUPx21fe/6oox336Wih/XF1fnmzLmptNMOvATbTZXNY=
354+
github.com/fluxcd/pkg/gittestserver v0.4.2 h1:XqoiemTnnUNldnOw8N7OTdalu2iZp1FTRhp9uUauDJQ=
355+
github.com/fluxcd/pkg/gittestserver v0.4.2/go.mod h1:hUPx21fe/6oox336Wih/XF1fnmzLmptNMOvATbTZXNY=
356356
github.com/fluxcd/pkg/gitutil v0.1.0 h1:VO3kJY/CKOCO4ysDNqfdpTg04icAKBOSb3lbR5uE/IE=
357357
github.com/fluxcd/pkg/gitutil v0.1.0/go.mod h1:Ybz50Ck5gkcnvF0TagaMwtlRy3X3wXuiri1HVsK5id4=
358358
github.com/fluxcd/pkg/helmtestserver v0.2.0/go.mod h1:Yie8n7xuu5Nvf1Q7302LKsubJhWpwzCaK0rLJvmF7aI=
@@ -366,10 +366,10 @@ github.com/fluxcd/pkg/testserver v0.1.0/go.mod h1:fvt8BHhXw6c1+CLw1QFZxcQprlcXzs
366366
github.com/fluxcd/pkg/untar v0.1.0/go.mod h1:aGswNyzB1mlz/T/kpOS58mITBMxMKc9tlJBH037A2HY=
367367
github.com/fluxcd/pkg/version v0.1.0 h1:v+SmCanmCB5Tj2Cx9TXlj+kNRfPGbAvirkeqsp7ZEAQ=
368368
github.com/fluxcd/pkg/version v0.1.0/go.mod h1:V7Z/w8dxLQzv0FHqa5ox5TeyOd2zOd49EeuWFgnwyj4=
369-
github.com/fluxcd/source-controller v0.17.0 h1:NXemBcfzZzv3OiT5mjK2vynKl0Ni1IPY5PJAsZChOfs=
370-
github.com/fluxcd/source-controller v0.17.0/go.mod h1:0vG0i0o33aviv369fHguCyG3O9nyoLFVcKNDvWl75P0=
371-
github.com/fluxcd/source-controller/api v0.17.0 h1:skXx2H5SeziUTwJrp9MPJNwTtYTctJMQ7ZIJfLmg9b0=
372-
github.com/fluxcd/source-controller/api v0.17.0/go.mod h1:guUCCapjzE2kocwFreQTM/IGvtAglIJc4L97mokairo=
369+
github.com/fluxcd/source-controller v0.17.2 h1:lPnZxXX0CshkDVHfZxz1GWu98TLrcRuk3X3f544pMjs=
370+
github.com/fluxcd/source-controller v0.17.2/go.mod h1:nEaHFlXAVbv/gNo1kWWF52y8J922MIQQUYXD/azF6f8=
371+
github.com/fluxcd/source-controller/api v0.17.2 h1:noePJGsevuvxWols6ErbowujuAHGWb/ZO8irtRHcVAc=
372+
github.com/fluxcd/source-controller/api v0.17.2/go.mod h1:guUCCapjzE2kocwFreQTM/IGvtAglIJc4L97mokairo=
373373
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
374374
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
375375
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=

0 commit comments

Comments
 (0)