Skip to content

Commit 556f03a

Browse files
authored
Merge pull request #1036 from immanuwell/fix/propagate-git-repo-fetch-errors
fix: propagate non-not-found errors when fetching GitRepository
2 parents c1bda86 + 568057f commit 556f03a

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

internal/source/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func buildGitConfig(ctx context.Context, c client.Client, originKey, srcKey type
7676
if client.IgnoreNotFound(err) == nil {
7777
return nil, fmt.Errorf("referenced git repository does not exist: %w", err)
7878
}
79+
return nil, fmt.Errorf("failed to get referenced git repository: %w", err)
7980
}
8081
cfg.url = repo.Spec.URL
8182

@@ -89,7 +90,6 @@ func buildGitConfig(ctx context.Context, c client.Client, originKey, srcKey type
8990
// Get the checkout ref for the source, prioritizing the image automation
9091
// object gitSpec checkout reference and falling back to the GitRepository
9192
// reference if not provided.
92-
// var checkoutRef *sourcev1.GitRepositoryRef
9393
if gitSpec.Checkout != nil {
9494
cfg.checkoutRef = &gitSpec.Checkout.Reference
9595
} else if repo.Spec.Reference != nil {

internal/source/git_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/client-go/kubernetes/scheme"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
32+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3233

3334
"github.com/fluxcd/pkg/apis/meta"
3435
"github.com/fluxcd/pkg/auth/aws"
@@ -405,7 +406,9 @@ func Test_buildGitConfig(t *testing.T) {
405406
gitRepoURL string
406407
gitRepoProxyData map[string][]byte
407408
srcOpts SourceOptions
409+
injectGetError error
408410
wantErr bool
411+
wantErrContains string
409412
wantCheckoutRef *sourcev1.GitRepositoryRef
410413
wantPushBranch string
411414
wantSwitchBranch bool
@@ -527,6 +530,20 @@ func Test_buildGitConfig(t *testing.T) {
527530
gitSpec: &imagev1.GitSpec{},
528531
wantErr: true,
529532
},
533+
{
534+
name: "transient error fetching gitRepo",
535+
gitSpec: &imagev1.GitSpec{
536+
Checkout: &imagev1.GitCheckoutSpec{
537+
Reference: sourcev1.GitRepositoryRef{Branch: "main"},
538+
},
539+
Push: &imagev1.PushSpec{Branch: "main"},
540+
},
541+
gitRepoName: testGitRepoName,
542+
gitRepoURL: testGitURL,
543+
injectGetError: fmt.Errorf("transient-test-error"),
544+
wantErr: true,
545+
wantErrContains: "transient-test-error",
546+
},
530547
{
531548
name: "use gitrepo timeout",
532549
gitSpec: &imagev1.GitSpec{},
@@ -616,6 +633,17 @@ func Test_buildGitConfig(t *testing.T) {
616633
clientBuilder := fakeclient.NewClientBuilder().
617634
WithScheme(scheme.Scheme).
618635
WithObjects(testObjects...)
636+
if tt.injectGetError != nil {
637+
injectErr := tt.injectGetError
638+
clientBuilder = clientBuilder.WithInterceptorFuncs(interceptor.Funcs{
639+
Get: func(ctx context.Context, cl client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
640+
if _, ok := obj.(*sourcev1.GitRepository); ok {
641+
return injectErr
642+
}
643+
return cl.Get(ctx, key, obj, opts...)
644+
},
645+
})
646+
}
619647
c := clientBuilder.Build()
620648

621649
gitRepoKey := types.NamespacedName{
@@ -633,6 +661,9 @@ func Test_buildGitConfig(t *testing.T) {
633661
g.Fail(fmt.Sprintf("unexpected error: %v", err))
634662
return
635663
}
664+
if tt.wantErrContains != "" {
665+
g.Expect(err.Error()).To(ContainSubstring(tt.wantErrContains))
666+
}
636667
if err == nil {
637668
g.Expect(gitSrcCfg.checkoutRef).To(Equal(tt.wantCheckoutRef), "unexpected checkoutRef")
638669
g.Expect(gitSrcCfg.pushBranch).To(Equal(tt.wantPushBranch), "unexpected push branch")

0 commit comments

Comments
 (0)