From cce66d7e8ed47d5c15615d64f891533db2bcfcd2 Mon Sep 17 00:00:00 2001 From: Ryota Sakamoto Date: Sun, 16 Jun 2024 21:23:07 +0900 Subject: [PATCH] add create cluster --- pkg/cmd/create.go | 39 +++++++++++++++++++++++++++++++------ pkg/provisioner/cluster.go | 21 ++++++++++++++++---- pkg/provisioner/instance.go | 2 +- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 8d3cd29..36ddc47 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -6,6 +6,14 @@ import ( "github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/provisioner" ) +const ( + defaultInstanceCpus = "2" + defaultInstanceMemory = "4G" + defaultInstanceDisk = "10G" + defaultK8sVersion = "v1.30.0" + defaultInstanceImage = "22.04" +) + var createCmd = &cobra.Command{ Use: "create", Short: "Create a new cluster, master or worker node", @@ -18,7 +26,26 @@ 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{}) + return provisioner.CreateCluster(cmd.Flag("cluster-name").Value.String(), + provisioner.ClusterConfig{}, + provisioner.InstanceConfig{ + Name: "master", + CPUs: defaultInstanceCpus, + Memory: defaultInstanceMemory, + Disk: defaultInstanceDisk, + K8sVersion: defaultK8sVersion, + Image: defaultInstanceImage, + }, + provisioner.InstanceConfig{ + Name: "worker", + CPUs: defaultInstanceCpus, + Memory: defaultInstanceMemory, + Disk: defaultInstanceDisk, + K8sVersion: defaultK8sVersion, + Image: defaultInstanceImage, + IsJoinCluster: true, + }, + ) }, } @@ -46,11 +73,11 @@ func getProvisionerInstanceConfig(cmd *cobra.Command) provisioner.InstanceConfig 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") - 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") + cmd.Flags().StringP("cpus", "c", defaultInstanceCpus, "Number of CPUs") + cmd.Flags().StringP("memory", "m", defaultInstanceMemory, "Amount of memory") + cmd.Flags().StringP("disk", "d", defaultInstanceDisk, "Amount of disk space") + cmd.Flags().StringP("k8s-version", "k", defaultK8sVersion, "Kubernetes version") + cmd.Flags().StringP("image", "i", defaultInstanceImage, "Image to use for the VM") cmd.Flags().BoolP("join", "j", true, "Join the cluster") } diff --git a/pkg/provisioner/cluster.go b/pkg/provisioner/cluster.go index 1a1c9df..85703c3 100644 --- a/pkg/provisioner/cluster.go +++ b/pkg/provisioner/cluster.go @@ -14,7 +14,6 @@ import ( ) type ClusterConfig struct { - Name string } type InstanceConfig struct { @@ -27,10 +26,24 @@ type InstanceConfig struct { IsJoinCluster bool } -func CreateCluster(clusterName string, config ClusterConfig) error { - slog.Debug("create cluster", slog.String("clusterName", clusterName), slog.Any("config", config)) +func CreateCluster(clusterName string, clusterConfig ClusterConfig, masterConfig InstanceConfig, workerConfig InstanceConfig) error { + slog.Debug("create cluster", slog.String("clusterName", clusterName), + slog.Any("clusterConfig", clusterConfig), + slog.Any("masterConfig", masterConfig), + slog.Any("workerConfig", workerConfig), + ) - return nil + err := CreateMaster(clusterName, masterConfig) + if err != nil { + return fmt.Errorf("failed to create master: %w", err) + } + + err = CreateWorker(clusterName, workerConfig) + if err != nil { + return fmt.Errorf("failed to create worker: %w", err) + } + + return GenerateKubeconfig(clusterName + "-master") } func CreateMaster(clusterName string, config InstanceConfig) error { diff --git a/pkg/provisioner/instance.go b/pkg/provisioner/instance.go index 493209b..8b10208 100644 --- a/pkg/provisioner/instance.go +++ b/pkg/provisioner/instance.go @@ -26,7 +26,7 @@ func LaunchInstance(clusterName string, config InstanceConfig, cloudinitConfig c return "", fmt.Errorf("instance already exists: %s", instanceName) } - template, err := cloudinit.Generate(GetWorkerTemplate(), map[string]string{ + template, err := cloudinit.Generate(cloudinitConfig, map[string]string{ "KubernetesVersion": config.K8sVersion, "Arch": "amd64", })