-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow config authors to always use Prefix #1368
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -82,8 +82,7 @@ location = "internal-registry-for-example.net/bar" | |||||
requests for the image `example.com/foo/myimage:latest` will actually work with the | ||||||
`internal-registry-for-example.net/bar/myimage:latest` image. | ||||||
|
||||||
With a `prefix` containing a wildcard in the format: "*.example.com" for subdomain matching, | ||||||
the location can be empty. In such a case, | ||||||
With any valid `prefix`, the location can be emtpy. In such a case, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
but we don’t exactly mean “empty”, we mean “missing” / “not provided by the user”; that this shows up as |
||||||
prefix matching will occur, but no reference rewrite will occur. The | ||||||
Comment on lines
+85
to
86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ultimately, the “Choosing a This is a paragraph about the |
||||||
original requested image string will be used as-is. But other settings like | ||||||
`insecure` / `blocked` / `mirrors` will be applied to matching images. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not sure we even need the example for doing nothing; we almost certainly don’t need two. Also the “But other settings like” sentence is duplicated above and below the example, probably unnecessarily. |
||||||
|
@@ -92,6 +91,10 @@ Example: Given | |||||
``` | ||||||
prefix = "*.example.com" | ||||||
``` | ||||||
OR | ||||||
``` | ||||||
prefix = "blah.example.com" | ||||||
``` | ||||||
requests for the image `blah.example.com/foo/myimage:latest` will be used | ||||||
as-is. But other settings like insecure/blocked/mirrors will be applied to matching images | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,14 +75,7 @@ func (e *Endpoint) rewriteReference(ref reference.Named, prefix string) (referen | |
} | ||
// In the case of an empty `location` field, simply return the original | ||
// input ref as-is. | ||
// | ||
// FIXME: already validated in postProcessRegistries, so check can probably | ||
// be dropped. | ||
// https://github.com/containers/image/pull/1191#discussion_r610621608 | ||
if e.Location == "" { | ||
if prefix[:2] != "*." { | ||
return nil, fmt.Errorf("invalid prefix '%v' for empty location, should be in the format: *.example.com", prefix) | ||
} | ||
return ref, nil | ||
} | ||
newNamedRef = e.Location + refString[prefixLen:] | ||
|
@@ -357,21 +350,27 @@ func (config *V2RegistriesConf) postProcessRegistries() error { | |
return err | ||
} | ||
|
||
if reg.Prefix == "" { | ||
if reg.Location == "" { | ||
return &InvalidRegistries{s: "invalid condition: both location and prefix are unset"} | ||
} | ||
reg.Prefix = reg.Location | ||
} else { | ||
// Prefix and Location cannot both be empty. | ||
// Either one at least must be set. | ||
if reg.Prefix != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simpler as if reg.Prefix == "" && reg.Location == "" { fail }
if reg.Location != "" { reg.Location, err = parseLocation(…); if err … }
if reg.Prefix == "" { reg.Prefix = reg.Location }
else { reg.Prefix, err = parseLocation(…); if err … } or perhaps // Validate individual fields
if reg.Location != "" { reg.Location, err = parseLocation(…); if err … }
if reg.Prefix != "" { reg.Prefix, err = parseLocation(…); if err … }
// Validate the combination, normalize to have reg.Prefix always set
if reg.Prefix == "" {
if reg.Location == "" { fail }
else { reg.Prefix = reg.Location }
} Either way, please remove the |
||
reg.Prefix, err = parseLocation(reg.Prefix) | ||
if err != nil { | ||
return err | ||
} | ||
// FIXME: allow config authors to always use Prefix. | ||
// https://github.com/containers/image/pull/1191#discussion_r610622495 | ||
if reg.Prefix[:2] != "*." && reg.Location == "" { | ||
return &InvalidRegistries{s: "invalid condition: location is unset and prefix is not in the format: *.example.com"} | ||
if reg.Location != "" { | ||
reg.Location, err = parseLocation(reg.Location) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} else if reg.Location != "" { | ||
reg.Prefix, err = parseLocation(reg.Location) | ||
if err != nil { | ||
return err | ||
} | ||
reg.Location = reg.Prefix | ||
} else { | ||
return &InvalidRegistries{s: "invalid condition: both location and prefix are unset"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Absolutely non-blocking, this is pre-existing and has more instances, i.e. basically a note to self to improve this later: |
||
} | ||
|
||
// make sure mirrors are valid | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,7 +291,7 @@ func TestFindRegistry(t *testing.T) { | |
|
||
registries, err := GetRegistries(sys) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 19, len(registries)) | ||
assert.Equal(t, 20, len(registries)) | ||
|
||
reg, err := FindRegistry(sys, "simple-prefix.com/foo/bar:latest") | ||
assert.Nil(t, err) | ||
|
@@ -383,6 +383,12 @@ func TestFindRegistry(t *testing.T) { | |
assert.Equal(t, "empty-prefix.com", reg.Prefix) | ||
assert.Equal(t, "empty-prefix.com", reg.Location) | ||
|
||
reg, err = FindRegistry(sys, "empty-location.set-prefix.example.com/namespace/repo") | ||
assert.Nil(t, err) | ||
assert.NotNil(t, reg) | ||
Comment on lines
+387
to
+388
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Non-blocking: These should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so would you prefer I could make a separate PR if we need to change it throughout the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly don’t think this is worth much worrying about; if you find updating pre-existing test cases fun, sure, go for it, but it’s not work that results in anything users could notice and consider valuable while using the software. As far as “clean code” goes, (Just to be explicit: ptr, err := testSubject()
require.NoError(t, err)
assert.Equal(t, …, *ptr) // would crash if testSubject returned (nil, err) and the above were assert.NoError(…) But there is a minimal practical difference; with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack, that's really helpful thanks, I'll consider introducing that in a future PR, will make a note in the test file for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I’m sorry, this is nonsense of course; |
||
assert.NotNil(t, "empty-location.set-prefix.example.com", reg.Prefix) | ||
assert.Equal(t, "", reg.Location) | ||
|
||
_, err = FindRegistry(&types.SystemContext{SystemRegistriesConfPath: "testdata/this-does-not-exist.conf"}, "example.com") | ||
assert.Error(t, err) | ||
} | ||
|
@@ -579,6 +585,14 @@ func TestRewriteReferenceSuccess(t *testing.T) { | |
{"sub.example.io/library/prefix/image", "*.example.io", "example.com", "example.com/library/prefix/image"}, | ||
{"another.sub.example.io:5000/library/prefix/image:latest", "*.sub.example.io", "example.com", "example.com:5000/library/prefix/image:latest"}, | ||
{"foo.bar.io/ns1/ns2/ns3/ns4", "*.bar.io", "omg.bbq.com/roflmao", "omg.bbq.com/roflmao/ns1/ns2/ns3/ns4"}, | ||
// Empty location with non-wildcard prefix examples. Essentially, no | ||
// rewrite occurs and original reference is used as-is. | ||
{"registry.com/foo", "registry.com", "", "registry.com/foo"}, | ||
{"abc.internal.registry.com/foo:bar", "abc.internal.registry.com", "", "abc.internal.registry.com/foo:bar"}, | ||
{"abc.internal.registry.com/foo/bar:baz", "abc.internal.registry.com", "", "abc.internal.registry.com/foo/bar:baz"}, | ||
{"alien.vs.predator.foobar.io:5000/foo@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "alien.vs.predator.foobar.io", "", | ||
"alien.vs.predator.foobar.io:5000/foo@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, | ||
{"alien.vs.predator.foobar.io:5000/omg:bbq", "alien.vs.predator.foobar.io", "", "alien.vs.predator.foobar.io:5000/omg:bbq"}, | ||
// Empty location with wildcard prefix examples. Essentially, no | ||
// rewrite occurs and original reference is used as-is. | ||
{"abc.internal.registry.com/foo:bar", "*.internal.registry.com", "", "abc.internal.registry.com/foo:bar"}, | ||
|
@@ -602,7 +616,6 @@ func TestRewriteReferenceFailedDuringParseNamed(t *testing.T) { | |
{"example.com/foo/image:latest", "example.com/foo", "example.com/path/"}, | ||
{"example.com/foo@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
"example.com/foo", "example.com"}, | ||
{"example.com:5000/image:latest", "example.com", ""}, | ||
{"example.com:5000/image:latest", "example.com", "example.com:5000"}, | ||
// Malformed prefix | ||
{"example.com/foo/image:latest", "example.com//foo", "example.com/path"}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,3 +60,6 @@ prefix="*.com" | |
|
||
[[registry]] | ||
prefix="*.foobar.io" | ||
|
||
[[registry]] | ||
prefix="empty-location.set-prefix.example.com" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above, in
the parenthesized aside doesn’t make sense, if
location
is unset,prefix
can’t be omitted.Basically I think we should document it this way:
prefix
is how the right section is chosen. (It is ~assumed to be always set.)location
is how we redirect; if unset, no redirection happens.prefix
unset +location
set) is interpreted the same as (prefix
set +location
unset)We already document the special case in the “Choosing a
[[registry]]
TOML table” section, so maybe we can just not discuss it here at all.