Skip to content

Commit

Permalink
Added: support for populating labels via kubeadm's kubeletExtraArgs c…
Browse files Browse the repository at this point in the history
…onfig now
  • Loading branch information
yashvardhan-kukreja committed Nov 18, 2020
1 parent 54fa55b commit 4b5c1bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 129 deletions.
34 changes: 32 additions & 2 deletions pkg/cluster/internal/create/actions/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,42 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
}
}

// Populate the list of control-plane node labels and the list of worker node labels respectively.
// controlPlaneLabels is an array of maps (labels, read from config) associated with all the control-plane nodes.
// workerLabels is an array of maps (labels, read from config) associated with all the worker nodes.
controlPlaneLabels := []map[string]string{}
workerLabels := []map[string]string{}
for _, node := range ctx.Config.Nodes {
if node.Role == config.ControlPlaneRole {
controlPlaneLabels = append(controlPlaneLabels, node.Labels)
} else if node.Role == config.WorkerRole {
workerLabels = append(workerLabels, node.Labels)
} else {
continue
}
}

// hashMapLabelsToCommaSeparatedLabels converts labels in hashmap form to labels in a comma-separated string form like "key1=value1,key2=value2"
hashMapLabelsToCommaSeparatedLabels := func(labels map[string]string) string {
output := ""
for key, value := range labels {
output += fmt.Sprintf("%s=%s,", key, value)
}
return strings.TrimSuffix(output, ",") // remove the last character (comma) in the output string
}

// create the kubeadm join configuration for control plane nodes
controlPlanes, err := nodeutils.ControlPlaneNodes(allNodes)
if err != nil {
return err
}

for _, node := range controlPlanes {
for i, node := range controlPlanes {
node := node // capture loop variable
configData := configData // copy config data
if len(controlPlaneLabels[i]) > 0 {
configData.NodeLabels = hashMapLabelsToCommaSeparatedLabels(controlPlaneLabels[i]) // updating the config with the respective labels to be written over the current control-plane node in consideration
}
fns = append(fns, kubeadmConfigPlusPatches(node, configData))
}

Expand All @@ -109,10 +136,13 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
}
if len(workers) > 0 {
// create the workers concurrently
for _, node := range workers {
for i, node := range workers {
node := node // capture loop variable
configData := configData // copy config data
configData.ControlPlane = false
if len(workerLabels[i]) > 0 {
configData.NodeLabels = hashMapLabelsToCommaSeparatedLabels(workerLabels[i]) // updating the config with the respective labels to be written over the current worker node in consideration
}
fns = append(fns, kubeadmConfigPlusPatches(node, configData))
}
}
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/cluster/internal/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/kubeadminit"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/kubeadmjoin"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/loadbalancer"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/postcreationconfiguration"
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions/waitforready"
"sigs.k8s.io/kind/pkg/cluster/internal/kubeconfig"
)
Expand Down Expand Up @@ -137,7 +136,6 @@ func Cluster(logger log.Logger, p providers.Provider, opts *ClusterOptions) erro
installstorage.NewAction(), // install StorageClass
kubeadmjoin.NewAction(), // run kubeadm join
waitforready.NewAction(opts.WaitForReady), // wait for cluster readiness
postcreationconfiguration.NewAction(), // perform post-cluster-creation configurations like custom labelling, tainting, etc.
)
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/internal/kubeadm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type ConfigData struct {
// IPv4 values take precedence over IPv6 by default, if true set IPv6 default values
IPv6 bool

// Labels are the labels, in the format "key1=val1,key2=val2", with which the respective node will be labeled
NodeLabels string

// DerivedConfigData is populated by Derive()
// These auto-generated fields are available to Config templates,
// but not meant to be set by hand
Expand Down Expand Up @@ -315,6 +318,7 @@ nodeRegistration:
fail-swap-on: "false"
node-ip: "{{ .NodeAddress }}"
provider-id: "kind://{{.NodeProvider}}/{{.ClusterName}}/{{.NodeName}}"
node-labels: "{{ .NodeLabels }}"
---
# no-op entry that exists solely so it can be patched
apiVersion: kubeadm.k8s.io/v1beta2
Expand All @@ -333,6 +337,7 @@ nodeRegistration:
fail-swap-on: "false"
node-ip: "{{ .NodeAddress }}"
provider-id: "kind://{{.NodeProvider}}/{{.ClusterName}}/{{.NodeName}}"
node-labels: "{{ .NodeLabels }}"
discovery:
bootstrapToken:
apiServerEndpoint: "{{ .ControlPlaneEndpoint }}"
Expand Down

0 comments on commit 4b5c1bb

Please sign in to comment.