Skip to content
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

Extend containerd registry mirror config #1467

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions pkg/containerruntime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"

corev1 "k8s.io/api/core/v1"
Expand All @@ -45,27 +46,38 @@ func BuildConfig(opts Opts) (Config, error) {
}
}

var registryMirrors []string
// we want to match e.g. docker.io=registry.docker-cn.com, having docker.io as the first
Copy link
Contributor Author

@LittleFox94 LittleFox94 Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially worked with : instead of =, so docker.io:registry.docker-cn.com, which looks a bit nicer to me - but there are already flags with Key=Value so matching those is probably best?

// match group and registry.docker-cn.com as the second one.
registryMirrorRegexp := regexp.MustCompile(`^([a-zA-Z0-9\.-]+)=(.*)`)

if opts.ContainerdRegistryMirrors == nil {
opts.ContainerdRegistryMirrors = make(RegistryMirrorsFlags)
}

for _, mirror := range strings.Split(opts.RegistryMirrors, ",") {
if trimmedMirror := strings.TrimSpace(mirror); trimmedMirror != "" {
if !strings.HasPrefix(mirror, "http") {
trimmedMirror = "https://" + mirror
registry := "docker.io"

if matches := registryMirrorRegexp.FindStringSubmatch(trimmedMirror); matches != nil {
registry = matches[1]
trimmedMirror = matches[2]
}

if !strings.HasPrefix(trimmedMirror, "http") {
trimmedMirror = "https://" + trimmedMirror
}

_, err := url.Parse(trimmedMirror)
if err != nil {
return Config{}, fmt.Errorf("incorrect mirror provided: %w", err)
}

registryMirrors = append(registryMirrors, trimmedMirror)
}
}
if opts.ContainerdRegistryMirrors[registry] == nil {
opts.ContainerdRegistryMirrors[registry] = make([]string, 0, 1)
}

if len(registryMirrors) > 0 {
if opts.ContainerdRegistryMirrors == nil {
opts.ContainerdRegistryMirrors = make(RegistryMirrorsFlags)
opts.ContainerdRegistryMirrors[registry] = append(opts.ContainerdRegistryMirrors[registry], trimmedMirror)
}
opts.ContainerdRegistryMirrors["docker.io"] = registryMirrors
}

// Only validate registry credential here
Expand Down
134 changes: 134 additions & 0 deletions pkg/containerruntime/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright 2022 The Machine Controller Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package containerruntime

import (
"errors"
"fmt"
"testing"
)

func TestContainerdRegistryMirror(t *testing.T) {
type testCase struct {
desc string
flag string
expectedMirrors map[string][]string
expectedError error
}

testCases := []testCase{
{
desc: "no registry mirrors set",
flag: "",
expectedMirrors: map[string][]string{},
expectedError: nil,
},

{
desc: "registry mirror without name and protocol",
flag: "registry-v1.docker.io",
expectedMirrors: map[string][]string{
"docker.io": {"https://registry-v1.docker.io"},
},
expectedError: nil,
},
{
desc: "multiple registry mirrors without name, with and without protocol",
flag: "registry-v1.docker.io,http://registry.docker-cn.com",
expectedMirrors: map[string][]string{
"docker.io": {
"https://registry-v1.docker.io",
"http://registry.docker-cn.com",
},
},
expectedError: nil,
},

{
desc: "registry mirror with name and without protocol",
flag: "quay.io=my-quay-io-mirror.example.com",
expectedMirrors: map[string][]string{
"quay.io": {"https://my-quay-io-mirror.example.com"},
},
expectedError: nil,
},
{
desc: "registry mirror with name and protocol",
flag: "quay.io=http://my-quay-io-mirror.example.com",
expectedMirrors: map[string][]string{
"quay.io": {"http://my-quay-io-mirror.example.com"},
},
expectedError: nil,
},
{
desc: "multiple registry mirrors with same name",
flag: "quay.io=http://my-quay-io-mirror.example.com,quay.io=example.net",
expectedMirrors: map[string][]string{
"quay.io": {
"http://my-quay-io-mirror.example.com",
"https://example.net",
},
},
expectedError: nil,
},

{
desc: "complex example",
flag: "quay.io=http://my-quay-io-mirror.example.com,quay.io=example.net," +
"registry-v1.docker.io,http://registry.docker-cn.com," +
"ghcr.io=http://foo/bar",
expectedMirrors: map[string][]string{
"quay.io": {
"http://my-quay-io-mirror.example.com",
"https://example.net",
},
"docker.io": {
"https://registry-v1.docker.io",
"http://registry.docker-cn.com",
},
"ghcr.io": {
"http://foo/bar",
},
},
expectedError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
opts := Opts{
ContainerRuntime: containerdName,
RegistryMirrors: tc.flag,
}

config, err := BuildConfig(opts)
if tc.expectedError != nil {
if !errors.Is(err, tc.expectedError) {
t.Errorf("expected error %q but got %q", tc.expectedError, err)
}
}

if err != nil {
t.Errorf("expected success but got error: %q", err)
}

if fmt.Sprint(config.RegistryMirrors) != fmt.Sprint(tc.expectedMirrors) {
t.Errorf("expected to get %v instead got: %v", tc.expectedMirrors, config.RegistryMirrors)
}
})
}
}