From 7005f8182a2b907bf4d0cd1471221067398854e8 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Sun, 29 Mar 2020 19:39:41 +0200 Subject: [PATCH] validate clustername on create and use global runtime variable --- cmd/create/createCluster.go | 42 ++++++++++++++++++------------------- cmd/create/createNode.go | 18 ++++------------ cmd/root.go | 12 ++++++++++- pkg/runtimes/runtime.go | 3 +++ 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 0fb9df9b..30f1b283 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -29,6 +29,7 @@ import ( "github.com/spf13/cobra" cliutil "github.com/rancher/k3d/cmd/util" + "github.com/rancher/k3d/pkg/cluster" k3dCluster "github.com/rancher/k3d/pkg/cluster" "github.com/rancher/k3d/pkg/runtimes" k3d "github.com/rancher/k3d/pkg/types" @@ -49,11 +50,11 @@ func NewCmdCreateCluster() *cobra.Command { Long: `Create a new k3s cluster with containerized nodes (k3s in docker).`, 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 := parseCreateClusterCmd(cmd, args, createClusterOpts) - if err := k3dCluster.CreateCluster(cluster, runtime); err != nil { + cluster := parseCreateClusterCmd(cmd, args, createClusterOpts) + if err := k3dCluster.CreateCluster(cluster, runtimes.SelectedRuntime); err != nil { log.Errorln(err) log.Errorln("Failed to create cluster >>> Rolling Back") - if err := k3dCluster.DeleteCluster(cluster, runtime); err != nil { + if err := k3dCluster.DeleteCluster(cluster, runtimes.SelectedRuntime); err != nil { log.Errorln(err) log.Fatalln("Cluster creation FAILED, also FAILED to rollback changes!") } @@ -98,7 +99,7 @@ func NewCmdCreateCluster() *cobra.Command { cmd.Flags().String("datastore-keyfile", "", "Specify external datastore's TLS key file'") */ - /* k3s */ // TODO: to implement extra args + /* k3s */ cmd.Flags().StringArrayVar(&createClusterOpts.K3sServerArgs, "k3s-server-arg", nil, "Additional args passed to the `k3s server` command on master nodes (new flag per arg)") cmd.Flags().StringArrayVar(&createClusterOpts.K3sAgentArgs, "k3s-agent-arg", nil, "Additional args passed to the `k3s agent` command on worker nodes (new flag per arg)") @@ -109,17 +110,21 @@ func NewCmdCreateCluster() *cobra.Command { } // parseCreateClusterCmd parses the command input into variables required to create a cluster -func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts *k3d.CreateClusterOpts) (runtimes.Runtime, *k3d.Cluster) { - // --runtime - rt, err := cmd.Flags().GetString("runtime") - if err != nil { - log.Fatalln("No runtime specified") - } - runtime, err := runtimes.GetRuntime(rt) - if err != nil { - log.Fatalln(err) +func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts *k3d.CreateClusterOpts) *k3d.Cluster { + + /******************************** + * Parse and validate arguments * + ********************************/ + + clustername := args[0] + if err := cluster.CheckName(clustername); err != nil { + log.Fatal(err) } + /**************************** + * Parse and validate flags * + ****************************/ + // --image image, err := cmd.Flags().GetString("image") if err != nil { @@ -136,11 +141,6 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts log.Fatalln(err) } - // TODO: allow more than one master - if masterCount > 1 { - log.Warnln("Multi-Master is setup not fully implemented/supported right now!") - } - // --workers workerCount, err := cmd.Flags().GetInt("workers") if err != nil { @@ -311,7 +311,7 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts ********************/ cluster := &k3d.Cluster{ - Name: args[0], // TODO: validate name0 + Name: clustername, Network: network, Secret: secret, CreateClusterOpts: createClusterOpts, @@ -332,7 +332,7 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts MasterOpts: k3d.MasterOpts{}, } - // TODO: by default, we don't expose an PI port, even if we only have a single master: should we change that? + // TODO: by default, we don't expose an API port, even if we only have a single master: should we change that? // -> if we want to change that, simply add the exposeAPI struct here // first master node will be init node if we have more than one master specified but no external datastore @@ -411,5 +411,5 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts } } - return runtime, cluster + return cluster } diff --git a/cmd/create/createNode.go b/cmd/create/createNode.go index 82661dc0..27837574 100644 --- a/cmd/create/createNode.go +++ b/cmd/create/createNode.go @@ -43,9 +43,9 @@ func NewCmdCreateNode() *cobra.Command { Long: `Create a new containerized k3s node (k3s in docker).`, 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) { - nodes, cluster, runtime := parseCreateNodeCmd(cmd, args) + nodes, cluster := parseCreateNodeCmd(cmd, args) for _, node := range nodes { - if err := k3dc.AddNodeToCluster(runtime, node, cluster); err != nil { + if err := k3dc.AddNodeToCluster(runtimes.SelectedRuntime, node, cluster); err != nil { log.Errorf("Failed to add node '%s' to cluster '%s'", node.Name, cluster.Name) log.Errorln(err) } @@ -68,17 +68,7 @@ func NewCmdCreateNode() *cobra.Command { } // parseCreateNodeCmd parses the command input into variables required to create a cluster -func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cluster, runtimes.Runtime) { - - // --runtime - rt, err := cmd.Flags().GetString("runtime") - if err != nil { - log.Fatalln("No runtime specified") - } - runtime, err := runtimes.GetRuntime(rt) - if err != nil { - log.Fatalln(err) - } +func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cluster) { // --replicas replicas, err := cmd.Flags().GetInt("replicas") @@ -125,5 +115,5 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl nodes = append(nodes, node) } - return nodes, cluster, runtime + return nodes, cluster } diff --git a/cmd/root.go b/cmd/root.go index 94e8ace9..85cdd452 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,6 +35,7 @@ import ( "github.com/rancher/k3d/cmd/load" "github.com/rancher/k3d/cmd/start" "github.com/rancher/k3d/cmd/stop" + "github.com/rancher/k3d/pkg/runtimes" "github.com/rancher/k3d/version" @@ -70,7 +71,7 @@ func Execute() { } func init() { - cobra.OnInitialize(initLogging) + cobra.OnInitialize(initLogging, initRuntime) // add persistent flags (present to all subcommands) // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k3d/config.yaml)") @@ -114,6 +115,15 @@ func initLogging() { } } +func initRuntime() { + runtime, err := runtimes.GetRuntime(flags.runtime) + if err != nil { + log.Fatalln(err) + } + runtimes.SelectedRuntime = runtime + log.Debugf("Selected runtime is '%T'", runtimes.SelectedRuntime) +} + // Completion var completionFunctions = map[string]func(io.Writer) error{ "bash": rootCmd.GenBashCompletion, diff --git a/pkg/runtimes/runtime.go b/pkg/runtimes/runtime.go index 0b3c33a9..93997583 100644 --- a/pkg/runtimes/runtime.go +++ b/pkg/runtimes/runtime.go @@ -30,6 +30,9 @@ import ( k3d "github.com/rancher/k3d/pkg/types" ) +// SelectedRuntime is a runtime (pun intended) variable determining the selected runtime +var SelectedRuntime Runtime = docker.Docker{} + // Runtimes defines a map of implemented k3d runtimes var Runtimes = map[string]Runtime{ "docker": docker.Docker{},