create multiple nodes at once
This commit is contained in:
parent
0278388f34
commit
cd4223e4ed
@ -22,6 +22,8 @@ THE SOFTWARE.
|
|||||||
package create
|
package create
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/rancher/k3d/pkg/cluster"
|
"github.com/rancher/k3d/pkg/cluster"
|
||||||
@ -40,10 +42,8 @@ func NewCmdCreateNode() *cobra.Command {
|
|||||||
Long: `Create a new containerized k3s node (k3s in docker).`,
|
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
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
runtime, node := parseCreateNodeCmd(cmd, args)
|
runtime, nodes := parseCreateNodeCmd(cmd, args)
|
||||||
if err := cluster.CreateNode(node, runtime); err != nil {
|
cluster.CreateNodes(nodes, runtime)
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ func NewCmdCreateNode() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseCreateNodeCmd parses the command input into variables required to create a cluster
|
// 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")
|
rt, err := cmd.Flags().GetString("runtime")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Runtime not defined")
|
log.Fatalln("Runtime not defined")
|
||||||
@ -65,7 +65,17 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
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
|
||||||
}
|
}
|
||||||
|
@ -32,13 +32,17 @@ import (
|
|||||||
// - a docker network
|
// - a docker network
|
||||||
func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
|
func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
|
||||||
|
|
||||||
if err := runtime.CreateNode(&k3d.Node{}); err != nil {
|
for _, node := range cluster.Nodes {
|
||||||
log.Debugln("...failed")
|
log.Infoln("Creating node", node.Name)
|
||||||
|
if err := runtime.CreateNode(&node); err != nil {
|
||||||
|
log.Errorln("...failed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.Debugln("...success")
|
log.Debugln("...success")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCluster deletes an existing cluster
|
||||||
func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
|
func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@ import (
|
|||||||
"github.com/rancher/k3d/pkg/types"
|
"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'
|
// 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
|
// so that we can construct the host names based on the cluster name, and still stay
|
||||||
// within the 64 characters limit.
|
// within the 64 characters limit.
|
||||||
func CheckClusterName(name string) error {
|
func CheckName(name string) error {
|
||||||
if err := ValidateHostname(name); err != nil {
|
if err := ValidateHostname(name); err != nil {
|
||||||
return fmt.Errorf("Invalid cluster name. %+v", ValidateHostname(name))
|
return fmt.Errorf("Invalid cluster name. %+v", ValidateHostname(name))
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,18 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
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
|
// CreateNode creates a new containerized k3s node
|
||||||
func CreateNode(nodeSpec *k3d.Node, runtime k3drt.Runtime) error {
|
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 {
|
if err := runtime.CreateNode(nodeSpec); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user