diff --git a/integration/creategetdelete_test.go b/integration/creategetdelete_test.go index dbe017c7fa..69adbed630 100644 --- a/integration/creategetdelete_test.go +++ b/integration/creategetdelete_test.go @@ -14,6 +14,7 @@ import ( "k8s.io/client-go/tools/clientcmd" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4" + "github.com/weaveworks/eksctl/pkg/ctl/cmdutils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -21,7 +22,6 @@ import ( "github.com/weaveworks/eksctl/pkg/testutils/aws" . "github.com/weaveworks/eksctl/pkg/testutils/matchers" - "github.com/weaveworks/eksctl/pkg/utils" ) var _ = Describe("(Integration) Create, Get, Scale & Delete", func() { @@ -60,7 +60,7 @@ var _ = Describe("(Integration) Create, Get, Scale & Delete", func() { fmt.Fprintf(GinkgoWriter, "Using kubeconfig: %s\n", kubeconfigPath) if clusterName == "" { - clusterName = utils.ClusterName("", "") + clusterName = cmdutils.ClusterName("", "") } eksctl("create", "cluster", diff --git a/pkg/ctl/cmdutils/namers.go b/pkg/ctl/cmdutils/namers.go new file mode 100644 index 0000000000..c3051f91a2 --- /dev/null +++ b/pkg/ctl/cmdutils/namers.go @@ -0,0 +1,57 @@ +package cmdutils + +import ( + "fmt" + "math/rand" + "time" + + "github.com/kubicorn/kubicorn/pkg/namer" +) + +const ( + randNodeGroupNameLength = 8 + randNodeGroupNameComponents = "abcdef0123456789" +) + +var r = rand.New(rand.NewSource(time.Now().UnixNano())) + +// NodeGroupName generates a name string when a and b are empty strings. +// If either a or b are non-empty, it returns whichever is non-empty. +// If neither a nor b are empty, it returns empty name, to indicate +// ambiguous usage. +// It uses a different naming scheme from ClusterName, so that users can +// easily distinguish a cluster name from nodegroup name. +func NodeGroupName(a, b string) string { + return useNameOrGenerate(a, b, func() string { + name := make([]byte, randNodeGroupNameLength) + for i := 0; i < randNodeGroupNameLength; i++ { + name[i] = randNodeGroupNameComponents[r.Intn(len(randNodeGroupNameComponents))] + } + return fmt.Sprintf("ng-%s", string(name)) + }) +} + +// useNameOrGenerate picks one of the provided strings or generates a +// new one using the provided generate function +func useNameOrGenerate(a, b string, generate func() string) string { + if a != "" && b != "" { + return "" + } + if a != "" { + return a + } + if b != "" { + return b + } + return generate() +} + +// ClusterName generates a name string when a and b are empty strings. +// If either a or b are non-empty, it returns whichever is non-empty. +// If neither a nor b are empty, it returns empty name, to indicate +// ambiguous usage. +func ClusterName(a, b string) string { + return useNameOrGenerate(a, b, func() string { + return fmt.Sprintf("%s-%d", namer.RandomName(), time.Now().Unix()) + }) +} diff --git a/pkg/ctl/create/nodegroup_utils.go b/pkg/ctl/create/nodegroup_utils.go index 199a51875c..b4a9bfb16a 100644 --- a/pkg/ctl/create/nodegroup_utils.go +++ b/pkg/ctl/create/nodegroup_utils.go @@ -2,39 +2,13 @@ package create import ( "fmt" - "math/rand" - "time" "github.com/spf13/cobra" "github.com/weaveworks/eksctl/pkg/ami" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4" "github.com/weaveworks/eksctl/pkg/ctl/cmdutils" - "github.com/weaveworks/eksctl/pkg/utils" ) -const ( - randNodeGroupNameLength = 8 - randNodeGroupNameComponents = "abcdef0123456789" -) - -var r = rand.New(rand.NewSource(time.Now().UnixNano())) - -// NodeGroupName generates a name string when a and b are empty strings. -// If either a or b are non-empty, it returns whichever is non-empty. -// If neither a nor b are empty, it returns empty name, to indicate -// ambiguous usage. -// It uses a different naming scheme from ClusterName, so that users can -// easily distinguish a cluster name from nodegroup name. -func NodeGroupName(a, b string) string { - return utils.UseNameOrGenerate(a, b, func() string { - name := make([]byte, randNodeGroupNameLength) - for i := 0; i < randNodeGroupNameLength; i++ { - name[i] = randNodeGroupNameComponents[r.Intn(len(randNodeGroupNameComponents))] - } - return fmt.Sprintf("ng-%s", string(name)) - }) -} - func configureNodeGroups(ngFilter *cmdutils.NodeGroupFilter, nodeGroups []*api.NodeGroup, cmd *cobra.Command) error { return ngFilter.CheckEachNodeGroup(nodeGroups, func(i int, ng *api.NodeGroup) error { if ng.AllowSSH && ng.SSHPublicKeyPath == "" { diff --git a/pkg/utils/namer.go b/pkg/utils/namer.go deleted file mode 100644 index fa0201a7df..0000000000 --- a/pkg/utils/namer.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "fmt" - "time" - - "github.com/kubicorn/kubicorn/pkg/namer" -) - -// UseNameOrGenerate picks one of the provided strings or generates a -// new one using the provided generate function -func UseNameOrGenerate(a, b string, generate func() string) string { - if a != "" && b != "" { - return "" - } - if a != "" { - return a - } - if b != "" { - return b - } - return generate() -} - -// ClusterName generates a name string when a and b are empty strings. -// If either a or b are non-empty, it returns whichever is non-empty. -// If neither a nor b are empty, it returns empty name, to indicate -// ambiguous usage. -func ClusterName(a, b string) string { - return UseNameOrGenerate(a, b, func() string { - return fmt.Sprintf("%s-%d", namer.RandomName(), time.Now().Unix()) - }) -}