generate container names in a func and use it to determine if node-specifier is valid
This commit is contained in:
parent
f77d67aaf0
commit
a68d9e7042
@ -23,7 +23,10 @@ import (
|
|||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultRegistry = "docker.io"
|
const (
|
||||||
|
defaultRegistry = "docker.io"
|
||||||
|
defaultServerCount = 1
|
||||||
|
)
|
||||||
|
|
||||||
// CheckTools checks if the docker API server is responding
|
// CheckTools checks if the docker API server is responding
|
||||||
func CheckTools(c *cli.Context) error {
|
func CheckTools(c *cli.Context) error {
|
||||||
@ -100,7 +103,7 @@ func CreateCluster(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// new port map
|
// new port map
|
||||||
portmap, err := mapNodesToPortSpecs(c.StringSlice("publish"))
|
portmap, err := mapNodesToPortSpecs(c.StringSlice("publish"), GetAllContainerNames(c.String("name"), defaultServerCount, c.Int("workers")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,10 @@ import (
|
|||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultContainerNamePrefix = "k3d"
|
||||||
|
)
|
||||||
|
|
||||||
type cluster struct {
|
type cluster struct {
|
||||||
name string
|
name string
|
||||||
image string
|
image string
|
||||||
@ -25,6 +29,26 @@ type cluster struct {
|
|||||||
workers []types.Container
|
workers []types.Container
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetContainerName generates the container names
|
||||||
|
func GetContainerName(role, clusterName string, postfix int) string {
|
||||||
|
if postfix >= 0 {
|
||||||
|
return fmt.Sprintf("%s-%s-%s-%d", defaultContainerNamePrefix, clusterName, role, postfix)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s-%s-%s", defaultContainerNamePrefix, clusterName, role)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllContainerNames returns a list of all containernames that will be created
|
||||||
|
func GetAllContainerNames(clusterName string, serverCount, workerCount int) []string {
|
||||||
|
names := []string{}
|
||||||
|
for postfix := 0; postfix < serverCount; postfix++ {
|
||||||
|
names = append(names, GetContainerName("server", clusterName, postfix))
|
||||||
|
}
|
||||||
|
for postfix := 0; postfix < workerCount; postfix++ {
|
||||||
|
names = append(names, GetContainerName("worker", clusterName, postfix))
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
// createDirIfNotExists checks for the existence of a directory and creates it along with all required parents if not.
|
// createDirIfNotExists checks for the existence of a directory and creates it along with all required parents if not.
|
||||||
// It returns an error if the directory (or parents) couldn't be created and nil if it worked fine or if the path already exists.
|
// It returns an error if the directory (or parents) couldn't be created and nil if it worked fine or if the path already exists.
|
||||||
func createDirIfNotExists(path string) error {
|
func createDirIfNotExists(path string) error {
|
||||||
|
|||||||
@ -72,7 +72,7 @@ func createServer(verbose bool, image string, apiPort string, args []string, env
|
|||||||
containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05")
|
containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05")
|
||||||
containerLabels["cluster"] = name
|
containerLabels["cluster"] = name
|
||||||
|
|
||||||
containerName := fmt.Sprintf("k3d-%s-server", name)
|
containerName := GetContainerName("server", name, -1)
|
||||||
|
|
||||||
// ports to be assigned to the server belong to roles
|
// ports to be assigned to the server belong to roles
|
||||||
// all, server or <server-container-name>
|
// all, server or <server-container-name>
|
||||||
@ -132,7 +132,7 @@ func createWorker(verbose bool, image string, args []string, env []string, name
|
|||||||
containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05")
|
containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05")
|
||||||
containerLabels["cluster"] = name
|
containerLabels["cluster"] = name
|
||||||
|
|
||||||
containerName := fmt.Sprintf("k3d-%s-worker-%d", name, postfix)
|
containerName := GetContainerName("worker", name, postfix)
|
||||||
|
|
||||||
env = append(env, fmt.Sprintf("K3S_URL=https://k3d-%s-server:%s", name, serverPort))
|
env = append(env, fmt.Sprintf("K3S_URL=https://k3d-%s-server:%s", name, serverPort))
|
||||||
|
|
||||||
|
|||||||
20
cli/port.go
20
cli/port.go
@ -2,6 +2,7 @@ package run
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -24,26 +25,41 @@ var nodeRuleGroupsMap = map[string][]string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mapNodesToPortSpecs maps nodes to portSpecs
|
// mapNodesToPortSpecs maps nodes to portSpecs
|
||||||
func mapNodesToPortSpecs(specs []string) (map[string][]string, error) {
|
func mapNodesToPortSpecs(specs []string, createdNodes []string) (map[string][]string, error) {
|
||||||
|
|
||||||
if err := validatePortSpecs(specs); err != nil {
|
if err := validatePortSpecs(specs); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check node-specifier possibilitites
|
||||||
|
possibleNodeSpecifiers := []string{"all", "workers", "server", "master"}
|
||||||
|
possibleNodeSpecifiers = append(possibleNodeSpecifiers, createdNodes...)
|
||||||
|
|
||||||
nodeToPortSpecMap := make(map[string][]string)
|
nodeToPortSpecMap := make(map[string][]string)
|
||||||
|
|
||||||
for _, spec := range specs {
|
for _, spec := range specs {
|
||||||
nodes, portSpec := extractNodes(spec)
|
nodes, portSpec := extractNodes(spec)
|
||||||
|
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
|
// check if node-specifier is valid (either a role or a name) and append to list if matches
|
||||||
|
nodeFound := false
|
||||||
|
for _, name := range possibleNodeSpecifiers {
|
||||||
|
if node == name {
|
||||||
|
nodeFound = true
|
||||||
nodeToPortSpecMap[node] = append(nodeToPortSpecMap[node], portSpec)
|
nodeToPortSpecMap[node] = append(nodeToPortSpecMap[node], portSpec)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !nodeFound {
|
||||||
|
log.Printf("WARNING: Unknown node-specifier [%s] in port mapping entry [%s]", node, spec)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodeToPortSpecMap, nil
|
return nodeToPortSpecMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The factory function for PublishedPorts
|
// CreatePublishedPorts is the factory function for PublishedPorts
|
||||||
func CreatePublishedPorts(specs []string) (*PublishedPorts, error) {
|
func CreatePublishedPorts(specs []string) (*PublishedPorts, error) {
|
||||||
if len(specs) == 0 {
|
if len(specs) == 0 {
|
||||||
var newExposedPorts = make(map[nat.Port]struct{}, 1)
|
var newExposedPorts = make(map[nat.Port]struct{}, 1)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user