Skip to content

Commit 98b2edc

Browse files
committed
Remove helm sdk oras v1 workarounds
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 910e7e7 commit 98b2edc

File tree

4 files changed

+28
-110
lines changed

4 files changed

+28
-110
lines changed

internal/controller/helmchart_controller.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,8 @@ type HelmChartReconciler struct {
143143
patchOptions []patch.Option
144144
}
145145

146-
// RegistryClientGeneratorFunc is a function that returns a registry client
147-
// and an optional file name.
148-
// The file is used to store the registry client credentials.
149-
// The caller is responsible for deleting the file.
150-
type RegistryClientGeneratorFunc func(tlsConfig *tls.Config, isLogin, insecure bool) (*helmreg.Client, string, error)
146+
// RegistryClientGeneratorFunc is a function that returns a registry client.
147+
type RegistryClientGeneratorFunc func(tlsConfig *tls.Config, isLogin, insecure bool) (*helmreg.Client, error)
151148

152149
func (r *HelmChartReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
153150
return r.SetupWithManagerAndOptions(ctx, mgr, HelmChartReconcilerOptions{})
@@ -552,11 +549,7 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
552549
return chartRepoConfigErrorReturn(err, obj)
553550
}
554551

555-
// with this function call, we create a temporary file to store the credentials if needed.
556-
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
557-
// TODO@souleb: remove this once the registry move to Oras v2
558-
// or rework to enable reusing credentials to avoid the unneccessary handshake operations
559-
registryClient, credentialsFile, err := r.RegistryClientGenerator(clientOpts.TlsConfig, clientOpts.MustLoginToRegistry(), repo.Spec.Insecure)
552+
registryClient, err := r.RegistryClientGenerator(clientOpts.TlsConfig, clientOpts.MustLoginToRegistry(), repo.Spec.Insecure)
560553
if err != nil {
561554
e := serror.NewGeneric(
562555
fmt.Errorf("failed to construct Helm client: %w", err),
@@ -566,15 +559,6 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
566559
return sreconcile.ResultEmpty, e
567560
}
568561

569-
if credentialsFile != "" {
570-
defer func() {
571-
if err := os.Remove(credentialsFile); err != nil {
572-
r.eventLogf(ctx, obj, corev1.EventTypeWarning, meta.FailedReason,
573-
"failed to delete temporary credentials file: %s", err)
574-
}
575-
}()
576-
}
577-
578562
var verifiers []soci.Verifier
579563
if obj.Spec.Verify != nil {
580564
provider := obj.Spec.Verify.Provider
@@ -1026,39 +1010,34 @@ func (r *HelmChartReconciler) namespacedChartRepositoryCallback(ctx context.Cont
10261010

10271011
var chartRepo repository.Downloader
10281012
if helmreg.IsOCI(normalizedURL) {
1029-
registryClient, credentialsFile, err := r.RegistryClientGenerator(clientOpts.TlsConfig, clientOpts.MustLoginToRegistry(), obj.Spec.Insecure)
1013+
registryClient, err := r.RegistryClientGenerator(clientOpts.TlsConfig, clientOpts.MustLoginToRegistry(), obj.Spec.Insecure)
10301014
if err != nil {
10311015
return nil, fmt.Errorf("failed to create registry client: %w", err)
10321016
}
10331017

1034-
var errs []error
10351018
// Tell the chart repository to use the OCI client with the configured getter
10361019
getterOpts = append(getterOpts, helmgetter.WithRegistryClient(registryClient))
10371020
ociChartRepo, err := repository.NewOCIChartRepository(normalizedURL, repository.WithOCIGetter(r.Getters),
10381021
repository.WithOCIGetterOptions(getterOpts),
10391022
repository.WithOCIRegistryClient(registryClient),
1040-
repository.WithCertificatesStore(certsTmpDir),
1041-
repository.WithCredentialsFile(credentialsFile))
1023+
repository.WithCertificatesStore(certsTmpDir))
10421024
if err != nil {
1043-
errs = append(errs, fmt.Errorf("failed to create OCI chart repository: %w", err))
1044-
// clean up the credentialsFile
1045-
if credentialsFile != "" {
1046-
if err := os.Remove(credentialsFile); err != nil {
1047-
errs = append(errs, err)
1048-
}
1049-
}
1050-
return nil, kerrors.NewAggregate(errs)
1025+
return nil, fmt.Errorf("failed to create OCI chart repository: %w", err)
10511026
}
10521027

10531028
// If login options are configured, use them to login to the registry
10541029
// The OCIGetter will later retrieve the stored credentials to pull the chart
10551030
if clientOpts.MustLoginToRegistry() {
10561031
err = ociChartRepo.Login(clientOpts.RegLoginOpts...)
10571032
if err != nil {
1058-
errs = append(errs, fmt.Errorf("failed to login to OCI chart repository: %w", err))
1059-
// clean up the credentialsFile
1060-
errs = append(errs, ociChartRepo.Clear())
1061-
return nil, kerrors.NewAggregate(errs)
1033+
err = fmt.Errorf("failed to login to OCI chart repository: %w", err)
1034+
if clearErr := ociChartRepo.Clear(); clearErr != nil {
1035+
var errs []error
1036+
errs = append(errs, err)
1037+
errs = append(errs, clearErr)
1038+
return nil, kerrors.NewAggregate(errs)
1039+
}
1040+
return nil, err
10621041
}
10631042
}
10641043

internal/helm/chart/dependency_manager_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func (g *mockGetter) Get(_ string, _ ...helmgetter.Option) (*bytes.Buffer, error
7676
func TestDependencyManager_Clear(t *testing.T) {
7777
g := NewWithT(t)
7878

79-
file, err := os.CreateTemp("", "")
80-
g.Expect(err).ToNot(HaveOccurred())
81-
ociRepoWithCreds, err := repository.NewOCIChartRepository("oci://example.com", repository.WithCredentialsFile(file.Name()))
79+
ociRepoWithCreds, err := repository.NewOCIChartRepository("oci://example.com")
8280
g.Expect(err).ToNot(HaveOccurred())
8381

8482
downloaders := map[string]repository.Downloader{
@@ -99,14 +97,8 @@ func TestDependencyManager_Clear(t *testing.T) {
9997
case *repository.ChartRepository:
10098
g.Expect(v.Index).To(BeNil())
10199
case *repository.OCIChartRepository:
102-
g.Expect(v.HasCredentials()).To(BeFalse())
103100
}
104101
}
105-
106-
if _, err := os.Stat(file.Name()); !errors.Is(err, os.ErrNotExist) {
107-
err = os.Remove(file.Name())
108-
g.Expect(err).ToNot(HaveOccurred())
109-
}
110102
}
111103

112104
func TestDependencyManager_Build(t *testing.T) {

internal/helm/registry/client.go

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,29 @@ import (
2020
"crypto/tls"
2121
"io"
2222
"net/http"
23-
"os"
2423

2524
"helm.sh/helm/v3/pkg/registry"
26-
"k8s.io/apimachinery/pkg/util/errors"
2725
)
2826

29-
// ClientGenerator generates a registry client and a temporary credential file.
27+
// ClientGenerator generates a registry client.
3028
// The client is meant to be used for a single reconciliation.
31-
// The file is meant to be used for a single reconciliation and deleted after.
32-
func ClientGenerator(tlsConfig *tls.Config, isLogin, insecureHTTP bool) (*registry.Client, string, error) {
29+
func ClientGenerator(tlsConfig *tls.Config, isLogin, insecureHTTP bool) (*registry.Client, error) {
3330
if isLogin {
34-
// create a temporary file to store the credentials
35-
// this is needed because otherwise the credentials are stored in ~/.docker/config.json.
36-
credentialsFile, err := os.CreateTemp("", "credentials")
31+
rClient, err := newClient(tlsConfig, insecureHTTP)
3732
if err != nil {
38-
return nil, "", err
33+
return nil, err
3934
}
40-
41-
var errs []error
42-
rClient, err := newClient("", tlsConfig, insecureHTTP)
43-
if err != nil {
44-
errs = append(errs, err)
45-
// attempt to delete the temporary file
46-
if credentialsFile != nil {
47-
err := os.Remove(credentialsFile.Name())
48-
if err != nil {
49-
errs = append(errs, err)
50-
}
51-
}
52-
return nil, "", errors.NewAggregate(errs)
53-
}
54-
return rClient, credentialsFile.Name(), nil
35+
return rClient, nil
5536
}
5637

57-
rClient, err := newClient("", tlsConfig, insecureHTTP)
38+
rClient, err := newClient(tlsConfig, insecureHTTP)
5839
if err != nil {
59-
return nil, "", err
40+
return nil, err
6041
}
61-
return rClient, "", nil
42+
return rClient, nil
6243
}
6344

64-
func newClient(credentialsFile string, tlsConfig *tls.Config, insecureHTTP bool) (*registry.Client, error) {
45+
func newClient(tlsConfig *tls.Config, insecureHTTP bool) (*registry.Client, error) {
6546
opts := []registry.ClientOption{
6647
registry.ClientOptWriter(io.Discard),
6748
}
@@ -75,9 +56,5 @@ func newClient(credentialsFile string, tlsConfig *tls.Config, insecureHTTP bool)
7556
Transport: t,
7657
}))
7758
}
78-
if credentialsFile != "" {
79-
opts = append(opts, registry.ClientOptCredentialsFile(credentialsFile))
80-
}
81-
8259
return registry.NewClient(opts...)
8360
}

internal/helm/repository/oci_chart_repository.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"context"
2222
"crypto/tls"
23-
"errors"
2423
"fmt"
2524
"net/url"
2625
"os"
@@ -67,9 +66,6 @@ type OCIChartRepository struct {
6766
// RegistryClient is a client to use while downloading tags or charts from a registry.
6867
RegistryClient RegistryClient
6968

70-
// credentialsFile is a temporary credentials file to use while downloading tags or charts from a registry.
71-
credentialsFile string
72-
7369
// certificatesStore is a temporary store to use while downloading tags or charts from a registry.
7470
certificatesStore string
7571

@@ -127,14 +123,6 @@ func WithOCIGetterOptions(getterOpts []getter.Option) OCIChartRepositoryOption {
127123
}
128124
}
129125

130-
// WithCredentialsFile returns a ChartRepositoryOption that will set the credentials file
131-
func WithCredentialsFile(credentialsFile string) OCIChartRepositoryOption {
132-
return func(r *OCIChartRepository) error {
133-
r.credentialsFile = credentialsFile
134-
return nil
135-
}
136-
}
137-
138126
// WithCertificatesStore returns a ChartRepositoryOption that will set the certificates store
139127
func WithCertificatesStore(store string) OCIChartRepositoryOption {
140128
return func(r *OCIChartRepository) error {
@@ -281,31 +269,13 @@ func (r *OCIChartRepository) Logout() error {
281269
return nil
282270
}
283271

284-
// HasCredentials returns true if the OCIChartRepository has credentials.
285-
func (r *OCIChartRepository) HasCredentials() bool {
286-
return r.credentialsFile != ""
287-
}
288-
289-
// Clear deletes the OCI registry credentials file.
290-
func (r *OCIChartRepository) Clear() error {
291-
var errs error
292-
// clean the credentials file if it exists
293-
if r.credentialsFile != "" {
294-
if err := os.Remove(r.credentialsFile); err != nil {
295-
errs = errors.Join(errs, err)
296-
}
297-
}
298-
r.credentialsFile = ""
299-
300-
// clean the certificates store if it exists
272+
// Clear deletes the OCI registry certificates store if it exists.
273+
func (r *OCIChartRepository) Clear() (err error) {
301274
if r.certificatesStore != "" {
302-
if err := os.RemoveAll(r.certificatesStore); err != nil {
303-
errs = errors.Join(errs, err)
304-
}
275+
err = os.RemoveAll(r.certificatesStore)
305276
}
306277
r.certificatesStore = ""
307-
308-
return errs
278+
return
309279
}
310280

311281
// getLastMatchingVersionOrConstraint returns the last version that matches the given version string.

0 commit comments

Comments
 (0)