From 6aee9528e1c6bc6164c8fa4f77e6148d7b06c0ae Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 15 Aug 2016 01:21:12 -0400 Subject: [PATCH] Validate that the cluster name is a valid DNS name This should help users avoid the common mistake of just specifying the name, not a fully-qualified domain name. Fix #46 --- upup/pkg/api/validation.go | 14 ++++++++++++++ upup/pkg/api/validation_test.go | 15 +++++++++++++++ upup/pkg/fi/cloudup/validation_test.go | 12 ++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 upup/pkg/api/validation_test.go diff --git a/upup/pkg/api/validation.go b/upup/pkg/api/validation.go index 53886b653c714..7642abd7a0fb2 100644 --- a/upup/pkg/api/validation.go +++ b/upup/pkg/api/validation.go @@ -2,8 +2,10 @@ package api import ( "fmt" + "k8s.io/kubernetes/pkg/util/validation" "net" "net/url" + "strings" ) func (c *Cluster) Validate(strict bool) error { @@ -13,6 +15,18 @@ func (c *Cluster) Validate(strict bool) error { return fmt.Errorf("Cluster Name is required (e.g. --name=mycluster.myzone.com)") } + { + // Must be a dns name + errs := validation.IsDNS1123Subdomain(c.Name) + if len(errs) != 0 { + return fmt.Errorf("Cluster Name must be a valid DNS name (e.g. --name=mycluster.myzone.com) errors: %s", strings.Join(errs, ", ")) + } + + if !strings.Contains(c.Name, ".") { + return fmt.Errorf("Cluster Name must be a fully-qualified DNS name (e.g. --name=mycluster.myzone.com)") + } + } + if len(c.Spec.Zones) == 0 { // TODO: Auto choose zones from region? return fmt.Errorf("must configure at least one Zone (use --zones)") diff --git a/upup/pkg/api/validation_test.go b/upup/pkg/api/validation_test.go new file mode 100644 index 0000000000000..2f65226b55d05 --- /dev/null +++ b/upup/pkg/api/validation_test.go @@ -0,0 +1,15 @@ +package api + +import ( + "testing" + "k8s.io/kubernetes/pkg/util/validation" +) + +func Test_Validate_DNS(t *testing.T) { + for _, name := range []string{"test.-", "!", "-"} { + errs := validation.IsDNS1123Subdomain(name) + if len(errs) == 0 { + t.Fatalf("Expected errors validating name %q", name) + } + } +} diff --git a/upup/pkg/fi/cloudup/validation_test.go b/upup/pkg/fi/cloudup/validation_test.go index a8bdc88c2227b..f71b4cf659af1 100644 --- a/upup/pkg/fi/cloudup/validation_test.go +++ b/upup/pkg/fi/cloudup/validation_test.go @@ -104,6 +104,18 @@ func TestValidateFull_Default_Validates(t *testing.T) { } } +func TestValidateFull_ClusterName_InvalidDNS_NoDot(t *testing.T) { + c := buildDefaultCluster(t) + c.Name = "test" + expectErrorFromValidate(t, c, "DNS name") +} + +func TestValidateFull_ClusterName_InvalidDNS_Invalid(t *testing.T) { + c := buildDefaultCluster(t) + c.Name = "test.-" + expectErrorFromValidate(t, c, "DNS name") +} + func TestValidateFull_ClusterName_Required(t *testing.T) { c := buildDefaultCluster(t) c.Name = ""