Skip to content

Commit

Permalink
add ability to set docker labels on worker nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelnicolas committed Jan 14, 2020
1 parent dc4c293 commit 5922423
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func CreateCluster(c *cli.Context) error {
env = append(env, c.StringSlice("env")...)
env = append(env, fmt.Sprintf("K3S_CLUSTER_SECRET=%s", GenerateRandomString(20)))

/*
* --label, -l
* Docker container labels that will be added to the k3d node containers
*/
// labels
labels := []string{}
labels = append(labels, c.StringSlice("label")...)

/*
* Arguments passed on to the k3s server and agent, will be filled later
*/
Expand Down Expand Up @@ -204,6 +212,7 @@ func CreateCluster(c *cli.Context) error {
AutoRestart: c.Bool("auto-restart"),
ClusterName: c.String("name"),
Env: env,
Labels: labels,
Image: image,
NodeToPortSpecMap: portmap,
PortAutoOffset: c.Int("port-auto-offset"),
Expand Down Expand Up @@ -481,6 +490,7 @@ func AddNode(c *cli.Context) error {
AutoRestart: false,
ClusterName: clusterName,
Env: nil,
Labels: nil,
Image: "",
NodeToPortSpecMap: nil,
PortAutoOffset: 0,
Expand Down Expand Up @@ -559,6 +569,13 @@ func AddNode(c *cli.Context) error {
return nil
}

/* (0.6)
* --label, -l <key1=val1>
* Docker container labels that will be added to the k3d node containers
*/
clusterSpec.Labels = []string{}
clusterSpec.Labels = append(clusterSpec.Labels, c.StringSlice("label")...)

/*
* (1) Check cluster
*/
Expand Down
10 changes: 10 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func createWorker(spec *ClusterSpec, postfix int) (string, error) {
containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05")
containerLabels["cluster"] = spec.ClusterName

for _, label := range spec.Labels {
labelSlice := strings.SplitN(label, "=", 2)

if len(labelSlice) > 1 {
containerLabels[labelSlice[0]] = labelSlice[1]
} else {
containerLabels[labelSlice[0]] = ""
}
}

containerName := GetContainerName("worker", spec.ClusterName, postfix)
env := spec.Env

Expand Down
1 change: 1 addition & 0 deletions cli/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ClusterSpec struct {
AutoRestart bool
ClusterName string
Env []string
Labels []string
Image string
NodeToPortSpecMap map[string][]string
PortAutoOffset int
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func main() {
Name: "env, e",
Usage: "Pass an additional environment variable (new flag per variable)",
},
cli.StringSliceFlag{
Name: "label, l",
Usage: "Add one or more docker labels to every node container of the cluster, using Docker notation `key=value` (new flag per label)",
},
cli.IntFlag{
Name: "workers, w",
Value: 0,
Expand Down Expand Up @@ -154,6 +158,10 @@ func main() {
Name: "env, e",
Usage: "Pass an additional environment variable (new flag per variable)",
},
cli.StringSliceFlag{
Name: "label, l",
Usage: "Add one or more docker labels to every node container of the cluster (Docker notation: `key=value`)",
},
cli.StringSliceFlag{
Name: "volume, v",
Usage: "Mount one or more volumes into every created node (Docker notation: `source:destination`)",
Expand Down

0 comments on commit 5922423

Please sign in to comment.