Skip to content

Commit

Permalink
add base for create cluster command
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 16, 2024
1 parent b199f50 commit c85c98b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
23 changes: 16 additions & 7 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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")
Expand All @@ -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)
}
16 changes: 13 additions & 3 deletions pkg/provisioner/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/provisioner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c85c98b

Please sign in to comment.