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

🐛 MD controller: use regular random suffix for MachineSets, ensure max length 63 #9298

Merged
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
11 changes: 6 additions & 5 deletions internal/controllers/machinedeployment/machinedeployment_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func (r *Reconciler) computeDesiredMachineSet(deployment *clusterv1.MachineDeplo
// Append a random string at the end of template hash. This is required to distinguish MachineSets that
// could be created with the same spec as a result of rolloutAfter. If not, computeDesiredMachineSet
// will end up updating the existing MachineSet instead of creating a new one.
uniqueIdentifierLabelValue = fmt.Sprintf("%d-%s", templateHash, apirand.String(5))

name = computeNewMachineSetName(deployment.Name+"-", apirand.SafeEncodeString(uniqueIdentifierLabelValue))
var randomSuffix string
name, randomSuffix = computeNewMachineSetName(deployment.Name + "-")
uniqueIdentifierLabelValue = fmt.Sprintf("%d-%s", templateHash, randomSuffix)

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

// scale scales proportionally in order to mitigate risk. Otherwise, scaling up can increase the size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package machinedeployment
import (
"context"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -799,3 +800,39 @@ func assertCondition(t *testing.T, from conditions.Getter, condition *clusterv1.
}
}
}

func Test_computeNewMachineSetName(t *testing.T) {
tests := []struct {
base string
wantPrefix string
}{
{
"a",
"a",
},
{
fmt.Sprintf("%058d", 0),
fmt.Sprintf("%058d", 0),
},
{
fmt.Sprintf("%059d", 0),
fmt.Sprintf("%058d", 0),
},
{
fmt.Sprintf("%0100d", 0),
fmt.Sprintf("%058d", 0),
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("base=%q, wantPrefix=%q", tt.base, tt.wantPrefix), func(t *testing.T) {
got, gotSuffix := computeNewMachineSetName(tt.base)
gotPrefix := strings.TrimSuffix(got, gotSuffix)
if gotPrefix != tt.wantPrefix {
t.Errorf("computeNewMachineSetName() = (%v, %v) wantPrefix %v", got, gotSuffix, tt.wantPrefix)
}
if len(got) > maxNameLength {
t.Errorf("expected %s to be of max length %d", got, maxNameLength)
}
})
}
}
Loading