Skip to content

Commit 3df4c49

Browse files
committed
refactoring and fix tests
Signed-off-by: Soule BA <[email protected]>
1 parent 4e0d792 commit 3df4c49

18 files changed

+435
-176
lines changed

api/v1/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/spec/v1beta2/helmrepositories.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ flux create secret oci ghcr-auth \
454454

455455
#### TLS authentication
456456

457-
**Note:** TLS authentication is not yet supported by OCI Helm repositories.
458-
459457
To provide TLS credentials to use while connecting with the Helm repository,
460458
the referenced Secret is expected to contain `.data.certFile` and
461459
`.data.keyFile`, and/or `.data.caFile` values.
@@ -487,6 +485,28 @@ data:
487485
caFile: <BASE64>
488486
```
489487

488+
#### Provide TLS credentials in a secret of type kubernetes.io/dockerconfigjson
489+
490+
For OCI Helm repositories, Kubernetes secrets of type [kubernetes.io/dockerconfigjson](https://kubernetes.io/docs/concepts/configuration/secret/#secret-types)
491+
are also supported. It is possible to append TLS credentials to the secret data.
492+
493+
For example:
494+
495+
```yaml
496+
apiVersion: v1
497+
kind: Secret
498+
metadata:
499+
name: example-tls
500+
namespace: default
501+
type: kubernetes.io/dockerconfigjson
502+
data:
503+
.dockerconfigjson: <BASE64>
504+
certFile: <BASE64>
505+
keyFile: <BASE64>
506+
# NOTE: Can be supplied without the above values
507+
caFile: <BASE64>
508+
```
509+
490510
### Pass credentials
491511

492512
`.spec.passCredentials` is an optional field to allow the credentials from the

internal/controller/helmchart_controller.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
510510
tlsConfig *tls.Config
511511
authenticator authn.Authenticator
512512
keychain authn.Keychain
513+
tlsLoginOpt helmreg.LoginOption
514+
tmpCertsDir string
513515
)
514516
// Used to login with the repository declared provider
515517
ctxTimeout, cancel := context.WithTimeout(ctx, repo.Spec.Timeout.Duration)
@@ -549,6 +551,22 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
549551
}
550552
clientOpts = append(clientOpts, opts...)
551553
tlsConfig = tlsCfg
554+
tlsLoginOpt, tmpCertsDir, err = makeTLSLoginOption(secret)
555+
if err != nil {
556+
e := &serror.Event{
557+
Err: 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+
defer func() {
565+
if err := os.RemoveAll(tmpCertsDir); err != nil {
566+
r.eventLogf(ctx, obj, corev1.EventTypeWarning, meta.FailedReason,
567+
"failed to delete temporary certificates directory: %s", err)
568+
}
569+
}()
552570

553571
// Build registryClient options from secret
554572
keychain, err = registry.LoginOptionFromSecret(normalizedURL, *secret)
@@ -650,7 +668,11 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
650668
// If login options are configured, use them to login to the registry
651669
// The OCIGetter will later retrieve the stored credentials to pull the chart
652670
if loginOpt != nil {
653-
err = ociChartRepo.Login(loginOpt)
671+
opts := []helmreg.LoginOption{loginOpt}
672+
if tlsLoginOpt != nil {
673+
opts = append(opts, tlsLoginOpt)
674+
}
675+
err = ociChartRepo.Login(opts...)
654676
if err != nil {
655677
e := &serror.Event{
656678
Err: fmt.Errorf("failed to login to OCI registry: %w", err),
@@ -1023,9 +1045,11 @@ func (r *HelmChartReconciler) garbageCollect(ctx context.Context, obj *helmv1.He
10231045
// or a shim with defaults if no object could be found.
10241046
// The callback returns an object with a state, so the caller has to do the necessary cleanup.
10251047
func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Context, name, namespace string) chart.GetChartDownloaderCallback {
1026-
return func(url string) (repository.Downloader, error) {
1048+
return func(url string) (repo repository.Downloader, err error) {
10271049
var (
10281050
tlsConfig *tls.Config
1051+
tlsLoginOpt helmreg.LoginOption
1052+
tmpCertsDir string
10291053
authenticator authn.Authenticator
10301054
keychain authn.Keychain
10311055
)
@@ -1069,6 +1093,19 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
10691093
}
10701094
clientOpts = append(clientOpts, opts...)
10711095
tlsConfig = tlsCfg
1096+
tlsLoginOpt, tmpCertsDir, err = makeTLSLoginOption(secret)
1097+
if err != nil {
1098+
return nil, err
1099+
}
1100+
defer func() {
1101+
var errs []error
1102+
if errf := os.RemoveAll(tmpCertsDir); errf != nil {
1103+
errs = append(errs, errf)
1104+
}
1105+
errs = append(errs, err)
1106+
err = kerrors.NewAggregate(errs)
1107+
return
1108+
}()
10721109

10731110
// Build registryClient options from secret
10741111
keychain, err = registry.LoginOptionFromSecret(normalizedURL, *secret)
@@ -1119,7 +1156,11 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
11191156
// If login options are configured, use them to login to the registry
11201157
// The OCIGetter will later retrieve the stored credentials to pull the chart
11211158
if loginOpt != nil {
1122-
err = ociChartRepo.Login(loginOpt)
1159+
opts := []helmreg.LoginOption{loginOpt}
1160+
if tlsLoginOpt != nil {
1161+
opts = append(opts, tlsLoginOpt)
1162+
}
1163+
err = ociChartRepo.Login(opts...)
11231164
if err != nil {
11241165
errs = append(errs, fmt.Errorf("failed to login to OCI chart repository for HelmRepository '%s': %w", obj.Name, err))
11251166
// clean up the credentialsFile

0 commit comments

Comments
 (0)