From 07ee0afaf23582eb7a1ddd22767447e9bba0a43c Mon Sep 17 00:00:00 2001 From: Ryota Sakamoto Date: Mon, 17 Jun 2024 00:34:06 +0900 Subject: [PATCH] add RegisterNode --- pkg/cloudinit/generator.go | 6 ++-- pkg/cloudinit/generator_test.go | 7 +++-- pkg/cmd/create.go | 53 +++++++++++++++++++-------------- pkg/provisioner/cloudinit.go | 17 +++++++++-- pkg/provisioner/cluster.go | 33 +++++++++++++------- pkg/provisioner/instance.go | 5 +--- pkg/provisioner/template.go | 4 +++ 7 files changed, 79 insertions(+), 46 deletions(-) diff --git a/pkg/cloudinit/generator.go b/pkg/cloudinit/generator.go index c1112f7..ae0c609 100644 --- a/pkg/cloudinit/generator.go +++ b/pkg/cloudinit/generator.go @@ -11,6 +11,8 @@ type Config struct { Packages []string `yaml:"packages"` WriteFiles []WriteFile `yaml:"write_files"` RunCmds []string `yaml:"runcmd"` + + Vars map[string]any `yaml:"-"` } type WriteFile struct { @@ -19,7 +21,7 @@ type WriteFile struct { Permissions string `yaml:"permissions,omitempty"` } -func (c *Config) Generate(vars map[string]string) (string, error) { +func (c *Config) Generate() (string, error) { buff := &bytes.Buffer{} if err := yaml.NewEncoder(buff).Encode(c); err != nil { return "", err @@ -34,7 +36,7 @@ package_update: true } output := &bytes.Buffer{} - if err := temp.Execute(output, vars); err != nil { + if err := temp.Execute(output, c.Vars); err != nil { return "", err } diff --git a/pkg/cloudinit/generator_test.go b/pkg/cloudinit/generator_test.go index ed1eaf5..c0d316e 100644 --- a/pkg/cloudinit/generator_test.go +++ b/pkg/cloudinit/generator_test.go @@ -28,9 +28,10 @@ func TestGenerate(t *testing.T) { RunCmds: []string{ "/use/bin/ok", }, - }).Generate(map[string]string{ - "Content": "content", - }) + Vars: map[string]any{ + "Content": "content", + }, + }).Generate() assert.NoError(t, err) expectedResult := `#cloud-config diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 36ddc47..fb23d42 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -28,21 +28,26 @@ var createClusterCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { 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.MasterConfig{ + InstanceConfig: provisioner.InstanceConfig{ + Name: "master", + CPUs: defaultInstanceCpus, + Memory: defaultInstanceMemory, + Disk: defaultInstanceDisk, + K8sVersion: defaultK8sVersion, + Image: defaultInstanceImage, + }, + IsRegisterNode: true, }, - provisioner.InstanceConfig{ - Name: "worker", - CPUs: defaultInstanceCpus, - Memory: defaultInstanceMemory, - Disk: defaultInstanceDisk, - K8sVersion: defaultK8sVersion, - Image: defaultInstanceImage, + provisioner.WorkerConfig{ + InstanceConfig: provisioner.InstanceConfig{ + Name: "worker", + CPUs: defaultInstanceCpus, + Memory: defaultInstanceMemory, + Disk: defaultInstanceDisk, + K8sVersion: defaultK8sVersion, + Image: defaultInstanceImage, + }, IsJoinCluster: true, }, ) @@ -53,20 +58,22 @@ 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(), getProvisionerInstanceConfig(cmd)) + return provisioner.CreateWorker(cmd.Flag("cluster-name").Value.String(), getWorkerConfig(cmd)) }, } -func getProvisionerInstanceConfig(cmd *cobra.Command) provisioner.InstanceConfig { +func getWorkerConfig(cmd *cobra.Command) provisioner.WorkerConfig { join, _ := cmd.Flags().GetBool("join") - return provisioner.InstanceConfig{ - Name: cmd.Flag("name").Value.String(), - CPUs: cmd.Flag("cpus").Value.String(), - Memory: cmd.Flag("memory").Value.String(), - Disk: cmd.Flag("disk").Value.String(), - K8sVersion: cmd.Flag("k8s-version").Value.String(), - Image: cmd.Flag("image").Value.String(), + return provisioner.WorkerConfig{ + InstanceConfig: provisioner.InstanceConfig{ + Name: cmd.Flag("name").Value.String(), + CPUs: cmd.Flag("cpus").Value.String(), + Memory: cmd.Flag("memory").Value.String(), + Disk: cmd.Flag("disk").Value.String(), + K8sVersion: cmd.Flag("k8s-version").Value.String(), + Image: cmd.Flag("image").Value.String(), + }, IsJoinCluster: join, } } diff --git a/pkg/provisioner/cloudinit.go b/pkg/provisioner/cloudinit.go index dc21655..13808a9 100644 --- a/pkg/provisioner/cloudinit.go +++ b/pkg/provisioner/cloudinit.go @@ -4,15 +4,26 @@ import ( "github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/cloudinit" ) -func GetMasterTemplate() cloudinit.Config { +func GetMasterTemplate(k8sVersion string, arch string, isRegisterNode bool) cloudinit.Config { result := baseTemplate result.WriteFiles = append(result.WriteFiles, masterTemplate.WriteFiles...) result.RunCmds = append(result.RunCmds, masterTemplate.RunCmds...) + result.Vars = map[string]any{ + "KubernetesVersion": k8sVersion, + "Arch": arch, + "RegisterNode": isRegisterNode, + } return result } -func GetWorkerTemplate() cloudinit.Config { - return baseTemplate +func GetWorkerTemplate(k8sVersion string, arch string) cloudinit.Config { + result := baseTemplate + result.Vars = map[string]any{ + "KubernetesVersion": k8sVersion, + "Arch": arch, + } + + return result } diff --git a/pkg/provisioner/cluster.go b/pkg/provisioner/cluster.go index e47129b..3ab6fca 100644 --- a/pkg/provisioner/cluster.go +++ b/pkg/provisioner/cluster.go @@ -18,16 +18,27 @@ type ClusterConfig struct { } type InstanceConfig struct { - Name string - CPUs string - Memory string - Disk string - K8sVersion string - Image string + Name string + CPUs string + Memory string + Disk string + K8sVersion string + Image string +} + +type MasterConfig struct { + InstanceConfig + + IsRegisterNode bool +} + +type WorkerConfig struct { + InstanceConfig + IsJoinCluster bool } -func CreateCluster(clusterName string, clusterConfig ClusterConfig, masterConfig InstanceConfig, workerConfig InstanceConfig) error { +func CreateCluster(clusterName string, clusterConfig ClusterConfig, masterConfig MasterConfig, workerConfig WorkerConfig) error { slog.Debug("create cluster", slog.String("clusterName", clusterName), slog.Any("clusterConfig", clusterConfig), slog.Any("masterConfig", masterConfig), @@ -47,11 +58,11 @@ func CreateCluster(clusterName string, clusterConfig ClusterConfig, masterConfig return GenerateKubeconfig(clusterName + "-master") } -func CreateMaster(clusterName string, config InstanceConfig) error { +func CreateMaster(clusterName string, config MasterConfig) error { slog.Debug("create master", slog.String("clusterName", clusterName), slog.Any("config", config)) config.Name = "master" - _, err := LaunchInstance(clusterName, config, GetMasterTemplate()) + _, err := LaunchInstance(clusterName, config.InstanceConfig, GetMasterTemplate(config.K8sVersion, "amd64", config.IsRegisterNode)) if err != nil { return fmt.Errorf("failed to launch instance: %w", err) } @@ -59,10 +70,10 @@ func CreateMaster(clusterName string, config InstanceConfig) error { return nil } -func CreateWorker(clusterName string, config InstanceConfig) error { +func CreateWorker(clusterName string, config WorkerConfig) error { slog.Debug("create worker", slog.String("clusterName", clusterName), slog.Any("config", config)) - instanceName, err := LaunchInstance(clusterName, config, GetWorkerTemplate()) + instanceName, err := LaunchInstance(clusterName, config.InstanceConfig, GetWorkerTemplate(config.K8sVersion, "amd64")) if err != nil { return fmt.Errorf("failed to launch instance: %w", err) } diff --git a/pkg/provisioner/instance.go b/pkg/provisioner/instance.go index d1fff11..1ff4028 100644 --- a/pkg/provisioner/instance.go +++ b/pkg/provisioner/instance.go @@ -26,10 +26,7 @@ func LaunchInstance(clusterName string, config InstanceConfig, cloudinitConfig c return "", fmt.Errorf("instance already exists: %s", instanceName) } - template, err := cloudinitConfig.Generate(map[string]string{ - "KubernetesVersion": config.K8sVersion, - "Arch": "amd64", - }) + template, err := cloudinitConfig.Generate() if err != nil { return "", fmt.Errorf("failed to generate cloud-init template: %w", err) } diff --git a/pkg/provisioner/template.go b/pkg/provisioner/template.go index 9204c65..7225546 100644 --- a/pkg/provisioner/template.go +++ b/pkg/provisioner/template.go @@ -104,6 +104,10 @@ nodeRegistration: - NumCPU - Mem --- +apiVersion: kubelet.config.k8s.io/v1beta1 +kind: KubeletConfiguration +registerNode: {{ .RegisterNode }} +--- apiVersion: kubeadm.k8s.io/v1beta3 kind: ClusterConfiguration kubernetesVersion: {{ .KubernetesVersion }}