Skip to content

Commit

Permalink
add create cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 16, 2024
1 parent dc94f3b commit cce66d7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
39 changes: 33 additions & 6 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
)
},
}

Expand Down Expand Up @@ -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")
}

Expand Down
21 changes: 17 additions & 4 deletions pkg/provisioner/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

type ClusterConfig struct {
Name string
}

type InstanceConfig struct {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provisioner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down

0 comments on commit cce66d7

Please sign in to comment.