Skip to content

Commit

Permalink
added validation for clustername and nodegroup name for cloudformatio…
Browse files Browse the repository at this point in the history
…n error and updated docs
  • Loading branch information
Himangini committed Jul 8, 2021
1 parent 0db929e commit 99b582a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/ctl/cmdutils/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmdutils
import (
"fmt"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -103,6 +104,12 @@ func GetNameArg(args []string) string {
return ""
}

// IsValidNameArg checks whether the name contains invalid characters
func IsValidNameArg(name string) bool {
re := regexp.MustCompile(`[^a-zA-Z0-9\-]+`)
return !re.MatchString(name)
}

// AddCommonFlagsForAWS adds common flags for api.ProviderConfig
func AddCommonFlagsForAWS(group *NamedFlagSetGroup, p *api.ProviderConfig, addCfnOptions bool) {
group.InFlagSet("AWS client", func(fs *pflag.FlagSet) {
Expand Down Expand Up @@ -219,6 +226,11 @@ func ErrFlagAndArg(kind, flag, arg string) error {
return fmt.Errorf("%s=%s and argument %s %s", kind, flag, arg, IncompatibleFlags)
}

// ErrInvalidName error when invalid characters for a name is provided
func ErrInvalidName(name string) error {
return fmt.Errorf("validation for %s failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*", name)
}

// ErrMustBeSet is a common error message
func ErrMustBeSet(pathOrFlag string) error {
return fmt.Errorf("%s must be set", pathOrFlag)
Expand Down
4 changes: 4 additions & 0 deletions pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func doCreateCluster(cmd *cmdutils.Cmd, ngFilter *filter.NodeGroupFilter, params
cfg := cmd.ClusterConfig
meta := cmd.ClusterConfig.Metadata

if meta.Name != "" && !cmdutils.IsValidNameArg(meta.Name) {
return cmdutils.ErrInvalidName(meta.Name)
}

printer := printers.NewJSONPrinter()

ctl, err := cmd.NewCtl()
Expand Down
20 changes: 20 additions & 0 deletions pkg/ctl/create/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var _ = Describe("create cluster", func() {
Entry("without cluster name", ""),
Entry("with cluster name as flag", "--name", "clusterName"),
Entry("with cluster name as argument", "clusterName"),
Entry("with cluster name with hyphen as flag", "--name", "my-cluster-name-is-fine10"),
Entry("with cluster name with hyphen as argument", "my-Cluster-name-is-fine10"),
// vpc networking flags
Entry("with vpc-cidr flag", "--vpc-cidr", "10.0.0.0/20"),
Entry("with vpc-private-subnets flag", "--vpc-private-subnets", "10.0.0.0/24"),
Expand Down Expand Up @@ -97,6 +99,14 @@ var _ = Describe("create cluster", func() {
args: []string{"cluster", "--invalid", "dummy"},
error: "unknown flag: --invalid",
}),
Entry("with --name option with invalid characters that are rejected by cloudformation", invalidParamsCase{
args: []string{"test-k8_cluster01"},
error: fmt.Errorf("Error: validation for test-k8_cluster01 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
Entry("with cluster name argument with invalid characters that are rejected by cloudformation", invalidParamsCase{
args: []string{"--name", "eksctl-testing-k_8_cluster01"},
error: fmt.Errorf("Error: validation for eksctl-testing-k_8_cluster01 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
)
})

Expand All @@ -120,6 +130,8 @@ var _ = Describe("create cluster", func() {
Entry("without cluster name", ""),
Entry("with cluster name as flag", "--name", "clusterName"),
Entry("with cluster name as argument", "clusterName"),
Entry("with cluster name with hyphen as flag", "--name", "my-cluster-name-is-fine10"),
Entry("with cluster name with hyphen as argument", "my-Cluster-name-is-fine10"),
// vpc networking flags
Entry("with vpc-cidr flag", "--vpc-cidr", "10.0.0.0/20"),
Entry("with vpc-private-subnets flag", "--vpc-private-subnets", "10.0.0.0/24"),
Expand Down Expand Up @@ -167,6 +179,14 @@ var _ = Describe("create cluster", func() {
args: []string{"cluster", "--invalid", "dummy"},
error: "unknown flag: --invalid",
}),
Entry("with --name option with invalid characters that are rejected by cloudformation", invalidParamsCase{
args: []string{"test-k8_cluster01"},
error: fmt.Errorf("Error: validation for test-k8_cluster01 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
Entry("with cluster name argument with invalid characters that are rejected by cloudformation", invalidParamsCase{
args: []string{"--name", "eksctl-testing-k_8_cluster01"},
error: fmt.Errorf("Error: validation for eksctl-testing-k_8_cluster01 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
)
})
})
4 changes: 4 additions & 0 deletions pkg/ctl/create/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type nodegroupOptions struct {

func createNodeGroupCmd(cmd *cmdutils.Cmd) {
createNodeGroupCmdWithRunFunc(cmd, func(cmd *cmdutils.Cmd, ng *api.NodeGroup, options nodegroupOptions) error {
if ng.Name != "" && !cmdutils.IsValidNameArg(ng.Name) {
return cmdutils.ErrInvalidName(ng.Name)
}

ngFilter := filter.NewNodeGroupFilter()
if err := cmdutils.NewCreateNodeGroupLoader(cmd, ng, ngFilter, options.CreateNGOptions, options.CreateManagedNGOptions).Load(); err != nil {
return errors.Wrap(err, "couldn't create node group filter from command line options")
Expand Down
12 changes: 12 additions & 0 deletions pkg/ctl/create/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var _ = Describe("create nodegroup", func() {
Expect(count).To(Equal(1))
},
Entry("with nodegroup name as flag", "--name", "nodegroupName"),
Entry("with nodegroup name with a hyphen as flag", "--name", "nodegroup-name"),
Entry("with nodegroup name as argument", "nodegroupName"),
Entry("with nodegroup name with a hyphen as argument", "nodegroup-name"),
Entry("with node-type flag", "--node-type", "m5.large"),
Entry("with nodes flag", "--nodes", "2"),
Entry("with nodes-min flag", "--nodes-min", "2"),
Expand Down Expand Up @@ -81,6 +83,10 @@ var _ = Describe("create nodegroup", func() {
args: []string{"--cluster", "foo", "--instance-types", "some-type"},
error: "--instance-types is only valid with managed nodegroups (--managed)",
}),
Entry("with nodegroup name as flag with invalid characters", invalidParamsCase{
args: []string{"--cluster", "clusterName", "--name", "eksctl-ng_k8s_nodegroup1"},
error: fmt.Errorf("Error: validation for eksctl-ng_k8s_nodegroup1 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
)
})

Expand All @@ -104,7 +110,9 @@ var _ = Describe("create nodegroup", func() {
},
Entry("without nodegroup name", ""),
Entry("with nodegroup name as flag", "--name", "nodegroupName"),
Entry("with nodegroup name with a hyphen as flag", "--name", "nodegroup-name"),
Entry("with nodegroup name as argument", "nodegroupName"),
Entry("with nodegroup name with a hyphen as argument", "nodegroup-name"),
Entry("with node-type flag", "--node-type", "m5.large"),
Entry("with nodes flag", "--nodes", "2"),
Entry("with nodes-min flag", "--nodes-min", "2"),
Expand Down Expand Up @@ -140,6 +148,10 @@ var _ = Describe("create nodegroup", func() {
args: []string{"--invalid", "dummy"},
error: "unknown flag: --invalid",
}),
Entry("with nodegroup name as flag with invalid characters", invalidParamsCase{
args: []string{"--name", "eksctl-ng_k8s_nodegroup1"},
error: fmt.Errorf("Error: validation for eksctl-ng_k8s_nodegroup1 failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*"),
}),
)
})
})
3 changes: 3 additions & 0 deletions userdocs/src/usage/creating-and-managing-clusters.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ nodeGroups:
imageBuilder: true
```
!!! note
The cluster name or nodegroup name can contain only alphanumeric characters (case-sensitive) and hyphens. It must start with an alphabetic character and can't be longer than 128 characters otherwise you will get a validation error. More information can be found [here](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-console-create-stack-parameters.html)
To delete this cluster, run:
```
Expand Down

0 comments on commit 99b582a

Please sign in to comment.