add command parsing to crete node

pull/227/head
iwilltry42 5 years ago
parent a88140dc48
commit 0278388f34
  1. 9
      cmd/create/createCluster.go
  2. 30
      cmd/create/createNode.go

@ -39,9 +39,9 @@ func NewCmdCreateCluster() *cobra.Command {
Use: "cluster",
Short: "Create a new k3s cluster in docker",
Long: `Create a new k3s cluster with containerized nodes (k3s in docker).`,
Args: cobra.ExactArgs(1), // exactly one cluster name can be set
Args: cobra.ExactArgs(1), // exactly one cluster name can be set // TODO: if not specified, use k3d.DefaultClusterName
Run: func(cmd *cobra.Command, args []string) {
runtime, cluster := parseCmd(cmd, args)
runtime, cluster := parseCreateClusterCmd(cmd, args)
if err := k3dCluster.CreateCluster(cluster, runtime); err != nil {
log.Fatalln(err)
}
@ -49,7 +49,6 @@ func NewCmdCreateCluster() *cobra.Command {
}
// add flags
cmd.Flags().StringP("name", "n", k3d.DefaultClusterName, "Set a name for the cluster")
cmd.Flags().StringP("api-port", "a", "6443", "Specify the Kubernetes cluster API server port (Format: `--api-port [host:]port`")
cmd.Flags().IntP("workers", "w", 0, "Specify how many workers you want to create")
@ -59,8 +58,8 @@ func NewCmdCreateCluster() *cobra.Command {
return cmd
}
// parseCmd parses the command input into variables required to create a cluster
func parseCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k3d.Cluster) {
// parseCreateClusterCmd parses the command input into variables required to create a cluster
func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k3d.Cluster) {
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Fatalln("Runtime not defined")

@ -38,18 +38,10 @@ func NewCmdCreateNode() *cobra.Command {
Use: "node",
Short: "Create a new k3s node in docker",
Long: `Create a new containerized k3s node (k3s in docker).`,
Args: cobra.ExactArgs(1), // exactly one name accepted
Args: cobra.ExactArgs(1), // exactly one name accepted // TODO: if not specified, inherit from cluster that the node shall belong to, if that is specified
Run: func(cmd *cobra.Command, args []string) {
log.Debugln("create node called")
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Debugln("runtime not defined")
}
runtime, err := runtimes.GetRuntime(rt)
if err != nil {
log.Fatalf("Unsupported runtime '%s'", rt)
}
if err := cluster.CreateNode(&k3d.Node{Name: args[0]}, runtime); err != nil {
runtime, node := parseCreateNodeCmd(cmd, args)
if err := cluster.CreateNode(node, runtime); err != nil {
log.Fatalln(err)
}
},
@ -57,7 +49,23 @@ func NewCmdCreateNode() *cobra.Command {
// add flags
cmd.Flags().Int("replicas", 1, "Number of replicas of this node specification.")
cmd.Flags().StringP("cluster", "c", "", "Select the cluster that the node shall connect to.")
// done
return cmd
}
// parseCreateNodeCmd parses the command input into variables required to create a cluster
func parseCreateNodeCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k3d.Node) {
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Fatalln("Runtime not defined")
}
runtime, err := runtimes.GetRuntime(rt)
if err != nil {
log.Fatalln(err)
}
node := k3d.Node{Name: args[0]}
return runtime, &node
}

Loading…
Cancel
Save