Skip to content

Commit 40e8d08

Browse files
committed
Addressing comments on api comments and code duplication
Signed-off-by: Soule BA <soule@weave.works>
1 parent eea3c22 commit 40e8d08

File tree

7 files changed

+35
-33
lines changed

7 files changed

+35
-33
lines changed

api/v1beta1/helmrepository_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const (
3030
// HelmRepositoryURLIndexKey is the key to use for indexing HelmRepository
3131
// resources by their HelmRepositorySpec.URL.
3232
HelmRepositoryURLIndexKey = ".metadata.helmRepositoryURL"
33+
// HelmRepositoryTypeDefault is the default HelmRepository type.
34+
// It is used when no type is specified and corresponds to a Helm repository.
35+
HelmRepositoryTypeDefault = "default"
36+
// HelmRepositoryTypeOCI is the type for an OCI repository.
37+
HelmRepositoryTypeOCI = "oci"
3338
)
3439

3540
// HelmRepositorySpec defines the reference to a Helm repository.

api/v1beta2/helmrepository_types.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ const (
3131
// HelmRepositoryURLIndexKey is the key used for indexing HelmRepository
3232
// objects by their HelmRepositorySpec.URL.
3333
HelmRepositoryURLIndexKey = ".metadata.helmRepositoryURL"
34-
34+
// HelmRepositoryTypeDefault is the default HelmRepository type.
35+
// It is used when no type is specified and corresponds to a Helm repository.
3536
HelmRepositoryTypeDefault = "default"
36-
HelmRepositoryTypeOCI = "oci"
37+
// HelmRepositoryTypeOCI is the type for an OCI repository.
38+
HelmRepositoryTypeOCI = "oci"
3739
)
3840

3941
// HelmRepositorySpec specifies the required configuration to produce an

config/manager/kustomization.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ resources:
66
images:
77
- name: fluxcd/source-controller
88
newName: fluxcd/source-controller
9-
newTag: latest
9+
newTag: v0.24.4

controllers/helmchart_controller.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -445,16 +445,18 @@ func (r *HelmChartReconciler) reconcileSource(ctx context.Context, obj *sourcev1
445445
// object, and returns early.
446446
func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *sourcev1.HelmChart,
447447
repo *sourcev1.HelmRepository, b *chart.Build) (sreconcile.Result, error) {
448-
var tlsConfig *tls.Config
448+
var (
449+
tlsConfig *tls.Config
450+
logOpts []registry.LoginOption
451+
)
449452

450453
// Construct the Getter options from the HelmRepository data
451454
clientOpts := []helmgetter.Option{
452455
helmgetter.WithURL(repo.Spec.URL),
453456
helmgetter.WithTimeout(repo.Spec.Timeout.Duration),
454457
helmgetter.WithPassCredentialsAll(repo.Spec.PassCredentials),
455458
}
456-
secret, err := r.getHelmRepositorySecret(ctx, repo)
457-
if secret != nil || err != nil {
459+
if secret, err := r.getHelmRepositorySecret(ctx, repo); secret != nil || err != nil {
458460
if err != nil {
459461
e := &serror.Event{
460462
Err: fmt.Errorf("failed to get secret '%s': %w", repo.Spec.SecretRef.Name, err),
@@ -488,13 +490,27 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
488490
// Requeue as content of secret might change
489491
return sreconcile.ResultEmpty, e
490492
}
493+
494+
// Build registryClient options from secret
495+
logOpt, err := loginOptionFromSecret(*secret)
496+
if err != nil {
497+
e := &serror.Event{
498+
Err: fmt.Errorf("failed to configure Helm client with secret data: %w", err),
499+
Reason: sourcev1.AuthenticationFailedReason,
500+
}
501+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
502+
// Requeue as content of secret might change
503+
return sreconcile.ResultEmpty, e
504+
}
505+
506+
logOpts = append([]registry.LoginOption{}, logOpt)
491507
}
492508

493509
// Initialize the chart repository
494510
var chartRepo chart.Remote
495511
if repo.Spec.Type == sourcev1.HelmRepositoryTypeOCI {
496512
if !registry.IsOCI(repo.Spec.URL) {
497-
err = fmt.Errorf("invalid OCI registry URL: %s", repo.Spec.URL)
513+
err := fmt.Errorf("invalid OCI registry URL: %s", repo.Spec.URL)
498514
return chartRepoErrorReturn(err, obj)
499515
}
500516
// Tell the chart repository to use the OCI client with the configured getter
@@ -507,15 +523,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
507523

508524
// If login options are configured, use them to login to the registry
509525
// The OCIGetter will later retrieve the stored credentials to pull the chart
510-
if secret != nil {
511-
// Construct actual options
512-
logOpt, err := loginOptionFromSecret(*secret)
513-
if err != nil {
514-
return chartRepoErrorReturn(err, obj)
515-
}
516-
517-
logOpts := append([]registry.LoginOption{}, logOpt)
518-
526+
if logOpts != nil {
519527
// create a temporary file to store the credentials
520528
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
521529
// TODO@souleb: remove this once the registry move to Oras v2
@@ -535,7 +543,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
535543
}
536544
} else {
537545
var httpChartRepo *repository.ChartRepository
538-
httpChartRepo, err = repository.NewChartRepository(repo.Spec.URL, r.Storage.LocalPath(*repo.GetArtifact()), r.Getters, tlsConfig, clientOpts,
546+
httpChartRepo, err := repository.NewChartRepository(repo.Spec.URL, r.Storage.LocalPath(*repo.GetArtifact()), r.Getters, tlsConfig, clientOpts,
539547
repository.WithMemoryCache(r.Storage.LocalPath(*repo.GetArtifact()), r.Cache, r.TTL, func(event string) {
540548
r.IncCacheEvents(event, obj.Name, obj.Namespace)
541549
}))

controllers/helmchart_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/darkowlzz/controller-check/status"
3535
. "github.com/onsi/gomega"
36+
hchart "helm.sh/helm/v3/pkg/chart"
3637
"helm.sh/helm/v3/pkg/chart/loader"
3738
"helm.sh/helm/v3/pkg/registry"
3839
corev1 "k8s.io/api/core/v1"
@@ -54,7 +55,6 @@ import (
5455
"github.com/fluxcd/source-controller/internal/helm/chart"
5556
sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
5657
"github.com/fluxcd/source-controller/internal/reconcile/summarize"
57-
hchart "helm.sh/helm/v3/pkg/chart"
5858
)
5959

6060
func TestHelmChartReconciler_Reconcile(t *testing.T) {

controllers/helmrepository_controller_oci.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ type HelmRepositoryOCIReconciler struct {
8383
client.Client
8484
kuberecorder.EventRecorder
8585
helper.Metrics
86-
Getters helmgetter.Providers
87-
// Storage *Storage
86+
Getters helmgetter.Providers
8887
ControllerName string
8988
RegistryClient *registry.Client
9089
}

internal/helm/repository/oci_chart_repository.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ func WithOCIGetterOptions(getterOpts []getter.Option) OCIChartRepositoryOption {
9393
}
9494
}
9595

96-
// WithOCITlsConfig returns a ChartRepositoryOption that will set the tls.Config
97-
func WithTlsConfig(tlsConfig *tls.Config) OCIChartRepositoryOption {
98-
return func(r *OCIChartRepository) error {
99-
r.tlsConfig = tlsConfig
100-
return nil
101-
}
102-
}
103-
10496
// NewOCIChartRepository constructs and returns a new ChartRepository with
10597
// the ChartRepository.Client configured to the getter.Getter for the
10698
// repository URL scheme. It returns an error on URL parsing failures.
@@ -111,7 +103,7 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
111103
return nil, err
112104
}
113105

114-
r := newOCIChartRepository()
106+
r := &OCIChartRepository{}
115107
r.URL = *u
116108
for _, opt := range chartRepoOpts {
117109
if err := opt(r); err != nil {
@@ -122,10 +114,6 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
122114
return r, nil
123115
}
124116

125-
func newOCIChartRepository() *OCIChartRepository {
126-
return &OCIChartRepository{}
127-
}
128-
129117
// Get returns the repo.ChartVersion for the given name, the version is expected
130118
// to be a semver.Constraints compatible string. If version is empty, the latest
131119
// stable version will be returned and prerelease versions will be ignored.

0 commit comments

Comments
 (0)