Skip to content

Commit

Permalink
Move namers under cmdutils
Browse files Browse the repository at this point in the history
These are only meant for interactive use in commands, not the API.
So cmdutils is the best place to keep these.
  • Loading branch information
errordeveloper committed Mar 28, 2019
1 parent 87fa483 commit dd0b039
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
4 changes: 2 additions & 2 deletions integration/creategetdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ 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"
"github.com/onsi/gomega/gexec"

"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() {
Expand Down Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions pkg/ctl/cmdutils/namers.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
26 changes: 0 additions & 26 deletions pkg/ctl/create/nodegroup_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
33 changes: 0 additions & 33 deletions pkg/utils/namer.go

This file was deleted.

0 comments on commit dd0b039

Please sign in to comment.