Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that the cluster name is a valid DNS name #310

Merged
merged 1 commit into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions upup/pkg/api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)")
Expand Down
15 changes: 15 additions & 0 deletions upup/pkg/api/validation_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
12 changes: 12 additions & 0 deletions upup/pkg/fi/cloudup/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down