Skip to content

Commit 3e9d734

Browse files
authored
Merge pull request #257 from nicholasSUSE/fix-paths-for-registries
Fixed proper url for registries
2 parents ddae817 + 88bf9db commit 3e9d734

File tree

2 files changed

+219
-281
lines changed

2 files changed

+219
-281
lines changed

pkg/auto/oci.go

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@ package auto
22

33
import (
44
"context"
5-
"crypto/tls"
65
"errors"
76
"fmt"
87
"log/slog"
9-
"net/http"
108
"os"
119
"strings"
1210

1311
"github.com/go-git/go-billy/v5"
14-
"github.com/google/go-containerregistry/pkg/authn"
15-
"github.com/google/go-containerregistry/pkg/name"
16-
"github.com/google/go-containerregistry/pkg/v1/remote"
17-
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
1812
"github.com/rancher/charts-build-scripts/pkg/logger"
1913
"github.com/rancher/charts-build-scripts/pkg/options"
2014
"github.com/rancher/charts-build-scripts/pkg/path"
21-
"github.com/rancher/charts-build-scripts/pkg/registries"
2215

2316
"helm.sh/helm/v3/pkg/action"
2417
"helm.sh/helm/v3/pkg/cli"
@@ -30,14 +23,13 @@ type checkAssetFunc func(ctx context.Context, regClient *registry.Client, ociDNS
3023
type pushFunc func(helmClient *registry.Client, data []byte, url string) error
3124

3225
type oci struct {
33-
DNS string
34-
user string
35-
password string
36-
helmClient *registry.Client
37-
registryOptions []remote.Option
38-
loadAsset loadAssetFunc
39-
checkAsset checkAssetFunc
40-
push pushFunc
26+
DNS string
27+
user string
28+
password string
29+
helmClient *registry.Client
30+
loadAsset loadAssetFunc
31+
checkAsset checkAssetFunc
32+
push pushFunc
4133
}
4234

4335
// UpdateOCI pushes Helm charts to an OCI registry
@@ -78,31 +70,13 @@ func setupOCI(ctx context.Context, ociDNS, ociUser, ociPass string, debug bool)
7870
return nil, err
7971
}
8072

81-
o.registryOptions = setupRegistryReader(ctx, o.DNS, o.user, o.password)
82-
8373
o.loadAsset = loadAsset
8474
o.checkAsset = checkAsset
8575
o.push = push
8676

8777
return o, nil
8878
}
8979

90-
func setupRegistryReader(ctx context.Context, ociDNS, ociUser, ociPass string) []remote.Option {
91-
tr := http.DefaultTransport.(*http.Transport).Clone()
92-
tr.TLSClientConfig = &tls.Config{
93-
InsecureSkipVerify: false,
94-
}
95-
96-
registryClientOpts := []remote.Option{
97-
remote.WithContext(ctx),
98-
remote.WithUserAgent(registries.UaString),
99-
remote.WithAuth(&authn.Basic{Username: ociUser, Password: ociPass}),
100-
remote.WithTransport(tr),
101-
}
102-
103-
return registryClientOpts
104-
}
105-
10680
func setupHelm(ctx context.Context, ociDNS, ociUser, ociPass string, debug bool) (*registry.Client, error) {
10781
settings := cli.New()
10882
actionConfig := new(action.Configuration)
@@ -206,13 +180,6 @@ func (o *oci) update(ctx context.Context, release *options.ReleaseOptions) ([]st
206180

207181
// Check if the asset version already exists in the OCI registry
208182
// Never overwrite a previously released chart!
209-
existsTest, err := o.checkRegistryTagExists(ctx, o.DNS, chart, version)
210-
if err != nil {
211-
logger.Log(ctx, slog.LevelError, "checkRegistryTagExists")
212-
return pushedAssets, err
213-
}
214-
logger.Log(ctx, slog.LevelWarn, "exists worked?", slog.Bool("exist", existsTest))
215-
216183
exists, err := o.checkAsset(ctx, o.helmClient, o.DNS, chart, version)
217184
if err != nil {
218185
return pushedAssets, err
@@ -281,18 +248,14 @@ func loadAsset(chart, asset string) ([]byte, error) {
281248

282249
// oci://<oci-dns>/<chart(repository)>:<version>
283250
func buildPushURL(ociDNS, chart, version string) string {
284-
return ociDNS + "/" + chart + ":" + version
251+
return ociDNS + "/rancher/charts/" + chart + ":" + version
285252
}
286253

287254
// checkAsset checks if a specific asset version exists in the OCI registry
288255
func checkAsset(ctx context.Context, helmClient *registry.Client, ociDNS, chart, version string) (bool, error) {
289256
// Once issue is resolved: https://github.com/helm/helm/issues/13368
290257
// Replace by: helmClient.Tags(ociDNS + "/" + chart + ":" + version)
291258
tagsURL := ociDNS + "/rancher/charts/" + chart
292-
logger.Log(ctx, slog.LevelDebug, "checking tags",
293-
slog.String("ociDNS", ociDNS),
294-
slog.String("chart", chart),
295-
slog.String("fullURL", tagsURL))
296259
existingVersions, err := helmClient.Tags(tagsURL)
297260
if err != nil {
298261
if strings.Contains(err.Error(), "unexpected status code 404: name unknown: repository name not known to registry") {
@@ -311,39 +274,3 @@ func checkAsset(ctx context.Context, helmClient *registry.Client, ociDNS, chart,
311274

312275
return false, nil
313276
}
314-
315-
// checkRegistryTagExists checks if a given source already exists at the target Registry
316-
func (o *oci) checkRegistryTagExists(ctx context.Context, ociDNS, chart, tag string) (bool, error) {
317-
var nameOpts []name.Option
318-
nameOpts = append(nameOpts, name.StrictValidation)
319-
nameOpts = append(nameOpts, name.Insecure)
320-
321-
ociTag := strings.ReplaceAll(tag, "+", "_")
322-
323-
// Build repository reference first (host + path, no tag)
324-
repoStr := ociDNS + "/rancher/charts/" + chart
325-
repo, err := name.NewRepository(repoStr, nameOpts...)
326-
if err != nil {
327-
logger.Log(ctx, slog.LevelError, "failed to parse repository", logger.Err(err))
328-
return false, err
329-
}
330-
// Then create tag reference from repository
331-
dst := repo.Tag(ociTag)
332-
333-
// ----------------------------------------------------
334-
exist := true
335-
if _, err := remote.Head(dst, o.registryOptions...); err != nil {
336-
exist = false
337-
338-
var te *transport.Error
339-
if errors.As(err, &te) && te.StatusCode == http.StatusNotFound {
340-
// 404s are not treated as errors, means the img/tag does not exist
341-
err = nil
342-
} else {
343-
logger.Log(ctx, slog.LevelError, "failure to check prime tag", logger.Err(err))
344-
}
345-
}
346-
347-
logger.Log(ctx, slog.LevelDebug, "checking", slog.Bool("exist", exist), slog.String("dst", dst.Name()))
348-
return exist, err
349-
}

0 commit comments

Comments
 (0)