Clean up and make more image handling consistent

- Cleans up some of the image registry handling by
initializing values in a more consistent and clear
manner.

- Adds the Docker library registry to the list of
values that is override-able.

- Adds a few branches to logic to ensure each registry
is handled the same.
This commit is contained in:
John Schnake
2021-03-31 14:38:07 -05:00
parent add13090e2
commit 0adf70b61b
3 changed files with 172 additions and 190 deletions

View File

@@ -24,108 +24,86 @@ import (
"k8s.io/apimachinery/pkg/util/diff"
)
type result struct {
result string
err error
}
var registryTests = []struct {
in string
out result
}{
{
"docker.io/library/test:123",
result{
result: "test.io/library/test:123",
err: nil,
},
},
{
"docker.io/library/test",
result{
result: "test.io/library/test",
err: nil,
},
},
{
"test",
result{
result: "test.io/library/test",
err: nil,
},
},
{
"gcr.io/kubernetes-e2e-test-images/test:123",
result{
result: "test.io/kubernetes-e2e-test-images/test:123",
err: nil,
},
},
{
"k8s.gcr.io/test:123",
result{
result: "test.io/test:123",
err: nil,
},
},
{
"gcr.io/k8s-authenticated-test/test:123",
result{
result: "test.io/k8s-authenticated-test/test:123",
err: nil,
},
},
{
"gcr.io/google-samples/test:latest",
result{
result: "test.io/google-samples/test:latest",
err: nil,
},
},
{
"gcr.io/gke-release/test:latest",
result{
result: "test.io/gke-release/test:latest",
err: nil,
},
},
{
"k8s.gcr.io/sig-storage/test:latest",
result{
result: "test.io/sig-storage/test:latest",
err: nil,
},
},
{
"unknwon.io/google-samples/test:latest",
result{
result: "",
err: fmt.Errorf("Registry: unknwon.io/google-samples is missing in test/utils/image/manifest.go, please add the registry, otherwise the test will fail on air-gapped clusters"),
},
},
}
// ToDo Add Benchmark
func TestReplaceRegistryInImageURL(t *testing.T) {
registryTests := []struct {
in string
out string
expectErr error
}{
{
in: "docker.io/library/test:123",
out: "test.io/library/test:123",
}, {
in: "docker.io/library/test",
out: "test.io/library/test",
}, {
in: "test",
out: "test.io/library/test",
}, {
in: "gcr.io/kubernetes-e2e-test-images/test:123",
out: "test.io/kubernetes-e2e-test-images/test:123",
}, {
in: "k8s.gcr.io/test:123",
out: "test.io/test:123",
}, {
in: "gcr.io/k8s-authenticated-test/test:123",
out: "test.io/k8s-authenticated-test/test:123",
}, {
in: "gcr.io/google-samples/test:latest",
out: "test.io/google-samples/test:latest",
}, {
in: "gcr.io/gke-release/test:latest",
out: "test.io/gke-release/test:latest",
}, {
in: "k8s.gcr.io/sig-storage/test:latest",
out: "test.io/sig-storage/test:latest",
}, {
in: "invalid.com/invalid/test:latest",
out: "test.io/invalid/test:latest",
}, {
in: "mcr.microsoft.com/test:latest",
out: "test.io/microsoft/test:latest",
}, {
in: "k8s.gcr.io/e2e-test-images/test:latest",
out: "test.io/promoter/test:latest",
}, {
in: "k8s.gcr.io/build-image/test:latest",
out: "test.io/build/test:latest",
}, {
in: "gcr.io/authenticated-image-pulling/test:latest",
out: "test.io/gcAuth/test:latest",
}, {
in: "unknwon.io/google-samples/test:latest",
expectErr: fmt.Errorf("Registry: unknwon.io/google-samples is missing in test/utils/image/manifest.go, please add the registry, otherwise the test will fail on air-gapped clusters"),
},
}
// Set custom registries
dockerLibraryRegistry = "test.io/library"
e2eRegistry = "test.io/kubernetes-e2e-test-images"
gcRegistry = "test.io"
gcrReleaseRegistry = "test.io/gke-release"
PrivateRegistry = "test.io/k8s-authenticated-test"
sampleRegistry = "test.io/google-samples"
sigStorageRegistry = "test.io/sig-storage"
reg := RegistryList{
DockerLibraryRegistry: "test.io/library",
E2eRegistry: "test.io/kubernetes-e2e-test-images",
GcRegistry: "test.io",
GcrReleaseRegistry: "test.io/gke-release",
PrivateRegistry: "test.io/k8s-authenticated-test",
SampleRegistry: "test.io/google-samples",
SigStorageRegistry: "test.io/sig-storage",
InvalidRegistry: "test.io/invalid",
MicrosoftRegistry: "test.io/microsoft",
PromoterE2eRegistry: "test.io/promoter",
BuildImageRegistry: "test.io/build",
GcAuthenticatedRegistry: "test.io/gcAuth",
}
for _, tt := range registryTests {
t.Run(tt.in, func(t *testing.T) {
s, err := ReplaceRegistryInImageURL(tt.in)
s, err := replaceRegistryInImageURLWithList(tt.in, reg)
if err != nil && err.Error() != tt.out.err.Error() {
t.Errorf("got %q, want %q", err, tt.out.err)
if err != nil && err.Error() != tt.expectErr.Error() {
t.Errorf("got %q, want %q", err, tt.expectErr)
}
if s != tt.out.result {
t.Errorf("got %q, want %q", s, tt.out.result)
if s != tt.out {
t.Errorf("got %q, want %q", s, tt.out)
}
})
}