Skip to content

Commit aa4e08b

Browse files
committed
improving debugging for local environment
1 parent 0d4426d commit aa4e08b

File tree

2 files changed

+64
-31
lines changed

2 files changed

+64
-31
lines changed

pkg/auto/oci.go

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
type loadAssetFunc func(chart, asset string) ([]byte, error)
22-
type checkAssetFunc func(regClient *registry.Client, ociDNS, chart, version string) (bool, error)
22+
type checkAssetFunc func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error)
2323
type pushFunc func(helmClient *registry.Client, data []byte, url string) error
2424

2525
type oci struct {
@@ -39,7 +39,7 @@ func UpdateOCI(ctx context.Context, rootFs billy.Filesystem, ociDNS, ociUser, oc
3939
return err
4040
}
4141

42-
oci, err := setupOCI(ociDNS, ociUser, ociPass, debug)
42+
oci, err := setupOCI(ctx, ociDNS, ociUser, ociPass, debug)
4343
if err != nil {
4444
return err
4545
}
@@ -53,15 +53,15 @@ func UpdateOCI(ctx context.Context, rootFs billy.Filesystem, ociDNS, ociUser, oc
5353
return nil
5454
}
5555

56-
func setupOCI(ociDNS, ociUser, ociPass string, debug bool) (*oci, error) {
56+
func setupOCI(ctx context.Context, ociDNS, ociUser, ociPass string, debug bool) (*oci, error) {
5757
var err error
5858
o := &oci{
5959
DNS: ociDNS,
6060
user: ociUser,
6161
password: ociPass,
6262
}
6363

64-
o.helmClient, err = setupHelm(o.DNS, o.user, o.password, debug)
64+
o.helmClient, err = setupHelm(ctx, o.DNS, o.user, o.password, debug)
6565
if err != nil {
6666
return nil, err
6767
}
@@ -73,7 +73,7 @@ func setupOCI(ociDNS, ociUser, ociPass string, debug bool) (*oci, error) {
7373
return o, nil
7474
}
7575

76-
func setupHelm(ociDNS, ociUser, ociPass string, debug bool) (*registry.Client, error) {
76+
func setupHelm(ctx context.Context, ociDNS, ociUser, ociPass string, debug bool) (*registry.Client, error) {
7777
settings := cli.New()
7878
actionConfig := new(action.Configuration)
7979
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
@@ -83,42 +83,73 @@ func setupHelm(ociDNS, ociUser, ociPass string, debug bool) (*registry.Client, e
8383
}
8484

8585
var regClient *registry.Client
86+
var err error
8687

87-
if debug {
88-
fmt.Println("debug mode you need to provide a self-signed certificate")
89-
caFile := "/etc/docker/certs.d/" + ociDNS + "/ca.crt"
88+
registryHost := extractRegistryHost(ociDNS)
89+
isLocalHost := strings.HasPrefix(registryHost, "localhost:")
9090

91-
regClient, err := registry.NewRegistryClientWithTLS(os.Stdout, "", "", caFile, false, "", true)
91+
switch {
92+
// Debug Mode but pointing to a server with custom-certificates
93+
case debug && !isLocalHost:
94+
logger.Log(ctx, slog.LevelDebug, "debug mode", slog.Bool("localhost", isLocalHost))
95+
caFile := "/etc/docker/certs.d/" + registryHost + "/ca.crt"
96+
regClient, err = registry.NewRegistryClientWithTLS(os.Stdout, "", "", caFile, false, "", true)
9297
if err != nil {
98+
logger.Log(ctx, slog.LevelError, "failed to create registry client with TLS")
9399
return nil, err
94100
}
95-
96-
if err := regClient.Login(
97-
ociDNS,
101+
if err = regClient.Login(
102+
registryHost,
98103
registry.LoginOptInsecure(false),
99104
registry.LoginOptTLSClientConfig("", "", caFile),
100105
registry.LoginOptBasicAuth(ociUser, ociPass),
101106
); err != nil {
107+
logger.Log(ctx, slog.LevelError, "failed to login to registry with TLS", slog.Group(ociDNS, ociUser, ociPass))
102108
return nil, err
103109
}
104110

105-
return regClient, nil
106-
}
107-
108-
regClient, err := registry.NewClient(registry.ClientOptDebug(false))
109-
if err != nil {
110-
return nil, err
111-
}
111+
// Debug Mode at localhost without TLS
112+
case debug && isLocalHost:
113+
logger.Log(ctx, slog.LevelDebug, "debug mode", slog.Bool("localhost", isLocalHost))
114+
regClient, err = registry.NewClient(
115+
registry.ClientOptDebug(true),
116+
registry.ClientOptPlainHTTP(),
117+
)
118+
if err != nil {
119+
logger.Log(ctx, slog.LevelError, "failed to create registry client")
120+
return nil, err
121+
}
122+
if err = regClient.Login(registryHost,
123+
registry.LoginOptInsecure(true), // true for localhost, false for production
124+
registry.LoginOptBasicAuth(ociUser, ociPass)); err != nil {
125+
logger.Log(ctx, slog.LevelError, "failed to login to registry", slog.Group(ociDNS, ociUser, ociPass))
126+
return nil, err
127+
}
112128

113-
if err := regClient.Login(ociDNS,
114-
registry.LoginOptInsecure(false),
115-
registry.LoginOptBasicAuth(ociUser, ociPass)); err != nil {
116-
return nil, err
129+
// Production code with Secure Mode and authentication
130+
default:
131+
regClient, err = registry.NewClient(registry.ClientOptDebug(false))
132+
if err != nil {
133+
return nil, err
134+
}
135+
if err = regClient.Login(registryHost,
136+
registry.LoginOptInsecure(false),
137+
registry.LoginOptBasicAuth(ociUser, ociPass)); err != nil {
138+
return nil, err
139+
}
117140
}
118141

119142
return regClient, nil
120143
}
121144

145+
// extractRegistryHost will extract the DNS for login
146+
func extractRegistryHost(ociDNS string) string {
147+
if idx := strings.Index(ociDNS, "/"); idx != -1 {
148+
return ociDNS[:idx]
149+
}
150+
return ociDNS
151+
}
152+
122153
// update will attempt to update a helm chart to an OCI registry.
123154
// 2 phases:
124155
// - 1: Pre-Flight validations (check the current chart + check if it already exists)
@@ -148,7 +179,7 @@ func (o *oci) update(ctx context.Context, release *options.ReleaseOptions) ([]st
148179

149180
// Check if the asset version already exists in the OCI registry
150181
// Never overwrite a previously released chart!
151-
exists, err := o.checkAsset(o.helmClient, o.DNS, chart, version)
182+
exists, err := o.checkAsset(ctx, o.helmClient, o.DNS, chart, version)
152183
if err != nil {
153184
logger.Log(ctx, slog.LevelError, "failed to check registry for asset", slog.String("asset", asset))
154185
return pushedAssets, err
@@ -221,14 +252,16 @@ func buildPushURL(ociDNS, chart, version string) string {
221252
}
222253

223254
// checkAsset checks if a specific asset version exists in the OCI registry
224-
func checkAsset(helmClient *registry.Client, ociDNS, chart, version string) (bool, error) {
255+
func checkAsset(ctx context.Context, helmClient *registry.Client, ociDNS, chart, version string) (bool, error) {
225256
// Once issue is resolved: https://github.com/helm/helm/issues/13368
226257
// Replace by: helmClient.Tags(ociDNS + "/" + chart + ":" + version)
227258
existingVersions, err := helmClient.Tags(ociDNS + "/" + chart)
228259
if err != nil {
229260
if strings.Contains(err.Error(), "unexpected status code 404: name unknown: repository name not known to registry") {
261+
logger.Log(ctx, slog.LevelDebug, "asset does not exist at registry", slog.String("chart", chart))
230262
return false, nil
231263
}
264+
logger.Err(err)
232265
return false, err
233266
}
234267

pkg/auto/oci_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Test_push(t *testing.T) {
3737
loadAsset: func(chart, asset string) ([]byte, error) {
3838
return []byte{}, nil
3939
},
40-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
40+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
4141
return false, nil
4242
},
4343
push: func(helmClient *registry.Client, data []byte, url string) error {
@@ -64,7 +64,7 @@ func Test_push(t *testing.T) {
6464
loadAsset: func(chart, asset string) ([]byte, error) {
6565
return []byte{}, nil
6666
},
67-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
67+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
6868
return false, nil
6969
},
7070
push: func(helmClient *registry.Client, data []byte, url string) error {
@@ -97,7 +97,7 @@ func Test_push(t *testing.T) {
9797
loadAsset: func(chart, asset string) ([]byte, error) {
9898
return []byte{}, errors.New("some-error")
9999
},
100-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
100+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
101101
return false, nil
102102
},
103103
push: func(helmClient *registry.Client, data []byte, url string) error {
@@ -124,7 +124,7 @@ func Test_push(t *testing.T) {
124124
loadAsset: func(chart, asset string) ([]byte, error) {
125125
return []byte{}, nil
126126
},
127-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
127+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
128128
return false, errors.New("some-error")
129129
},
130130
push: func(helmClient *registry.Client, data []byte, url string) error {
@@ -151,7 +151,7 @@ func Test_push(t *testing.T) {
151151
loadAsset: func(chart, asset string) ([]byte, error) {
152152
return []byte{}, nil
153153
},
154-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
154+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
155155
return true, nil
156156
},
157157
push: func(helmClient *registry.Client, data []byte, url string) error {
@@ -178,7 +178,7 @@ func Test_push(t *testing.T) {
178178
loadAsset: func(chart, asset string) ([]byte, error) {
179179
return []byte{}, nil
180180
},
181-
checkAsset: func(regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
181+
checkAsset: func(ctx context.Context, regClient *registry.Client, ociDNS, chart, version string) (bool, error) {
182182
return false, nil
183183
},
184184
push: func(helmClient *registry.Client, data []byte, url string) error {

0 commit comments

Comments
 (0)