Skip to content

Commit e20440c

Browse files
author
Itxaka
authored
Propagate inventory labels to node on bootstrap plan (#243)
Inventory labels are not propagated from the inventory into the node so they are pretty useless for things like upgrades. This patch fixes it by using the override yaml in the node k3s/rke2 configuration to append node labels obtained from the inventory Signed-off-by: Itxaka <igarcia@suse.com> Signed-off-by: Itxaka <igarcia@suse.com>
1 parent 221af67 commit e20440c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pkg/controllers/machineinventoryselector/bootstrap.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"bytes"
2121
"encoding/base64"
2222
"encoding/json"
23+
"fmt"
2324

2425
"github.com/pkg/errors"
2526
"github.com/rancher/elemental-operator/pkg/apis/elemental.cattle.io/v1beta1"
2627
"github.com/rancher/elemental-operator/pkg/controllers/machineinventory"
2728
"github.com/rancher/system-agent/pkg/applyinator"
2829
"github.com/rancher/wrangler/pkg/generic"
2930
"github.com/sirupsen/logrus"
31+
"sigs.k8s.io/yaml"
3032
)
3133

3234
// bootstrapReadyHandler once the `InventoryReady` condition is true the bootstrap will set the bootstrap plan and record the checksum. Once the bootstrap checksum is applied to the machine inventory bootstrap is considered complete.
@@ -145,6 +147,21 @@ func (h *handler) getBootstrapPlan(selector *v1beta1.MachineInventorySelector, i
145147
return "", nil, err
146148
}
147149

150+
type LabelsFromInventory struct {
151+
NodeLabels []string `yaml:"node-label+"`
152+
}
153+
154+
nodeLabelsFromInventory := LabelsFromInventory{NodeLabels: []string{}}
155+
for label, value := range inventory.Labels {
156+
nodeLabelsFromInventory.NodeLabels = append(nodeLabelsFromInventory.NodeLabels, fmt.Sprintf("%s=%s", label, value))
157+
}
158+
159+
nodeLabelsFromInventoryInYaml, err := yaml.Marshal(nodeLabelsFromInventory)
160+
if err != nil {
161+
logrus.Warnf("Could not decode the inventory labels to add them to the node: %s", err)
162+
nodeLabelsFromInventoryInYaml = []byte("")
163+
}
164+
148165
p := applyinator.Plan{
149166
Files: []applyinator.File{
150167
{
@@ -157,11 +174,21 @@ func (h *handler) getBootstrapPlan(selector *v1beta1.MachineInventorySelector, i
157174
Path: "/etc/rancher/rke2/config.yaml.d/99-elemental-name.yaml",
158175
Permissions: "0600",
159176
},
177+
{
178+
Content: base64.StdEncoding.EncodeToString(nodeLabelsFromInventoryInYaml),
179+
Path: "/etc/rancher/rke2/config.yaml.d/99-elemental-inventory-labels.yaml",
180+
Permissions: "0600",
181+
},
160182
{
161183
Content: base64.StdEncoding.EncodeToString([]byte("node-name: " + inventory.Name)),
162184
Path: "/etc/rancher/k3s/config.yaml.d/99-elemental-name.yaml",
163185
Permissions: "0600",
164186
},
187+
{
188+
Content: base64.StdEncoding.EncodeToString(nodeLabelsFromInventoryInYaml),
189+
Path: "/etc/rancher/k3s/config.yaml.d/99-elemental-inventory-labels.yaml",
190+
Permissions: "0600",
191+
},
165192
{
166193
Content: base64.StdEncoding.EncodeToString([]byte(inventory.Name)),
167194
Path: "/usr/local/etc/hostname",

0 commit comments

Comments
 (0)