-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add utils.CleanHostname * clean hostname when joining nodes or creating tokens * do not hard-code timeouts in join process
- Loading branch information
1 parent
00e7b19
commit 8e12ff0
Showing
6 changed files
with
92 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/net/idna" | ||
) | ||
|
||
// CleanHostname sanitises hostnames. | ||
func CleanHostname(hostname string) (string, error) { | ||
clean, err := idna.Lookup.ToASCII(hostname) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse hostname %q: %w", hostname, err) | ||
} | ||
if strings.HasSuffix(hostname, ".") { | ||
return "", fmt.Errorf("hostname cannot end with a dot (%q)", ".") | ||
} | ||
return clean, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/canonical/k8s/pkg/utils" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCleanHostname(t *testing.T) { | ||
for _, tc := range []struct { | ||
hostname string | ||
expectHostname string | ||
expectValid bool | ||
}{ | ||
{hostname: "w1", expectHostname: "w1", expectValid: true}, | ||
{hostname: "w1.internal", expectHostname: "w1.internal", expectValid: true}, | ||
{hostname: "w1.test.domain", expectHostname: "w1.test.domain", expectValid: true}, | ||
{hostname: "w1-with-dash", expectHostname: "w1-with-dash", expectValid: true}, | ||
{hostname: "Capital", expectHostname: "capital", expectValid: true}, | ||
{hostname: "dash-end-"}, | ||
{hostname: "dot-end."}, | ||
{hostname: "w1-with_underscore"}, | ||
{hostname: "spaces 123"}, | ||
{hostname: "special!@*!^%#*&$"}, | ||
} { | ||
t.Run(tc.hostname, func(t *testing.T) { | ||
g := NewWithT(t) | ||
hostname, err := utils.CleanHostname(tc.hostname) | ||
if tc.expectValid { | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(hostname).To(Equal(tc.expectHostname)) | ||
} else { | ||
g.Expect(err).To(Not(BeNil())) | ||
} | ||
}) | ||
} | ||
} |