create multiple nodes at once

This commit is contained in:
iwilltry42 2019-09-30 09:28:00 +02:00
parent 0278388f34
commit cd4223e4ed
4 changed files with 35 additions and 11 deletions

View File

@ -22,6 +22,8 @@ THE SOFTWARE.
package create
import (
"fmt"
"github.com/spf13/cobra"
"github.com/rancher/k3d/pkg/cluster"
@ -40,10 +42,8 @@ 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) {
runtime, node := parseCreateNodeCmd(cmd, args)
if err := cluster.CreateNode(node, runtime); err != nil {
log.Fatalln(err)
}
runtime, nodes := parseCreateNodeCmd(cmd, args)
cluster.CreateNodes(nodes, runtime)
},
}
@ -56,7 +56,7 @@ func NewCmdCreateNode() *cobra.Command {
}
// parseCreateNodeCmd parses the command input into variables required to create a cluster
func parseCreateNodeCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k3d.Node) {
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")
@ -65,7 +65,17 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k
if err != nil {
log.Fatalln(err)
}
node := k3d.Node{Name: args[0]}
return runtime, &node
replicas, err := cmd.Flags().GetInt("replicas")
if err != nil {
log.Errorln("Failed to parse flag '--replicas'")
log.Fatalln(err)
}
nodes := []*k3d.Node{}
for i := 0; i < replicas; i++ {
nodes = append(nodes, &k3d.Node{Name: fmt.Sprintf("%s-%d", args[0], i)})
}
return runtime, nodes
}

View File

@ -32,13 +32,17 @@ import (
// - a docker network
func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
if err := runtime.CreateNode(&k3d.Node{}); err != nil {
log.Debugln("...failed")
for _, node := range cluster.Nodes {
log.Infoln("Creating node", node.Name)
if err := runtime.CreateNode(&node); err != nil {
log.Errorln("...failed")
}
}
log.Debugln("...success")
return nil
}
// DeleteCluster deletes an existing cluster
func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
return nil
}

View File

@ -27,11 +27,11 @@ import (
"github.com/rancher/k3d/pkg/types"
)
// CheckClusterName ensures that a cluster name is also a valid host name according to RFC 1123.
// CheckName ensures that a cluster name is also a valid host name according to RFC 1123.
// We further restrict the length of the cluster name to maximum 'clusterNameMaxSize'
// so that we can construct the host names based on the cluster name, and still stay
// within the 64 characters limit.
func CheckClusterName(name string) error {
func CheckName(name string) error {
if err := ValidateHostname(name); err != nil {
return fmt.Errorf("Invalid cluster name. %+v", ValidateHostname(name))
}

View File

@ -30,8 +30,18 @@ import (
log "github.com/sirupsen/logrus"
)
// CreateNodes creates a list of nodes
func CreateNodes(nodes []*k3d.Node, runtime k3drt.Runtime) { // TODO: pass `--atomic` flag, so we stop and return an error if any node creation fails?
for _, node := range nodes {
if err := CreateNode(node, runtime); err != nil {
log.Error(err)
}
}
}
// CreateNode creates a new containerized k3s node
func CreateNode(nodeSpec *k3d.Node, runtime k3drt.Runtime) error {
log.Debugf("Creating node from spec\n%+v", nodeSpec)
if err := runtime.CreateNode(nodeSpec); err != nil {
log.Error(err)
}