Skip to content

Commit

Permalink
generate random name
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 16, 2024
1 parent 4b03833 commit 832f845
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
26 changes: 8 additions & 18 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
}
19 changes: 13 additions & 6 deletions pkg/provisioner/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -41,16 +42,22 @@ func CreateMaster(config Config) error {
}

return multipass.LaunchInstance(multipass.InstanceConfig{
Name: config.Name,
Name: instanceName,
CPUs: config.CPUs,
Memory: config.Memory,
Disk: config.Disk,
Image: config.Image,
}, 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)
}
Expand All @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions pkg/provisioner/name.go
Original file line number Diff line number Diff line change
@@ -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))]
}

0 comments on commit 832f845

Please sign in to comment.