Skip to content

Commit

Permalink
Fix registry name with port (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: xuezhaojun <zxue@redhat.com>
  • Loading branch information
xuezhaojun authored Mar 23, 2022
1 parent 925dcd4 commit 92ec0ca
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
18 changes: 14 additions & 4 deletions pkg/config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
95 changes: 95 additions & 0 deletions pkg/config/agent_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
5 changes: 4 additions & 1 deletion pkg/proxyagent/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 92ec0ca

Please sign in to comment.