Skip to content

Commit 81b5a6d

Browse files
committed
feat: Allow configuration of default namespace for registries (#91)
* feat: Allow configuration of default namespace for registries * Include defaultns
1 parent 1a6e238 commit 81b5a6d

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

.github/actions/spelling/allow.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ ctx
3838
Ctx
3939
deadcode
4040
Debugf
41+
defaultns
4142
dest
4243
dexidp
4344
dirname

docs/configuration/registries.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ registries:
2424
api_url: https://registry-1.docker.io
2525
ping: yes
2626
credentials: secret:foo/bar#creds
27+
defaultns: library
2728
- name: Google Container Registry
2829
api_url: https://gcr.io
2930
prefix: gcr.io
@@ -63,6 +64,11 @@ following semantics:
6364
* `insecure` (optional) if set to true, does not validate the TLS certificate
6465
for the connection to the registry. Use with care.
6566

67+
* `defaultns` (optional) defines a default namespace for images that do not
68+
specify one. For example, Docker Hub uses the default namespace `library`
69+
which turns an image specification of `nginx:latest` into the canonical name
70+
`library/nginx:latest`.
71+
6672
* `tagsortmode` (optional) defines whether and how the list of tags is sorted
6773
chronologically by the registry. Valid values are `latest_first` (the last
6874
pushed image will appear first in list), `latest_last` (the last pushed image
@@ -84,6 +90,7 @@ data:
8490
api_url: https://registry-1.docker.io
8591
ping: yes
8692
credentials: secret:foo/bar#creds
93+
defaultns: library
8794
- name: Google Container Registry
8895
api_url: https://gcr.io
8996
prefix: gcr.io

pkg/registry/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type RegistryConfiguration struct {
1919
TagSortMode string `yaml:"tagsortmode,omitempty"`
2020
Prefix string `yaml:"prefix,omitempty"`
2121
Insecure bool `yaml:"insecure,omitempty"`
22+
DefaultNS string `yaml:"defaultns,omitempty"`
2223
}
2324

2425
// RegistryList contains multiple RegistryConfiguration items
@@ -45,7 +46,7 @@ func LoadRegistryConfiguration(path string, clear bool) error {
4546
}
4647

4748
for _, reg := range registryList.Items {
48-
err = AddRegistryEndpoint(reg.Prefix, reg.Name, reg.ApiURL, reg.Credentials, reg.Insecure)
49+
err = AddRegistryEndpoint(reg.Prefix, reg.Name, reg.ApiURL, reg.Credentials, reg.DefaultNS, reg.Insecure)
4950
if err != nil {
5051
return err
5152
}

pkg/registry/endpoints.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type RegistryEndpoint struct {
4949
Ping bool
5050
Credentials string
5151
Insecure bool
52+
DefaultNS string
5253
TagListSort TagListSort
5354
Cache cache.ImageTagCache
5455

@@ -63,6 +64,7 @@ var defaultRegistries map[string]*RegistryEndpoint = map[string]*RegistryEndpoin
6364
RegistryAPI: "https://registry-1.docker.io",
6465
Ping: true,
6566
Insecure: false,
67+
DefaultNS: "library",
6668
Cache: cache.NewMemCache(),
6769
},
6870
"gcr.io": {
@@ -98,7 +100,7 @@ var registries map[string]*RegistryEndpoint = make(map[string]*RegistryEndpoint)
98100
var registryLock sync.RWMutex
99101

100102
// AddRegistryEndpoint adds registry endpoint information with the given details
101-
func AddRegistryEndpoint(prefix, name, apiUrl, credentials string, insecure bool) error {
103+
func AddRegistryEndpoint(prefix, name, apiUrl, credentials, defaultNS string, insecure bool) error {
102104
registryLock.Lock()
103105
defer registryLock.Unlock()
104106
registries[prefix] = &RegistryEndpoint{
@@ -108,6 +110,7 @@ func AddRegistryEndpoint(prefix, name, apiUrl, credentials string, insecure bool
108110
Credentials: credentials,
109111
Cache: cache.NewMemCache(),
110112
Insecure: insecure,
113+
DefaultNS: defaultNS,
111114
}
112115
return nil
113116
}
@@ -158,6 +161,7 @@ func (ep *RegistryEndpoint) DeepCopy() *RegistryEndpoint {
158161
newEp.TagListSort = ep.TagListSort
159162
newEp.Cache = cache.NewMemCache()
160163
newEp.Insecure = ep.Insecure
164+
newEp.DefaultNS = ep.DefaultNS
161165
return newEp
162166
}
163167

pkg/registry/endpoints_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Test_GetEndpoints(t *testing.T) {
3131

3232
func Test_AddEndpoint(t *testing.T) {
3333
t.Run("Add new endpoint", func(t *testing.T) {
34-
err := AddRegistryEndpoint("example.com", "Example", "https://example.com", "", false)
34+
err := AddRegistryEndpoint("example.com", "Example", "https://example.com", "", "", false)
3535
require.NoError(t, err)
3636
})
3737
t.Run("Get example.com endpoint", func(t *testing.T) {
@@ -42,14 +42,16 @@ func Test_AddEndpoint(t *testing.T) {
4242
assert.Equal(t, ep.RegistryName, "Example")
4343
assert.Equal(t, ep.RegistryAPI, "https://example.com")
4444
assert.Equal(t, ep.Insecure, false)
45+
assert.Equal(t, ep.DefaultNS, "")
4546
})
4647
t.Run("Change existing endpoint", func(t *testing.T) {
47-
err := AddRegistryEndpoint("example.com", "Example", "https://example.com", "", true)
48+
err := AddRegistryEndpoint("example.com", "Example", "https://example.com", "", "library", true)
4849
require.NoError(t, err)
4950
ep, err := GetRegistryEndpoint("example.com")
5051
require.NoError(t, err)
5152
require.NotNil(t, ep)
5253
assert.Equal(t, ep.Insecure, true)
54+
assert.Equal(t, ep.DefaultNS, "library")
5355
})
5456
}
5557

pkg/registry/registry.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ func (endpoint *RegistryEndpoint) GetTags(img *image.ContainerImage, regClient R
2323
var imgTag *tag.ImageTag
2424
var err error
2525

26-
// DockerHub has a special namespace 'library', that is hidden from the user
27-
// FIXME: How do other registries handle this?
26+
// Some registries have a default namespace that is used when the image name
27+
// doesn't specify one. For example at Docker Hub, this is 'library'.
2828
var nameInRegistry string
29-
if len := len(strings.Split(img.ImageName, "/")); len == 1 {
30-
nameInRegistry = "library/" + img.ImageName
29+
if len := len(strings.Split(img.ImageName, "/")); len == 1 && endpoint.DefaultNS != "" {
30+
nameInRegistry = endpoint.DefaultNS + "/" + img.ImageName
31+
log.Debugf("Using canonical image name '%s' for image '%s'", nameInRegistry, img.ImageName)
3132
} else {
3233
nameInRegistry = img.ImageName
3334
}

0 commit comments

Comments
 (0)