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

A feature-gate to limit CloneSet pod name within 63 chars or less #1737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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 @@
"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 @@
}
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 @@

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), shortNameLimitation-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
Loading