Skip to content

Commit 46c3891

Browse files
authored
Merge pull request #814 from neoaggelos/fix/parse-oci-source
🐛 Fix OCI source parsing issues
2 parents 8da5fa5 + 74a0c4b commit 46c3891

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

internal/controller/oci_source.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,29 @@ func (m mapStore) Tag(ctx context.Context, desc ocispec.Descriptor, reference st
195195

196196
var _ oras.Target = &mapStore{}
197197

198-
// CopyOCIStore collects artifacts from the provider OCI url and creates a map of file contents.
199-
func CopyOCIStore(ctx context.Context, url string, version string, store *mapStore, credential *auth.Credential) error {
200-
log := log.FromContext(ctx)
201-
198+
// parseOCISource accepts an OCI URL and the provider version. It returns the image name,
199+
// the image version (if not set on the OCI URL, the provider version is used) and whether
200+
// plain HTTP should be used to fetch the image (when url starts with "http://").
201+
func parseOCISource(url string, version string) (string, string, bool) {
202202
url, plainHTTP := strings.CutPrefix(url, "http://")
203203

204-
if parts := strings.SplitN(url, ":", 3); len(parts) == 2 {
204+
if parts := strings.SplitN(url, ":", 3); len(parts) == 2 && !strings.Contains(parts[1], "/") {
205205
url = parts[0]
206206
version = parts[1]
207207
} else if len(parts) == 3 {
208208
version = parts[2]
209-
url, _ = strings.CutSuffix(url, version)
209+
url = fmt.Sprintf("%s:%s", parts[0], parts[1])
210210
}
211211

212+
return url, version, plainHTTP
213+
}
214+
215+
// CopyOCIStore collects artifacts from the provider OCI url and creates a map of file contents.
216+
func CopyOCIStore(ctx context.Context, url string, version string, store *mapStore, credential *auth.Credential) error {
217+
log := log.FromContext(ctx)
218+
219+
url, version, plainHTTP := parseOCISource(url, version)
220+
212221
repo, err := remote.NewRepository(url)
213222
if err != nil {
214223
log.Error(err, "Invalid registry URL specified")
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func Test_parseOCISource(t *testing.T) {
27+
for _, tc := range []struct {
28+
url string
29+
version string
30+
expectURL string
31+
expectVersion string
32+
}{
33+
{
34+
url: "registry/image:v1.1.0",
35+
version: "v1.2.0",
36+
expectURL: "registry/image",
37+
expectVersion: "v1.1.0",
38+
},
39+
{
40+
url: "registry/image",
41+
version: "v1.2.0",
42+
expectURL: "registry/image",
43+
expectVersion: "v1.2.0",
44+
},
45+
{
46+
url: "registry:5050/image",
47+
version: "v1.2.0",
48+
expectURL: "registry:5050/image",
49+
expectVersion: "v1.2.0",
50+
},
51+
{
52+
url: "registry:5050/image:v1.1.0",
53+
version: "v1.2.0",
54+
expectURL: "registry:5050/image",
55+
expectVersion: "v1.1.0",
56+
},
57+
{
58+
url: "image",
59+
version: "v1.2.0",
60+
expectURL: "image",
61+
expectVersion: "v1.2.0",
62+
},
63+
} {
64+
t.Run(tc.url, func(t *testing.T) {
65+
g := NewWithT(t)
66+
67+
{
68+
url, version, plainHTTP := parseOCISource(tc.url, tc.version)
69+
g.Expect(url).To(Equal(tc.expectURL))
70+
g.Expect(version).To(Equal(tc.expectVersion))
71+
g.Expect(plainHTTP).To(BeFalse())
72+
}
73+
74+
{
75+
url, version, plainHTTP := parseOCISource(fmt.Sprintf("http://%s", tc.url), tc.version)
76+
g.Expect(url).To(Equal(tc.expectURL))
77+
g.Expect(version).To(Equal(tc.expectVersion))
78+
g.Expect(plainHTTP).To(BeTrue())
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)