@@ -54,7 +54,6 @@ import (
54
54
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
55
55
"github.com/fluxcd/pkg/apis/meta"
56
56
"github.com/fluxcd/pkg/git"
57
- "github.com/fluxcd/pkg/oci"
58
57
"github.com/fluxcd/pkg/runtime/conditions"
59
58
helper "github.com/fluxcd/pkg/runtime/controller"
60
59
"github.com/fluxcd/pkg/runtime/patch"
@@ -68,7 +67,6 @@ import (
68
67
serror "github.com/fluxcd/source-controller/internal/error"
69
68
"github.com/fluxcd/source-controller/internal/helm/chart"
70
69
"github.com/fluxcd/source-controller/internal/helm/getter"
71
- "github.com/fluxcd/source-controller/internal/helm/registry"
72
70
"github.com/fluxcd/source-controller/internal/helm/repository"
73
71
soci "github.com/fluxcd/source-controller/internal/oci"
74
72
sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
@@ -506,11 +504,6 @@ func (r *HelmChartReconciler) reconcileSource(ctx context.Context, sp *patch.Ser
506
504
// object, and returns early.
507
505
func (r * HelmChartReconciler ) buildFromHelmRepository (ctx context.Context , obj * helmv1.HelmChart ,
508
506
repo * helmv1.HelmRepository , b * chart.Build ) (sreconcile.Result , error ) {
509
- var (
510
- tlsConfig * tls.Config
511
- authenticator authn.Authenticator
512
- keychain authn.Keychain
513
- )
514
507
// Used to login with the repository declared provider
515
508
ctxTimeout , cancel := context .WithTimeout (ctx , repo .Spec .Timeout .Duration )
516
509
defer cancel ()
@@ -519,64 +512,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
519
512
if err != nil {
520
513
return chartRepoConfigErrorReturn (err , obj )
521
514
}
522
- // Construct the Getter options from the HelmRepository data
523
- clientOpts := []helmgetter.Option {
524
- helmgetter .WithURL (normalizedURL ),
525
- helmgetter .WithTimeout (repo .Spec .Timeout .Duration ),
526
- helmgetter .WithPassCredentialsAll (repo .Spec .PassCredentials ),
527
- }
528
- if secret , err := r .getHelmRepositorySecret (ctx , repo ); secret != nil || err != nil {
529
- if err != nil {
530
- e := & serror.Event {
531
- Err : fmt .Errorf ("failed to get secret '%s': %w" , repo .Spec .SecretRef .Name , err ),
532
- Reason : sourcev1 .AuthenticationFailedReason ,
533
- }
534
- conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , e .Err .Error ())
535
- // Return error as the world as observed may change
536
- return sreconcile .ResultEmpty , e
537
- }
538
-
539
- // Build client options from secret
540
- opts , tlsCfg , err := r .clientOptionsFromSecret (secret , normalizedURL )
541
- if err != nil {
542
- e := & serror.Event {
543
- Err : err ,
544
- Reason : sourcev1 .AuthenticationFailedReason ,
545
- }
546
- conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , e .Err .Error ())
547
- // Requeue as content of secret might change
548
- return sreconcile .ResultEmpty , e
549
- }
550
- clientOpts = append (clientOpts , opts ... )
551
- tlsConfig = tlsCfg
552
-
553
- // Build registryClient options from secret
554
- keychain , err = registry .LoginOptionFromSecret (normalizedURL , * secret )
555
- if err != nil {
556
- e := & serror.Event {
557
- Err : fmt .Errorf ("failed to configure Helm client with secret data: %w" , err ),
558
- Reason : sourcev1 .AuthenticationFailedReason ,
559
- }
560
- conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , e .Err .Error ())
561
- // Requeue as content of secret might change
562
- return sreconcile .ResultEmpty , e
563
- }
564
- } else if repo .Spec .Provider != helmv1 .GenericOCIProvider && repo .Spec .Type == helmv1 .HelmRepositoryTypeOCI {
565
- auth , authErr := oidcAuth (ctxTimeout , repo .Spec .URL , repo .Spec .Provider )
566
- if authErr != nil && ! errors .Is (authErr , oci .ErrUnconfiguredProvider ) {
567
- e := & serror.Event {
568
- Err : fmt .Errorf ("failed to get credential from %s: %w" , repo .Spec .Provider , authErr ),
569
- Reason : sourcev1 .AuthenticationFailedReason ,
570
- }
571
- conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , e .Err .Error ())
572
- return sreconcile .ResultEmpty , e
573
- }
574
- if auth != nil {
575
- authenticator = auth
576
- }
577
- }
578
-
579
- loginOpt , err := makeLoginOption (authenticator , keychain , normalizedURL )
515
+ hcOpts , err := repository .GetHelmClientOpts (ctxTimeout , r .Client , repo , normalizedURL , true )
580
516
if err != nil {
581
517
e := & serror.Event {
582
518
Err : err ,
@@ -585,6 +521,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
585
521
conditions .MarkTrue (obj , sourcev1 .FetchFailedCondition , e .Reason , e .Err .Error ())
586
522
return sreconcile .ResultEmpty , e
587
523
}
524
+ clientOpts := hcOpts .GetterOpts
588
525
589
526
// Initialize the chart repository
590
527
var chartRepo repository.Downloader
@@ -599,7 +536,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
599
536
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
600
537
// TODO@souleb: remove this once the registry move to Oras v2
601
538
// or rework to enable reusing credentials to avoid the unneccessary handshake operations
602
- registryClient , credentialsFile , err := r .RegistryClientGenerator (loginOpt != nil )
539
+ registryClient , credentialsFile , err := r .RegistryClientGenerator (hcOpts . LoginOpt != nil )
603
540
if err != nil {
604
541
e := & serror.Event {
605
542
Err : fmt .Errorf ("failed to construct Helm client: %w" , err ),
@@ -621,7 +558,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
621
558
var verifiers []soci.Verifier
622
559
if obj .Spec .Verify != nil {
623
560
provider := obj .Spec .Verify .Provider
624
- verifiers , err = r .makeVerifiers (ctx , obj , authenticator , keychain )
561
+ verifiers , err = r .makeVerifiers (ctx , obj , hcOpts . Authenticator , hcOpts . Keychain )
625
562
if err != nil {
626
563
if obj .Spec .Verify .SecretRef == nil {
627
564
provider = fmt .Sprintf ("%s keyless" , provider )
@@ -645,12 +582,11 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
645
582
if err != nil {
646
583
return chartRepoConfigErrorReturn (err , obj )
647
584
}
648
- chartRepo = ociChartRepo
649
585
650
586
// If login options are configured, use them to login to the registry
651
587
// The OCIGetter will later retrieve the stored credentials to pull the chart
652
- if loginOpt != nil {
653
- err = ociChartRepo .Login (loginOpt )
588
+ if hcOpts . LoginOpt != nil {
589
+ err = ociChartRepo .Login (hcOpts . LoginOpt )
654
590
if err != nil {
655
591
e := & serror.Event {
656
592
Err : fmt .Errorf ("failed to login to OCI registry: %w" , err ),
@@ -660,8 +596,9 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
660
596
return sreconcile .ResultEmpty , e
661
597
}
662
598
}
599
+ chartRepo = ociChartRepo
663
600
default :
664
- httpChartRepo , err := repository .NewChartRepository (normalizedURL , r .Storage .LocalPath (* repo .GetArtifact ()), r .Getters , tlsConfig , clientOpts ... )
601
+ httpChartRepo , err := repository .NewChartRepository (normalizedURL , r .Storage .LocalPath (* repo .GetArtifact ()), r .Getters , hcOpts . TlsConfig , clientOpts ... )
665
602
if err != nil {
666
603
return chartRepoConfigErrorReturn (err , obj )
667
604
}
@@ -1024,12 +961,6 @@ func (r *HelmChartReconciler) garbageCollect(ctx context.Context, obj *helmv1.He
1024
961
// The callback returns an object with a state, so the caller has to do the necessary cleanup.
1025
962
func (r * HelmChartReconciler ) namespacedChartRepositoryCallback (ctx context.Context , name , namespace string ) chart.GetChartDownloaderCallback {
1026
963
return func (url string ) (repository.Downloader , error ) {
1027
- var (
1028
- tlsConfig * tls.Config
1029
- authenticator authn.Authenticator
1030
- keychain authn.Keychain
1031
- )
1032
-
1033
964
normalizedURL , err := repository .NormalizeURL (url )
1034
965
if err != nil {
1035
966
return nil , err
@@ -1047,55 +978,26 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
1047
978
},
1048
979
}
1049
980
}
981
+ objKey := types.NamespacedName {
982
+ Name : obj .Name ,
983
+ Namespace : obj .Namespace ,
984
+ }
1050
985
1051
986
// Used to login with the repository declared provider
1052
987
ctxTimeout , cancel := context .WithTimeout (ctx , obj .Spec .Timeout .Duration )
1053
988
defer cancel ()
1054
989
1055
- clientOpts := []helmgetter.Option {
1056
- helmgetter .WithURL (normalizedURL ),
1057
- helmgetter .WithTimeout (obj .Spec .Timeout .Duration ),
1058
- helmgetter .WithPassCredentialsAll (obj .Spec .PassCredentials ),
1059
- }
1060
- if secret , err := r .getHelmRepositorySecret (ctx , obj ); secret != nil || err != nil {
1061
- if err != nil {
1062
- return nil , err
1063
- }
1064
-
1065
- // Build client options from secret
1066
- opts , tlsCfg , err := r .clientOptionsFromSecret (secret , normalizedURL )
1067
- if err != nil {
1068
- return nil , err
1069
- }
1070
- clientOpts = append (clientOpts , opts ... )
1071
- tlsConfig = tlsCfg
1072
-
1073
- // Build registryClient options from secret
1074
- keychain , err = registry .LoginOptionFromSecret (normalizedURL , * secret )
1075
- if err != nil {
1076
- return nil , fmt .Errorf ("failed to create login options for HelmRepository '%s': %w" , obj .Name , err )
1077
- }
1078
-
1079
- } else if obj .Spec .Provider != helmv1 .GenericOCIProvider && obj .Spec .Type == helmv1 .HelmRepositoryTypeOCI {
1080
- auth , authErr := oidcAuth (ctxTimeout , obj .Spec .URL , obj .Spec .Provider )
1081
- if authErr != nil && ! errors .Is (authErr , oci .ErrUnconfiguredProvider ) {
1082
- return nil , fmt .Errorf ("failed to get credential from %s: %w" , obj .Spec .Provider , authErr )
1083
- }
1084
- if auth != nil {
1085
- authenticator = auth
1086
- }
1087
- }
1088
-
1089
- loginOpt , err := makeLoginOption (authenticator , keychain , normalizedURL )
990
+ hcOpts , err := repository .GetHelmClientOpts (ctxTimeout , r .Client , obj , normalizedURL , true )
1090
991
if err != nil {
1091
992
return nil , err
1092
993
}
994
+ clientOpts := hcOpts .GetterOpts
1093
995
1094
996
var chartRepo repository.Downloader
1095
997
if helmreg .IsOCI (normalizedURL ) {
1096
- registryClient , credentialsFile , err := r .RegistryClientGenerator (loginOpt != nil )
998
+ registryClient , credentialsFile , err := r .RegistryClientGenerator (hcOpts . LoginOpt != nil )
1097
999
if err != nil {
1098
- return nil , fmt .Errorf ("failed to create registry client for HelmRepository '%s': %w" , obj . Name , err )
1000
+ return nil , fmt .Errorf ("failed to create registry client for HelmRepository '%s': %w" , objKey . String () , err )
1099
1001
}
1100
1002
1101
1003
var errs []error
@@ -1106,7 +1008,7 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
1106
1008
repository .WithOCIRegistryClient (registryClient ),
1107
1009
repository .WithCredentialsFile (credentialsFile ))
1108
1010
if err != nil {
1109
- errs = append (errs , fmt .Errorf ("failed to create OCI chart repository for HelmRepository '%s': %w" , obj . Name , err ))
1011
+ errs = append (errs , fmt .Errorf ("failed to create OCI chart repository for HelmRepository '%s': %w" , objKey . String () , err ))
1110
1012
// clean up the credentialsFile
1111
1013
if credentialsFile != "" {
1112
1014
if err := os .Remove (credentialsFile ); err != nil {
@@ -1118,10 +1020,10 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
1118
1020
1119
1021
// If login options are configured, use them to login to the registry
1120
1022
// The OCIGetter will later retrieve the stored credentials to pull the chart
1121
- if loginOpt != nil {
1122
- err = ociChartRepo .Login (loginOpt )
1023
+ if hcOpts . LoginOpt != nil {
1024
+ err = ociChartRepo .Login (hcOpts . LoginOpt )
1123
1025
if err != nil {
1124
- errs = append (errs , fmt .Errorf ("failed to login to OCI chart repository for HelmRepository '%s': %w" , obj . Name , err ))
1026
+ errs = append (errs , fmt .Errorf ("failed to login to OCI chart repository for HelmRepository '%s': %w" , objKey . String () , err ))
1125
1027
// clean up the credentialsFile
1126
1028
errs = append (errs , ociChartRepo .Clear ())
1127
1029
return nil , kerrors .NewAggregate (errs )
@@ -1130,7 +1032,7 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
1130
1032
1131
1033
chartRepo = ociChartRepo
1132
1034
} else {
1133
- httpChartRepo , err := repository .NewChartRepository (normalizedURL , "" , r .Getters , tlsConfig , clientOpts ... )
1035
+ httpChartRepo , err := repository .NewChartRepository (normalizedURL , "" , r .Getters , hcOpts . TlsConfig , clientOpts ... )
1134
1036
if err != nil {
1135
1037
return nil , err
1136
1038
}
0 commit comments