From c85c98bed4b41c5166b9b59cc558d7d5fd7eb3d9 Mon Sep 17 00:00:00 2001 From: Ryota Sakamoto Date: Sun, 16 Jun 2024 19:21:14 +0900 Subject: [PATCH] add base for create cluster command --- pkg/cmd/create.go | 23 ++++++++++++++++------- pkg/provisioner/cluster.go | 16 +++++++++++++--- pkg/provisioner/instance.go | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 8c8f3b8..8d3cd29 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -14,18 +14,26 @@ var createCmd = &cobra.Command{ }, } +var createClusterCmd = &cobra.Command{ + Use: "cluster", + Short: "Create a new cluster", + RunE: func(cmd *cobra.Command, args []string) error { + return provisioner.CreateCluster(cmd.Flag("cluster-name").Value.String(), provisioner.ClusterConfig{}) + }, +} + var createWorkerCmd = &cobra.Command{ Use: "worker", Short: "Create a new worker node", RunE: func(cmd *cobra.Command, args []string) error { - return provisioner.CreateWorker(cmd.Flag("cluster-name").Value.String(), getProvisionerConfig(cmd)) + return provisioner.CreateWorker(cmd.Flag("cluster-name").Value.String(), getProvisionerInstanceConfig(cmd)) }, } -func getProvisionerConfig(cmd *cobra.Command) provisioner.Config { +func getProvisionerInstanceConfig(cmd *cobra.Command) provisioner.InstanceConfig { join, _ := cmd.Flags().GetBool("join") - return provisioner.Config{ + return provisioner.InstanceConfig{ Name: cmd.Flag("name").Value.String(), CPUs: cmd.Flag("cpus").Value.String(), Memory: cmd.Flag("memory").Value.String(), @@ -36,9 +44,7 @@ func getProvisionerConfig(cmd *cobra.Command) provisioner.Config { } } -func defineCommonFlags(cmd *cobra.Command) { - cmd.Flags().StringP("cluster-name", "", "kubernetes", "Name of the cluster") - +func defineWorkerFlags(cmd *cobra.Command) { 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") @@ -50,6 +56,9 @@ func defineCommonFlags(cmd *cobra.Command) { func init() { rootCmd.AddCommand(createCmd) + createCmd.AddCommand(createClusterCmd) createCmd.AddCommand(createWorkerCmd) - defineCommonFlags(createWorkerCmd) + + createCmd.PersistentFlags().StringP("cluster-name", "", "kubernetes", "Name of the cluster") + defineWorkerFlags(createWorkerCmd) } diff --git a/pkg/provisioner/cluster.go b/pkg/provisioner/cluster.go index e144327..1a1c9df 100644 --- a/pkg/provisioner/cluster.go +++ b/pkg/provisioner/cluster.go @@ -13,7 +13,11 @@ import ( "github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/multipass" ) -type Config struct { +type ClusterConfig struct { + Name string +} + +type InstanceConfig struct { Name string CPUs string Memory string @@ -23,7 +27,13 @@ type Config struct { IsJoinCluster bool } -func CreateMaster(clusterName string, config Config) error { +func CreateCluster(clusterName string, config ClusterConfig) error { + slog.Debug("create cluster", slog.String("clusterName", clusterName), slog.Any("config", config)) + + return nil +} + +func CreateMaster(clusterName string, config InstanceConfig) error { slog.Debug("create master", slog.String("clusterName", clusterName), slog.Any("config", config)) config.Name = "master" @@ -35,7 +45,7 @@ func CreateMaster(clusterName string, config Config) error { return nil } -func CreateWorker(clusterName string, config Config) error { +func CreateWorker(clusterName string, config InstanceConfig) error { slog.Debug("create worker", slog.String("clusterName", clusterName), slog.Any("config", config)) instanceName, err := LaunchInstance(clusterName, config, GetWorkerTemplate()) diff --git a/pkg/provisioner/instance.go b/pkg/provisioner/instance.go index ecec651..493209b 100644 --- a/pkg/provisioner/instance.go +++ b/pkg/provisioner/instance.go @@ -8,7 +8,7 @@ import ( "github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/multipass" ) -func LaunchInstance(clusterName string, config Config, cloudinitConfig cloudinit.Config) (string, error) { +func LaunchInstance(clusterName string, config InstanceConfig, cloudinitConfig cloudinit.Config) (string, error) { name := config.Name if name == "" { name = GetRandomName()