Skip to content

Commit

Permalink
Merge pull request #1926 from yashvardhan-kukreja/issue-1574/support-…
Browse files Browse the repository at this point in the history
…for-node-labels-to-config

added: support for adding labels from the config file itself
  • Loading branch information
BenTheElder authored Mar 15, 2021
2 parents 5b79090 + 4b5c1bb commit efcd23b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/config/v1alpha4/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type Node struct {
// If unset a default image will be used, see defaults.Image
Image string `yaml:"image,omitempty"`

// Labels are the labels with which the respective node will be labeled
Labels map[string]string `yaml:"labels,omitempty"`

/* Advanced fields */

// TODO: cri-like types should be inline instead
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/config/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -98,15 +98,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 @@ -117,10 +144,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
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 @@ -326,6 +329,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 @@ -344,6 +348,7 @@ nodeRegistration:
fail-swap-on: "false"
node-ip: "{{ .NodeAddress }}"
provider-id: "kind://{{.NodeProvider}}/{{.ClusterName}}/{{.NodeName}}"
node-labels: "{{ .NodeLabels }}"
discovery:
bootstrapToken:
apiServerEndpoint: "{{ .ControlPlaneEndpoint }}"
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/apis/config/convert_v1alpha4.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func convertv1alpha4Node(in *v1alpha4.Node, out *Node) {
out.Role = NodeRole(in.Role)
out.Image = in.Image

out.Labels = in.Labels
out.KubeadmConfigPatches = in.KubeadmConfigPatches
out.ExtraMounts = make([]Mount, len(in.ExtraMounts))
out.ExtraPortMappings = make([]PortMapping, len(in.ExtraPortMappings))
Expand Down
3 changes: 3 additions & 0 deletions pkg/internal/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type Node struct {
// If unset a default image will be used, see defaults.Image
Image string

// Labels are the labels with which the respective node will be labeled
Labels map[string]string

/* Advanced fields */

// ExtraMounts describes additional mount points for the node container
Expand Down
7 changes: 7 additions & 0 deletions pkg/internal/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit efcd23b

Please sign in to comment.