Skip to content

Commit

Permalink
a feature-gate to limit cloneset pod name within 63 chars or less
Browse files Browse the repository at this point in the history
Signed-off-by: veophi <sunweixiang@xiaohongshu.com>
  • Loading branch information
veophi committed Sep 11, 2024
1 parent 2d992bf commit 4147ad7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/controller/cloneset/core/cloneset_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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$")
)
Expand Down Expand Up @@ -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)

Check warning on line 106 in pkg/controller/cloneset/core/cloneset_core.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/cloneset/core/cloneset_core.go#L106

Added line #L106 was not covered by tests
pod.Namespace = cs.Namespace
pod.Labels[appsv1alpha1.CloneSetInstanceID] = id

Expand Down Expand Up @@ -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)
}
47 changes: 47 additions & 0 deletions pkg/controller/cloneset/core/cloneset_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/features/kruise_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4147ad7

Please sign in to comment.