Skip to content

Commit

Permalink
support parser the host contains namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
whalecold committed Jan 3, 2024
1 parent 165ef19 commit f059104
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/manager/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,18 @@ func (bc *BootstrapConfig) tryExpandFQDN(host string) string {
if strings.Contains(host, ".svc.") {
return host
}

var b strings.Builder
b.Grow(len(host) + len(bc.configNamespace) + len(bc.nodeDomain) + 10)
b.WriteString(host)
b.WriteString(".")
b.WriteString(bc.configNamespace)

// the regex for Kubernetes service is [a-z]([-a-z0-9]*[a-z0-9])?, it should not contains `.` in it
// if the host not contains namespace.
if !strings.Contains(host, ".") {
b.WriteString(".")
b.WriteString(bc.configNamespace)
}

b.WriteString(".svc.")
b.WriteString(bc.nodeDomain)
return b.String()
Expand Down
38 changes: 38 additions & 0 deletions core/manager/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@ import (
"google.golang.org/protobuf/types/known/structpb"
)

func TestTryExpandFQDN(t *testing.T) {
testCases := []struct {
desc string
host string
want string
bsc *BootstrapConfig
}{
{
desc: "success",
host: "servicea",
want: "servicea.default.svc.cluster.local",
bsc: &BootstrapConfig{
nodeDomain: "cluster.local",
configNamespace: "default",
},
},
{
desc: "success",
host: "servicea.bookinfo",
want: "servicea.bookinfo.svc.cluster.local",
},
{
desc: "fqdn",
host: "servicea.bookinfo.svc.cluster.local",
want: "servicea.bookinfo.svc.cluster.local",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := tc.bsc.tryExpandFQDN(tc.host)
if got != tc.want {
t.Errorf("tryExpandFQDN() got = %v, want %v", got, tc.want)
}
})
}

Check failure on line 62 in core/manager/bootstrap_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
}

func TestParseMetaEnvs(t *testing.T) {
testCases := []struct {
desc string
Expand Down

0 comments on commit f059104

Please sign in to comment.