From 92ec0ca74a74e42c378560e322503e7b536aef14 Mon Sep 17 00:00:00 2001 From: xuezhaojun Date: Wed, 23 Mar 2022 17:46:23 +0800 Subject: [PATCH] Fix registry name with port (#88) Signed-off-by: xuezhaojun --- pkg/config/agent.go | 18 +++++-- pkg/config/agent_test.go | 95 +++++++++++++++++++++++++++++++++++ pkg/proxyagent/agent/agent.go | 5 +- 3 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 pkg/config/agent_test.go diff --git a/pkg/config/agent.go b/pkg/config/agent.go index 4bcb5774..0f208faa 100644 --- a/pkg/config/agent.go +++ b/pkg/config/agent.go @@ -14,12 +14,22 @@ func ValidateAgentImage() error { return nil } -func GetParsedAgentImage() (string, string, string) { - parts := strings.Split(AgentImageName, ":") +func GetParsedAgentImage() (string, string, string, error) { + imgParts := strings.Split(AgentImageName, "/") + if len(imgParts) != 2 && len(imgParts) != 3 { + // image name without registry is also legal. + return "", "", "", fmt.Errorf("invalid agent image name: %s", AgentImageName) + } + + registry := strings.Join(imgParts[0:len(imgParts)-1], "/") + + parts := strings.Split(imgParts[len(imgParts)-1], ":") + image := parts[0] + tag := "latest" if len(parts) >= 2 { tag = parts[len(parts)-1] } - imgParts := strings.Split(parts[0], "/") - return strings.Join(imgParts[0:len(imgParts)-1], "/"), imgParts[len(imgParts)-1], tag + + return registry, image, tag, nil } diff --git a/pkg/config/agent_test.go b/pkg/config/agent_test.go new file mode 100644 index 00000000..f69757ee --- /dev/null +++ b/pkg/config/agent_test.go @@ -0,0 +1,95 @@ +package config + +import ( + "testing" +) + +func TestGetParsedAgentImage(t *testing.T) { + testcases := []struct { + agentImageName string + expectErr bool + registry string + image string + tag string + }{ + { + // no registry + // no tag + "open-cluster-management.io/cluster-proxy-agent", + false, + "open-cluster-management.io", + "cluster-proxy-agent", + "latest", + }, + { + // no tag + "quay.io/open-cluster-management.io/cluster-proxy-agent", + false, + "quay.io/open-cluster-management.io", + "cluster-proxy-agent", + "latest", + }, + { + "quay.io/open-cluster-management.io/cluster-proxy-agent:v0.1.0", + false, + "quay.io/open-cluster-management.io", + "cluster-proxy-agent", + "v0.1.0", + }, + { + // registry with port + "quay.io:443/open-cluster-management.io/cluster-proxy-agent:v0.1.0", + false, + "quay.io:443/open-cluster-management.io", + "cluster-proxy-agent", + "v0.1.0", + }, + { + // registry with port + // no tag + "quay.io:443/open-cluster-management.io/cluster-proxy-agent", + false, + "quay.io:443/open-cluster-management.io", + "cluster-proxy-agent", + "latest", + }, + { + // empty image name + "", + true, + "", + "", + "", + }, + { + // wrong image name + "foo", + true, + "", + "", + "", + }, { + // wrong image name + "foo/foo/foo/foo", + true, + "", + "", + "", + }, + } + + for _, c := range testcases { + AgentImageName = c.agentImageName + r, i, tag, err := GetParsedAgentImage() + if err != nil { + if c.expectErr { + continue + } + t.Errorf("GetParsedAgentImage() error: %v", err) + } + + if r != c.registry || i != c.image || tag != c.tag { + t.Errorf("expect %s, %s, %s, but get %s, %s, %s", c.registry, c.image, c.tag, r, i, tag) + } + } +} diff --git a/pkg/proxyagent/agent/agent.go b/pkg/proxyagent/agent/agent.go index 67683dbe..6889af7d 100644 --- a/pkg/proxyagent/agent/agent.go +++ b/pkg/proxyagent/agent/agent.go @@ -190,7 +190,10 @@ func GetClusterProxyValueFunc( keyDataBase64 = base64.StdEncoding.EncodeToString(agentClientSecret.Data[corev1.TLSPrivateKeyKey]) } - registry, image, tag := config.GetParsedAgentImage() + registry, image, tag, err := config.GetParsedAgentImage() + if err != nil { + return nil, err + } return map[string]interface{}{ "agentDeploymentName": "cluster-proxy-proxy-agent", "serviceDomain": "svc.cluster.local",