Skip to content

Commit

Permalink
add RegisterNode
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 16, 2024
1 parent 9d92066 commit 07ee0af
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 46 deletions.
6 changes: 4 additions & 2 deletions pkg/cloudinit/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/cloudinit/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 30 additions & 23 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
Expand All @@ -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,
}
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/provisioner/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 22 additions & 11 deletions pkg/provisioner/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -47,22 +58,22 @@ 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)
}

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)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/provisioner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit 07ee0af

Please sign in to comment.