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)

Signed-off-by: huyinhou <[email protected]>
  • Loading branch information
huyinhou committed Sep 5, 2023
1 parent eba40d4 commit b31b1db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions util/db/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ 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-projec-2721640553", name)

name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]")
assert.NoError(t, err)
assert.Equal(t, "cluster-fe80-0000-0000-0000-01ff-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-0000-0000-0000-01ff-fe23-4567-890a-664858999", name)
}

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

"context"

"regexp"

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 +159,20 @@ func URIToSecretName(uriType, uri string) (string, error) {
if err != nil {
return "", err
}
re := regexp.MustCompile(`:\d+$`)
host := re.ReplaceAllLiteralString(parsedURI.Host, "")
if strings.Contains(host, ":") {
addr, err := netip.ParseAddr(host[1 : len(host)-1])
if err != nil {
return "", err
}
host = strings.ReplaceAll(addr.StringExpanded(), ":", "-")
}
length := len(host) + len(uriType) + 12
if length > 63 {
host = host[0 : len(host)-(length-63)]
}
h := fnv.New32a()
_, _ = h.Write([]byte(uri))
host := strings.ToLower(strings.Split(parsedURI.Host, ":")[0])
return fmt.Sprintf("%s-%s-%v", uriType, host, h.Sum32()), nil
}

0 comments on commit b31b1db

Please sign in to comment.