From 4147ad7db6dc5532af7d26a6f6acbf27bc59fb85 Mon Sep 17 00:00:00 2001 From: veophi Date: Wed, 11 Sep 2024 17:03:26 +0800 Subject: [PATCH] a feature-gate to limit cloneset pod name within 63 chars or less Signed-off-by: veophi --- pkg/controller/cloneset/core/cloneset_core.go | 18 ++++++- .../cloneset/core/cloneset_core_test.go | 47 +++++++++++++++++++ pkg/features/kruise_features.go | 4 ++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/pkg/controller/cloneset/core/cloneset_core.go b/pkg/controller/cloneset/core/cloneset_core.go index 5c26b22861..237dc9108b 100644 --- a/pkg/controller/cloneset/core/cloneset_core.go +++ b/pkg/controller/cloneset/core/cloneset_core.go @@ -27,14 +27,21 @@ import ( "k8s.io/klog/v2" podutil "k8s.io/kubernetes/pkg/api/v1/pod" kubecontroller "k8s.io/kubernetes/pkg/controller" + "k8s.io/utils/integer" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" appspub "github.com/openkruise/kruise/apis/apps/pub" appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" clonesetutils "github.com/openkruise/kruise/pkg/controller/cloneset/utils" + "github.com/openkruise/kruise/pkg/features" + utilfeature "github.com/openkruise/kruise/pkg/util/feature" "github.com/openkruise/kruise/pkg/util/inplaceupdate" ) +const ( + shortNameLimitation = 63 +) + var ( inPlaceUpdateTemplateSpecPatchRexp = regexp.MustCompile("^/containers/([0-9]+)/image$") ) @@ -96,7 +103,7 @@ func (c *commonControl) newVersionedPods(cs *appsv1alpha1.CloneSet, revision str } clonesetutils.WriteRevisionHash(pod, revision) - pod.Name = fmt.Sprintf("%s-%s", cs.Name, id) + pod.Name = generatePodName(cs.Name, id) pod.Namespace = cs.Namespace pod.Labels[appsv1alpha1.CloneSetInstanceID] = id @@ -249,3 +256,12 @@ func lifecycleFinalizerChanged(cs *appsv1alpha1.CloneSet, oldPod, curPod *v1.Pod return false } + +func generatePodName(prefix, id string) string { + name := fmt.Sprintf("%s-%s", prefix, id) + if !utilfeature.DefaultFeatureGate.Enabled(features.CloneSetShortPodName) || len(name) < shortNameLimitation { + return name + } + maxPrefixLen := integer.IntMax(integer.IntMin(len(prefix), 63-len(id)), 0) + return fmt.Sprintf("%s%s", prefix[:maxPrefixLen], id) +} diff --git a/pkg/controller/cloneset/core/cloneset_core_test.go b/pkg/controller/cloneset/core/cloneset_core_test.go index 75da20c915..faf66a1de8 100644 --- a/pkg/controller/cloneset/core/cloneset_core_test.go +++ b/pkg/controller/cloneset/core/cloneset_core_test.go @@ -5,6 +5,8 @@ import ( "testing" appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" + "github.com/openkruise/kruise/pkg/features" + utilfeature "github.com/openkruise/kruise/pkg/util/feature" "github.com/openkruise/kruise/pkg/util/inplaceupdate" ) @@ -72,3 +74,48 @@ func Test_CommonControl_GetUpdateOptions(t *testing.T) { }) } } + +func TestGeneratePodName(t *testing.T) { + tests := []struct { + name string + prefix string + id string + shortName string + longName string + }{ + { + name: "short prefix case", + prefix: "short-prefix", + id: "abcdefg", + shortName: "short-prefix-abcdefg", + longName: "short-prefix-abcdefg", + }, + { + name: "long prefix case", + prefix: "looooooooooooooooooooooooooooooooooooooooooooooooooooooooong-prefix", + id: "abcdefg", + shortName: "loooooooooooooooooooooooooooooooooooooooooooooooooooooooabcdefg", + longName: "looooooooooooooooooooooooooooooooooooooooooooooooooooooooong-prefix-abcdefg", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + defer utilfeature.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CloneSetShortPodName, true)() + generatedName := generatePodName(test.prefix, test.id) + if generatedName != test.shortName || len(generatedName) > 63 { + t.Fatalf("expect %s, but got %s", test.shortName, generatedName) + } + }) + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + defer utilfeature.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CloneSetShortPodName, false)() + generatedName := generatePodName(test.prefix, test.id) + if generatedName != test.longName { + t.Fatalf("expect %s, but got %s", test.longName, generatedName) + } + }) + } +} diff --git a/pkg/features/kruise_features.go b/pkg/features/kruise_features.go index 78eab56276..0600c14dae 100644 --- a/pkg/features/kruise_features.go +++ b/pkg/features/kruise_features.go @@ -131,6 +131,9 @@ const ( // Enables policies auto resizing PVCs created by a StatefulSet when user expands volumeClaimTemplates. StatefulSetAutoResizePVCGate featuregate.Feature = "StatefulSetAutoResizePVCGate" + + // CloneSetShortPodName enable CloneSet create Pods with 63 name length or less. + CloneSetShortPodName featuregate.Feature = "CloneSetShortPodName" ) var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ @@ -166,6 +169,7 @@ var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ PodIndexLabel: {Default: true, PreRelease: featuregate.Beta}, EnableExternalCerts: {Default: false, PreRelease: featuregate.Alpha}, StatefulSetAutoResizePVCGate: {Default: false, PreRelease: featuregate.Alpha}, + CloneSetShortPodName: {Default: false, PreRelease: featuregate.Alpha}, } func init() {