@@ -35,7 +35,6 @@ import (
35
35
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
36
"k8s.io/apimachinery/pkg/runtime"
37
37
"k8s.io/apimachinery/pkg/types"
38
- kerrors "k8s.io/apimachinery/pkg/util/errors"
39
38
"k8s.io/apimachinery/pkg/util/uuid"
40
39
kuberecorder "k8s.io/client-go/tools/record"
41
40
ctrl "sigs.k8s.io/controller-runtime"
@@ -463,10 +462,9 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
463
462
loginOpts []helmreg.LoginOption
464
463
)
465
464
466
- normalizedURL := repository .NormalizeURL (repo .Spec .URL )
467
465
// Construct the Getter options from the HelmRepository data
468
466
clientOpts := []helmgetter.Option {
469
- helmgetter .WithURL (normalizedURL ),
467
+ helmgetter .WithURL (repo . Spec . URL ),
470
468
helmgetter .WithTimeout (repo .Spec .Timeout .Duration ),
471
469
helmgetter .WithPassCredentialsAll (repo .Spec .PassCredentials ),
472
470
}
@@ -494,7 +492,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
494
492
}
495
493
clientOpts = append (clientOpts , opts ... )
496
494
497
- tlsConfig , err = getter .TLSClientConfigFromSecret (* secret , normalizedURL )
495
+ tlsConfig , err = getter .TLSClientConfigFromSecret (* secret , repo . Spec . URL )
498
496
if err != nil {
499
497
e := & serror.Event {
500
498
Err : fmt .Errorf ("failed to create TLS client config with secret data: %w" , err ),
@@ -506,7 +504,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
506
504
}
507
505
508
506
// Build registryClient options from secret
509
- loginOpt , err := registry .LoginOptionFromSecret (normalizedURL , * secret )
507
+ loginOpt , err := registry .LoginOptionFromSecret (repo . Spec . URL , * secret )
510
508
if err != nil {
511
509
e := & serror.Event {
512
510
Err : fmt .Errorf ("failed to configure Helm client with secret data: %w" , err ),
@@ -521,19 +519,19 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
521
519
}
522
520
523
521
// Initialize the chart repository
524
- var chartRepo repository. Downloader
522
+ var chartRepo chart. Remote
525
523
switch repo .Spec .Type {
526
524
case sourcev1 .HelmRepositoryTypeOCI :
527
- if ! helmreg .IsOCI (normalizedURL ) {
528
- err := fmt .Errorf ("invalid OCI registry URL: %s" , normalizedURL )
525
+ if ! helmreg .IsOCI (repo . Spec . URL ) {
526
+ err := fmt .Errorf ("invalid OCI registry URL: %s" , repo . Spec . URL )
529
527
return chartRepoConfigErrorReturn (err , obj )
530
528
}
531
529
532
530
// with this function call, we create a temporary file to store the credentials if needed.
533
531
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
534
532
// TODO@souleb: remove this once the registry move to Oras v2
535
533
// or rework to enable reusing credentials to avoid the unneccessary handshake operations
536
- registryClient , credentialsFile , err := r .RegistryClientGenerator (loginOpts != nil )
534
+ registryClient , file , err := r .RegistryClientGenerator (loginOpts != nil )
537
535
if err != nil {
538
536
e := & serror.Event {
539
537
Err : fmt .Errorf ("failed to construct Helm client: %w" , err ),
@@ -543,9 +541,9 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
543
541
return sreconcile .ResultEmpty , e
544
542
}
545
543
546
- if credentialsFile != "" {
544
+ if file != "" {
547
545
defer func () {
548
- if err := os .Remove (credentialsFile ); err != nil {
546
+ if err := os .Remove (file ); err != nil {
549
547
r .eventLogf (ctx , obj , corev1 .EventTypeWarning , meta .FailedReason ,
550
548
"failed to delete temporary credentials file: %s" , err )
551
549
}
@@ -554,7 +552,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
554
552
555
553
// Tell the chart repository to use the OCI client with the configured getter
556
554
clientOpts = append (clientOpts , helmgetter .WithRegistryClient (registryClient ))
557
- ociChartRepo , err := repository .NewOCIChartRepository (normalizedURL , repository .WithOCIGetter (r .Getters ), repository .WithOCIGetterOptions (clientOpts ), repository .WithOCIRegistryClient (registryClient ))
555
+ ociChartRepo , err := repository .NewOCIChartRepository (repo . Spec . URL , repository .WithOCIGetter (r .Getters ), repository .WithOCIGetterOptions (clientOpts ), repository .WithOCIRegistryClient (registryClient ))
558
556
if err != nil {
559
557
return chartRepoConfigErrorReturn (err , obj )
560
558
}
@@ -574,7 +572,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
574
572
}
575
573
}
576
574
default :
577
- httpChartRepo , err := repository .NewChartRepository (normalizedURL , r .Storage .LocalPath (* repo .GetArtifact ()), r .Getters , tlsConfig , clientOpts ,
575
+ httpChartRepo , err := repository .NewChartRepository (repo . Spec . URL , r .Storage .LocalPath (* repo .GetArtifact ()), r .Getters , tlsConfig , clientOpts ,
578
576
repository .WithMemoryCache (r .Storage .LocalPath (* repo .GetArtifact ()), r .Cache , r .TTL , func (event string ) {
579
577
r .IncCacheEvents (event , obj .Name , obj .Namespace )
580
578
}))
@@ -687,15 +685,9 @@ func (r *HelmChartReconciler) buildFromTarballArtifact(ctx context.Context, obj
687
685
688
686
// Setup dependency manager
689
687
dm := chart .NewDependencyManager (
690
- chart .WithDownloaderCallback (r .namespacedChartRepositoryCallback (ctx , obj .GetName (), obj .GetNamespace ())),
688
+ chart .WithRepositoryCallback (r .namespacedChartRepositoryCallback (ctx , obj .GetName (), obj .GetNamespace ())),
691
689
)
692
- defer func () {
693
- err := dm .Clear ()
694
- if err != nil {
695
- r .eventLogf (ctx , obj , corev1 .EventTypeWarning , meta .FailedReason ,
696
- "dependency manager cleanup error: %s" , err )
697
- }
698
- }()
690
+ defer dm .Clear ()
699
691
700
692
// Configure builder options, including any previously cached chart
701
693
opts := chart.BuildOptions {
@@ -922,17 +914,12 @@ func (r *HelmChartReconciler) garbageCollect(ctx context.Context, obj *sourcev1.
922
914
return nil
923
915
}
924
916
925
- // namespacedChartRepositoryCallback returns a chart.GetChartDownloaderCallback scoped to the given namespace.
926
- // The returned callback returns a repository.Downloader configured with the retrieved v1beta1.HelmRepository,
917
+ // namespacedChartRepositoryCallback returns a chart.GetChartRepositoryCallback scoped to the given namespace.
918
+ // The returned callback returns a repository.ChartRepository configured with the retrieved v1beta1.HelmRepository,
927
919
// or a shim with defaults if no object could be found.
928
- // The callback returns an object with a state, so the caller has to do the necessary cleanup.
929
- func (r * HelmChartReconciler ) namespacedChartRepositoryCallback (ctx context.Context , name , namespace string ) chart.GetChartDownloaderCallback {
930
- return func (url string ) (repository.Downloader , error ) {
931
- var (
932
- tlsConfig * tls.Config
933
- loginOpts []helmreg.LoginOption
934
- )
935
- normalizedURL := repository .NormalizeURL (url )
920
+ func (r * HelmChartReconciler ) namespacedChartRepositoryCallback (ctx context.Context , name , namespace string ) chart.GetChartRepositoryCallback {
921
+ return func (url string ) (* repository.ChartRepository , error ) {
922
+ var tlsConfig * tls.Config
936
923
repo , err := r .resolveDependencyRepository (ctx , url , namespace )
937
924
if err != nil {
938
925
// Return Kubernetes client errors, but ignore others
@@ -947,7 +934,7 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
947
934
}
948
935
}
949
936
clientOpts := []helmgetter.Option {
950
- helmgetter .WithURL (normalizedURL ),
937
+ helmgetter .WithURL (repo . Spec . URL ),
951
938
helmgetter .WithTimeout (repo .Spec .Timeout .Duration ),
952
939
helmgetter .WithPassCredentialsAll (repo .Spec .PassCredentials ),
953
940
}
@@ -961,77 +948,26 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
961
948
}
962
949
clientOpts = append (clientOpts , opts ... )
963
950
964
- tlsConfig , err = getter .TLSClientConfigFromSecret (* secret , normalizedURL )
951
+ tlsConfig , err = getter .TLSClientConfigFromSecret (* secret , repo . Spec . URL )
965
952
if err != nil {
966
953
return nil , fmt .Errorf ("failed to create TLS client config for HelmRepository '%s': %w" , repo .Name , err )
967
954
}
968
-
969
- // Build registryClient options from secret
970
- loginOpt , err := registry .LoginOptionFromSecret (normalizedURL , * secret )
971
- if err != nil {
972
- return nil , fmt .Errorf ("failed to create login options for HelmRepository '%s': %w" , repo .Name , err )
973
- }
974
-
975
- loginOpts = append ([]helmreg.LoginOption {}, loginOpt )
976
955
}
977
956
978
- var chartRepo repository.Downloader
979
- if helmreg .IsOCI (normalizedURL ) {
980
- registryClient , credentialsFile , err := r .RegistryClientGenerator (loginOpts != nil )
981
- if err != nil {
982
- return nil , fmt .Errorf ("failed to create registry client for HelmRepository '%s': %w" , repo .Name , err )
983
- }
984
-
985
- var errs []error
986
- // Tell the chart repository to use the OCI client with the configured getter
987
- clientOpts = append (clientOpts , helmgetter .WithRegistryClient (registryClient ))
988
- ociChartRepo , err := repository .NewOCIChartRepository (normalizedURL , repository .WithOCIGetter (r .Getters ),
989
- repository .WithOCIGetterOptions (clientOpts ),
990
- repository .WithOCIRegistryClient (registryClient ),
991
- repository .WithCredentialsFile (credentialsFile ))
992
- if err != nil {
993
- errs = append (errs , fmt .Errorf ("failed to create OCI chart repository for HelmRepository '%s': %w" , repo .Name , err ))
994
- // clean up the credentialsFile
995
- if credentialsFile != "" {
996
- if err := os .Remove (credentialsFile ); err != nil {
997
- errs = append (errs , err )
998
- }
999
- }
1000
- return nil , kerrors .NewAggregate (errs )
1001
- }
1002
-
1003
- // If login options are configured, use them to login to the registry
1004
- // The OCIGetter will later retrieve the stored credentials to pull the chart
1005
- if loginOpts != nil {
1006
- err = ociChartRepo .Login (loginOpts ... )
1007
- if err != nil {
1008
- errs = append (errs , fmt .Errorf ("failed to login to OCI chart repository for HelmRepository '%s': %w" , repo .Name , err ))
1009
- // clean up the credentialsFile
1010
- errs = append (errs , ociChartRepo .Clear ())
1011
- return nil , kerrors .NewAggregate (errs )
1012
- }
1013
- }
1014
-
1015
- chartRepo = ociChartRepo
1016
- } else {
1017
- httpChartRepo , err := repository .NewChartRepository (normalizedURL , "" , r .Getters , tlsConfig , clientOpts )
1018
- if err != nil {
1019
- return nil , err
1020
- }
1021
-
1022
- // Ensure that the cache key is the same as the artifact path
1023
- // otherwise don't enable caching. We don't want to cache indexes
1024
- // for repositories that are not reconciled by the source controller.
1025
- if repo .Status .Artifact != nil {
1026
- httpChartRepo .CachePath = r .Storage .LocalPath (* repo .GetArtifact ())
1027
- httpChartRepo .SetMemCache (r .Storage .LocalPath (* repo .GetArtifact ()), r .Cache , r .TTL , func (event string ) {
1028
- r .IncCacheEvents (event , name , namespace )
1029
- })
1030
- }
1031
-
1032
- chartRepo = httpChartRepo
957
+ chartRepo , err := repository .NewChartRepository (repo .Spec .URL , "" , r .Getters , tlsConfig , clientOpts )
958
+ if err != nil {
959
+ return nil , err
1033
960
}
1034
961
962
+ // Ensure that the cache key is the same as the artifact path
963
+ // otherwise don't enable caching. We don't want to cache indexes
964
+ // for repositories that are not reconciled by the source controller.
965
+ if repo .Status .Artifact != nil {
966
+ chartRepo .CachePath = r .Storage .LocalPath (* repo .GetArtifact ())
967
+ chartRepo .SetMemCache (r .Storage .LocalPath (* repo .GetArtifact ()), r .Cache , r .TTL , func (event string ) {
968
+ r .IncCacheEvents (event , name , namespace )
969
+ })
970
+ }
1035
971
return chartRepo , nil
1036
972
}
1037
973
}
0 commit comments