Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure the machineset label value is valid per K8s convention #69

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions harvester/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ func (d *Driver) Create() error {
//with this unique machine set. This can then be used for populating affinity rules
machineSetSplit := strings.Split(d.MachineName, "-")
machineSetSplit = append([]string{d.VMNamespace}, machineSetSplit...)
machineSetName := strings.Join(machineSetSplit[:len(machineSetSplit)-2], "-")
machineSetName, err := formatLabelValue(strings.Join(machineSetSplit[:len(machineSetSplit)-2], "-"))
if err != nil {
return err
}
vmBuilder = vmBuilder.Labels(map[string]string{poolNameLabelKey: machineSetName})
addtionalPodAffinityTerm := corev1.PodAffinityTerm{
additionalPodAffinityTerm := corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
poolNameLabelKey: machineSetName,
Expand All @@ -65,7 +68,7 @@ func (d *Driver) Create() error {
}
additionalWeightPodAffinity := corev1.WeightedPodAffinityTerm{
Weight: 1,
PodAffinityTerm: addtionalPodAffinityTerm,
PodAffinityTerm: additionalPodAffinityTerm,
}
if affinity.PodAffinity != nil {
affinity.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(affinity.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution, additionalWeightPodAffinity)
Expand Down
32 changes: 32 additions & 0 deletions harvester/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package harvester

import (
"encoding/base64"
"fmt"
"hash/fnv"

"k8s.io/apimachinery/pkg/util/validation"
)

// formatLabelValue returns v if it meets the standards for a Kubernetes
// label value. Otherwise, it returns a hash which meets the requirements.
// ref: https://github.com/kubernetes-sigs/cluster-api/blob/010af7f92f98ead971742d347d258a8786d5d57c/util/labels/format/helpers.go
func formatLabelValue(v string) (string, error) {
// a valid Kubernetes label value must:
// - be less than 64 characters long.
// - be an empty string OR consist of alphanumeric characters, '-', '_' or '.'.
// - start and end with an alphanumeric character
if len(validation.IsValidLabelValue(v)) == 0 {
return v, nil
}

hasher := fnv.New32a()
if _, err := hasher.Write([]byte(v)); err != nil {
return "", err
}

// use base64 URL encoding to avoid special characters like '/' in the generated
// hash
return fmt.Sprintf("hash_%s_z",
base64.RawURLEncoding.EncodeToString(hasher.Sum(nil))), nil
}
38 changes: 38 additions & 0 deletions harvester/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package harvester

import "testing"

func TestFormatLabelValue(t *testing.T) {
testCases := []struct {
desc string
labelValue string
expected string
}{
{
desc: "return empty string if label value is empty",
labelValue: "",
expected: "",
},
{
desc: "return label value unchanged if it's less than 63 characters",
labelValue: "machineSetName",
expected: "machineSetName",
},
{
desc: "return hashed label value if more than 63 characters",
labelValue: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName",
expected: "hash_FR_ghQ_z",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(*testing.T) {
actual, err := formatLabelValue(tc.labelValue)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tc.expected != actual {
t.Errorf("test case failed: %s. expected %s, got %s", tc.desc, tc.expected, actual)
}
})
}
}
Loading