From 832f845a8c2f0117412094d374a5a94f64a958fb Mon Sep 17 00:00:00 2001 From: Ryota Sakamoto Date: Sun, 16 Jun 2024 18:04:30 +0900 Subject: [PATCH] generate random name --- pkg/cmd/create.go | 26 ++++++++------------------ pkg/provisioner/cluster.go | 19 +++++++++++++------ pkg/provisioner/name.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 pkg/provisioner/name.go diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 81d495a..4cdc1d4 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -14,25 +14,17 @@ var createCmd = &cobra.Command{ }, } -var createMasterCmd = &cobra.Command{ - Use: "master", - Short: "Create a new master", - RunE: func(cmd *cobra.Command, args []string) error { - return provisioner.CreateMaster(getProvisionerConfig(cmd)) - }, -} - var createWorkerCmd = &cobra.Command{ Use: "worker", - Short: "Create a new worker", + Short: "Create a new worker node", RunE: func(cmd *cobra.Command, args []string) error { - return provisioner.CreateWorker(getProvisionerConfig(cmd)) + return provisioner.CreateWorker(cmd.Flag("cluster-name").Value.String(), getProvisionerConfig(cmd)) }, } func getProvisionerConfig(cmd *cobra.Command) provisioner.Config { return provisioner.Config{ - Name: cmd.Flag("prefix").Value.String() + cmd.Flag("name").Value.String(), + Name: cmd.Flag("name").Value.String(), CPUs: cmd.Flag("cpus").Value.String(), Memory: cmd.Flag("memory").Value.String(), Disk: cmd.Flag("disk").Value.String(), @@ -41,21 +33,19 @@ func getProvisionerConfig(cmd *cobra.Command) provisioner.Config { } } -func defineCommonFlags(cmd *cobra.Command, name string) { - cmd.Flags().StringP("prefix", "p", "kubernetes-", "Prefix for the instance") - cmd.Flags().StringP("name", "n", name, "Name of the instance") +func defineCommonFlags(cmd *cobra.Command) { + cmd.Flags().StringP("cluster-name", "", "kubernetes", "Name of the cluster") + + cmd.Flags().StringP("name", "n", "", "Name of the instance. If not provided, a random name will be generated") cmd.Flags().StringP("cpus", "c", "2", "Number of CPUs") cmd.Flags().StringP("memory", "m", "4G", "Amount of memory") cmd.Flags().StringP("disk", "d", "10G", "Amount of disk space") cmd.Flags().StringP("k8s-version", "k", "v1.30.0", "Kubernetes version") cmd.Flags().StringP("image", "i", "22.04", "Image to use for the VM") - } func init() { rootCmd.AddCommand(createCmd) - createCmd.AddCommand(createMasterCmd) createCmd.AddCommand(createWorkerCmd) - defineCommonFlags(createMasterCmd, "master") - defineCommonFlags(createWorkerCmd, "worker") + defineCommonFlags(createWorkerCmd) } diff --git a/pkg/provisioner/cluster.go b/pkg/provisioner/cluster.go index 1bf321c..5e37057 100644 --- a/pkg/provisioner/cluster.go +++ b/pkg/provisioner/cluster.go @@ -23,8 +23,9 @@ type Config struct { Image string } -func CreateMaster(config Config) error { - instance, err := multipass.GetInstance(config.Name) +func CreateMaster(clusterName string, config Config) error { + instanceName := fmt.Sprintf("%s-%s", clusterName, "master") + instance, err := multipass.GetInstance(instanceName) if err != nil { return fmt.Errorf("failed to get instance: %w", err) } @@ -41,7 +42,7 @@ func CreateMaster(config Config) error { } return multipass.LaunchInstance(multipass.InstanceConfig{ - Name: config.Name, + Name: instanceName, CPUs: config.CPUs, Memory: config.Memory, Disk: config.Disk, @@ -49,8 +50,14 @@ func CreateMaster(config Config) error { }, template) } -func CreateWorker(config Config) error { - instance, err := multipass.GetInstance(config.Name) +func CreateWorker(clusterName string, config Config) error { + name := config.Name + if name == "" { + name = GetRandomName() + } + + instanceName := fmt.Sprintf("%s-%s", clusterName, name) + instance, err := multipass.GetInstance(instanceName) if err != nil { return fmt.Errorf("failed to get instance: %w", err) } @@ -67,7 +74,7 @@ func CreateWorker(config Config) error { } return multipass.LaunchInstance(multipass.InstanceConfig{ - Name: config.Name, + Name: instanceName, CPUs: config.CPUs, Memory: config.Memory, Disk: config.Disk, diff --git a/pkg/provisioner/name.go b/pkg/provisioner/name.go new file mode 100644 index 0000000..774e230 --- /dev/null +++ b/pkg/provisioner/name.go @@ -0,0 +1,38 @@ +package provisioner + +import ( + "math/rand" +) + +var randomNameList = []string{ + "apple", + "banana", + "cherry", + "darjeeling", + "elderberry", + "fig", + "grape", + "honeydew", + "imbe", + "jackfruit", + "kiwi", + "lemon", + "melon", + "nectarine", + "orange", + "papaya", + "quince", + "raambutan", + "strawberry", + "tangerine", + "ugli", + "vanilla", + "watermelon", + "ximenia", + "yuzu", + "zucchini", +} + +func GetRandomName() string { + return randomNameList[rand.Intn(len(randomNameList))] +}