Skip to content

Commit 441e8a5

Browse files
authored
Merge pull request #9298 from chrischdi/pr-shorter-machineset-names
🐛 MD controller: use regular random suffix for MachineSets, ensure max length 63
2 parents abab528 + af137e2 commit 441e8a5

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

internal/controllers/machinedeployment/machinedeployment_sync.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ func (r *Reconciler) computeDesiredMachineSet(deployment *clusterv1.MachineDeplo
240240
// Append a random string at the end of template hash. This is required to distinguish MachineSets that
241241
// could be created with the same spec as a result of rolloutAfter. If not, computeDesiredMachineSet
242242
// will end up updating the existing MachineSet instead of creating a new one.
243-
uniqueIdentifierLabelValue = fmt.Sprintf("%d-%s", templateHash, apirand.String(5))
244-
245-
name = computeNewMachineSetName(deployment.Name+"-", apirand.SafeEncodeString(uniqueIdentifierLabelValue))
243+
var randomSuffix string
244+
name, randomSuffix = computeNewMachineSetName(deployment.Name + "-")
245+
uniqueIdentifierLabelValue = fmt.Sprintf("%d-%s", templateHash, randomSuffix)
246246

247247
// Add foregroundDeletion finalizer to MachineSet if the MachineDeployment has it.
248248
if sets.New[string](deployment.Finalizers...).Has(metav1.FinalizerDeleteDependents) {
@@ -366,11 +366,12 @@ const (
366366
// the upstream SimpleNameGenerator.
367367
// Note: We had to extract the logic as we want to use the MachineSet name suffix as
368368
// unique identifier for the MachineSet.
369-
func computeNewMachineSetName(base, suffix string) string {
369+
func computeNewMachineSetName(base string) (string, string) {
370370
if len(base) > maxGeneratedNameLength {
371371
base = base[:maxGeneratedNameLength]
372372
}
373-
return fmt.Sprintf("%s%s", base, suffix)
373+
r := apirand.String(randomLength)
374+
return fmt.Sprintf("%s%s", base, r), r
374375
}
375376

376377
// scale scales proportionally in order to mitigate risk. Otherwise, scaling up can increase the size

internal/controllers/machinedeployment/machinedeployment_sync_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package machinedeployment
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223
"testing"
2324
"time"
2425

@@ -799,3 +800,39 @@ func assertCondition(t *testing.T, from conditions.Getter, condition *clusterv1.
799800
}
800801
}
801802
}
803+
804+
func Test_computeNewMachineSetName(t *testing.T) {
805+
tests := []struct {
806+
base string
807+
wantPrefix string
808+
}{
809+
{
810+
"a",
811+
"a",
812+
},
813+
{
814+
fmt.Sprintf("%058d", 0),
815+
fmt.Sprintf("%058d", 0),
816+
},
817+
{
818+
fmt.Sprintf("%059d", 0),
819+
fmt.Sprintf("%058d", 0),
820+
},
821+
{
822+
fmt.Sprintf("%0100d", 0),
823+
fmt.Sprintf("%058d", 0),
824+
},
825+
}
826+
for _, tt := range tests {
827+
t.Run(fmt.Sprintf("base=%q, wantPrefix=%q", tt.base, tt.wantPrefix), func(t *testing.T) {
828+
got, gotSuffix := computeNewMachineSetName(tt.base)
829+
gotPrefix := strings.TrimSuffix(got, gotSuffix)
830+
if gotPrefix != tt.wantPrefix {
831+
t.Errorf("computeNewMachineSetName() = (%v, %v) wantPrefix %v", got, gotSuffix, tt.wantPrefix)
832+
}
833+
if len(got) > maxNameLength {
834+
t.Errorf("expected %s to be of max length %d", got, maxNameLength)
835+
}
836+
})
837+
}
838+
}

0 commit comments

Comments
 (0)