Skip to content

Commit

Permalink
fix: failed to add cluster when the cluster server address is ipv6 (a…
Browse files Browse the repository at this point in the history
…rgoproj#8204) (argoproj#15350)

* fix: failed to add cluster when the cluster server address is ipv6 (argoproj#8204)

Signed-off-by: huyinhou <[email protected]>

* fix: failed to add cluster when the cluster server address is ipv6 (argoproj#8204)

Signed-off-by: huyinhou <[email protected]>

* remove unused import

Signed-off-by: huyinhou <[email protected]>

* fix: lowercase URI secret name; abbreviated IPv6 address

Signed-off-by: huyinhou <[email protected]>

---------

Signed-off-by: huyinhou <[email protected]>
Signed-off-by: Jimmy Neville <[email protected]>
  • Loading branch information
huyinhou authored and Jneville0815 committed Sep 30, 2023
1 parent dc80efa commit 3dcf0e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
20 changes: 20 additions & 0 deletions util/db/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ func Test_URIToSecretName(t *testing.T) {
name, err := URIToSecretName("cluster", "http://foo")
assert.NoError(t, err)
assert.Equal(t, "cluster-foo-752281925", name)

name, err = URIToSecretName("cluster", "http://thelongestdomainnameintheworld.argocd-project.com:3000")
assert.NoError(t, err)
assert.Equal(t, "cluster-thelongestdomainnameintheworld.argocd-project.com-2721640553", name)

name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]")
assert.NoError(t, err)
assert.Equal(t, "cluster-fe80--1ff-fe23-4567-890a-3877258831", name)

name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]:8000")
assert.NoError(t, err)
assert.Equal(t, "cluster-fe80--1ff-fe23-4567-890a-664858999", name)

name, err = URIToSecretName("cluster", "http://[FE80::1FF:FE23:4567:890A]:8000")
assert.NoError(t, err)
assert.Equal(t, "cluster-fe80--1ff-fe23-4567-890a-682802007", name)

name, err = URIToSecretName("cluster", "http://:/abc")
assert.NoError(t, err)
assert.Equal(t, "cluster--1969338796", name)
}

func Test_secretToCluster(t *testing.T) {
Expand Down
20 changes: 19 additions & 1 deletion util/db/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package db
import (
"fmt"
"hash/fnv"
"net/netip"
"net/url"
"strconv"
"strings"
"time"

"context"

log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -155,8 +157,24 @@ func URIToSecretName(uriType, uri string) (string, error) {
if err != nil {
return "", err
}
host := parsedURI.Host
if strings.HasPrefix(host, "[") {
last := strings.Index(host, "]")
if last >= 0 {
addr, err := netip.ParseAddr(host[1:last])
if err != nil {
return "", err
}
host = strings.ReplaceAll(addr.String(), ":", "-")
}
} else {
last := strings.Index(host, ":")
if last >= 0 {
host = host[0:last]
}
}
h := fnv.New32a()
_, _ = h.Write([]byte(uri))
host := strings.ToLower(strings.Split(parsedURI.Host, ":")[0])
host = strings.ToLower(host)
return fmt.Sprintf("%s-%s-%v", uriType, host, h.Sum32()), nil
}

0 comments on commit 3dcf0e9

Please sign in to comment.