@@ -24,7 +24,6 @@ import (
2424 "fmt"
2525 "net/url"
2626
27- "github.com/fluxcd/pkg/oci"
2827 "github.com/google/go-containerregistry/pkg/authn"
2928 "github.com/google/go-containerregistry/pkg/authn/k8schain"
3029 helmgetter "helm.sh/helm/v3/pkg/getter"
@@ -33,6 +32,7 @@ import (
3332 "k8s.io/apimachinery/pkg/types"
3433 "sigs.k8s.io/controller-runtime/pkg/client"
3534
35+ "github.com/fluxcd/pkg/oci"
3636 helmv1 "github.com/fluxcd/source-controller/api/v1beta2"
3737 "github.com/fluxcd/source-controller/internal/helm/registry"
3838 soci "github.com/fluxcd/source-controller/internal/oci"
@@ -82,15 +82,6 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *helmv1.HelmReposit
8282 var authSecret * corev1.Secret
8383 var deprecatedTLSConfig bool
8484
85- if ociRepo {
86- if obj .Spec .ServiceAccountName != "" {
87- hrOpts .Keychain , err = getKeychainFromSAImagePullSecrets (ctx , c , obj .GetNamespace (), obj .Spec .ServiceAccountName )
88- if err != nil {
89- return nil , fmt .Errorf ("failed to get keychain from service account: %w" , err )
90- }
91- }
92- }
93-
9485 if obj .Spec .SecretRef != nil {
9586 authSecret , err = fetchSecret (ctx , c , obj .Spec .SecretRef .Name , obj .GetNamespace ())
9687 if err != nil {
@@ -118,28 +109,43 @@ func GetClientOpts(ctx context.Context, c client.Client, obj *helmv1.HelmReposit
118109 }
119110
120111 if ociRepo {
121- keychain , err : = registry .LoginOptionFromSecret (url , * authSecret )
112+ hrOpts . Keychain , err = registry .LoginOptionFromSecret (url , * authSecret )
122113 if err != nil {
123114 return nil , fmt .Errorf ("failed to configure login options: %w" , err )
124115 }
116+ }
117+ }
118+
119+ if ociRepo {
120+ if obj .Spec .ServiceAccountName != "" {
121+ keychain , err := getKeychainFromSAImagePullSecrets (ctx , c , obj .GetNamespace (), obj .Spec .ServiceAccountName )
122+ if err != nil {
123+ return nil , fmt .Errorf ("failed to get keychain from service account: %w" , err )
124+ }
125125
126126 if hrOpts .Keychain != nil {
127- hrOpts .Keychain = authn .NewMultiKeychain (keychain , hrOpts .Keychain )
127+ hrOpts .Keychain = authn .NewMultiKeychain (hrOpts .Keychain , keychain )
128128 } else {
129129 hrOpts .Keychain = keychain
130130 }
131131 }
132- } else if obj .Spec .Provider != helmv1 .GenericOCIProvider && obj .Spec .Type == helmv1 .HelmRepositoryTypeOCI && ociRepo {
133- authenticator , authErr := soci .OIDCAuth (ctx , obj .Spec .URL , obj .Spec .Provider )
134- if authErr != nil && ! errors .Is (authErr , oci .ErrUnconfiguredProvider ) {
135- return nil , fmt .Errorf ("failed to get credential from '%s': %w" , obj .Spec .Provider , authErr )
132+
133+ var hasKeychain bool
134+ if hrOpts .Keychain != nil {
135+ _ , ok := hrOpts .Keychain .(soci.Anonymous )
136+ hasKeychain = ! ok
136137 }
137- if authenticator != nil {
138- hrOpts .Authenticator = authenticator
138+
139+ if ! hasKeychain && obj .Spec .Provider != helmv1 .GenericOCIProvider {
140+ authenticator , authErr := soci .OIDCAuth (ctx , obj .Spec .URL , obj .Spec .Provider )
141+ if authErr != nil && ! errors .Is (authErr , oci .ErrUnconfiguredProvider ) {
142+ return nil , fmt .Errorf ("failed to get credential from '%s': %w" , obj .Spec .Provider , authErr )
143+ }
144+ if authenticator != nil {
145+ hrOpts .Authenticator = authenticator
146+ }
139147 }
140- }
141148
142- if ociRepo {
143149 hrOpts .RegLoginOpt , err = registry .NewLoginOption (hrOpts .Authenticator , hrOpts .Keychain , url )
144150 if err != nil {
145151 return nil , err
0 commit comments