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

Make CloneSet and StatefulSet Support the generateName setting #1635

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions apis/apps/defaults/v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ func SetDefaultsUnitedDeployment(obj *v1alpha1.UnitedDeployment, injectTemplateD

// SetDefaults_CloneSet set default values for CloneSet.
func SetDefaultsCloneSet(obj *v1alpha1.CloneSet, injectTemplateDefaults bool) {
if obj.GenerateName == "" {
obj.GenerateName = obj.Name
}
if obj.Spec.Replicas == nil {
obj.Spec.Replicas = utilpointer.Int32Ptr(1)
}
Expand Down
4 changes: 4 additions & 0 deletions apis/apps/defaults/v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (

// SetDefaultsStatefulSet set default values for StatefulSet.
func SetDefaultsStatefulSet(obj *v1beta1.StatefulSet, injectTemplateDefaults bool) {
if obj.GenerateName == "" {
obj.GenerateName = obj.Name
}

if len(obj.Spec.PodManagementPolicy) == 0 {
obj.Spec.PodManagementPolicy = appsv1.OrderedReadyPodManagement
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/cloneset/cloneset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (r *ReconcileCloneSet) doReconcile(request reconcile.Request) (res reconcil
}
}

//release Pods ownerRef
// release Pods ownerRef
filteredPods, err = r.claimPods(instance, filteredPods)
if err != nil {
return reconcile.Result{}, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/cloneset/cloneset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import (
var c client.Client

var (
expectedRequest = reconcile.Request{NamespacedName: types.NamespacedName{Name: "foo", Namespace: "default"}}
expectedRequest = reconcile.Request{NamespacedName: types.NamespacedName{Name: "foooo", Namespace: "default"}}
images = []string{"nginx:1.9.1", "nginx:1.9.2", "nginx:1.9.3", "nginx:1.9.2-alphine", "nginx:1.9.3-alphine"}
clonesetUID = "123"
productionLabel = map[string]string{"type": "production"}
Expand All @@ -72,7 +72,7 @@ var (
func TestReconcile(t *testing.T) {
g := gomega.NewGomegaWithT(t)
instance := &appsv1alpha1.CloneSet{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
ObjectMeta: metav1.ObjectMeta{Name: "foooo", GenerateName: "foo", Namespace: "default"},
Spec: appsv1alpha1.CloneSetSpec{
Replicas: getInt32(1),
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestReconcile(t *testing.T) {
g.Expect(err).NotTo(gomega.HaveOccurred())
c = utilclient.NewClientFromManager(mgr, "test-cloneset-controller")

//recFn, requests := SetupTestReconcile(newReconciler(mgr))
// recFn, requests := SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, newReconciler(mgr))).NotTo(gomega.HaveOccurred())

ctx, cancel := context.WithCancel(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/cloneset/core/cloneset_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
}
clonesetutils.WriteRevisionHash(pod, revision)

pod.Name = fmt.Sprintf("%s-%s", cs.Name, id)
pod.Name = fmt.Sprintf("%s-%s", cs.GenerateName, id)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L98 was not covered by tests
Copy link
Member

@zmberg zmberg Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the case where the generateName filed for the inventory cluster is empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we leave pod name empty and let the apiserver assign unique names using generateName?

pod.Namespace = cs.Namespace
pod.Labels[appsv1alpha1.CloneSetInstanceID] = id

Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/cloneset/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func NewCloneSetWithVolumes(replicas int, name string, petMounts []v1.VolumeMoun
APIVersion: "apps.kruise.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: v1.NamespaceDefault,
UID: types.UID("test"),
Name: name,
GenerateName: name,
Namespace: v1.NamespaceDefault,
UID: types.UID("test"),
},
Spec: appsv1alpha1.CloneSetSpec{
Selector: &metav1.LabelSelector{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/cloneset/utils/cloneset_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
}

// GetPersistentVolumeClaims gets a map of PersistentVolumeClaims to their template names, as defined in set. The
// returned PersistentVolumeClaims are each constructed with a the name specific to the Pod. This name is determined
// returned PersistentVolumeClaims are each constructed with the name specific to the Pod. This name is determined
// by getPersistentVolumeClaimName.
func GetPersistentVolumeClaims(cs *appsv1alpha1.CloneSet, pod *v1.Pod) map[string]v1.PersistentVolumeClaim {
templates := cs.Spec.VolumeClaimTemplates
Expand Down Expand Up @@ -211,7 +211,7 @@
// getPersistentVolumeClaimName gets the name of PersistentVolumeClaim for a Pod with an instance id. claim
// must be a PersistentVolumeClaim from set's VolumeClaims template.
func getPersistentVolumeClaimName(cs *appsv1alpha1.CloneSet, claim *v1.PersistentVolumeClaim, id string) string {
return fmt.Sprintf("%s-%s-%s", claim.Name, cs.Name, id)
return fmt.Sprintf("%s-%s-%s", claim.Name, cs.GenerateName, id)

Check warning on line 214 in pkg/controller/cloneset/utils/cloneset_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/cloneset/utils/cloneset_utils.go#L214

Added line #L214 was not covered by tests
}

// DoItSlowly tries to call the provided function a total of 'count' times,
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/statefulset/stateful_pod_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ func TestPodClaimIsStale(t *testing.T) {
for _, tc := range testCases {
set := appsv1beta1.StatefulSet{}
set.Name = "set"
set.GenerateName = "set"
set.Namespace = "default"
set.Spec.PersistentVolumeClaimRetentionPolicy = &appsv1beta1.StatefulSetPersistentVolumeClaimRetentionPolicy{
WhenDeleted: appsv1beta1.RetainPersistentVolumeClaimRetentionPolicyType,
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/statefulset/stateful_set_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func getParentNameAndOrdinal(pod *v1.Pod) (string, int) {
return parent, ordinal
}

// getParentName gets the name of pod's parent StatefulSet. If pod has not parent, the empty string is returned.
// getParentName gets the name of pod's parent StatefulSet. If pod has no parent, the empty string is returned.
func getParentName(pod *v1.Pod) string {
parent, _ := getParentNameAndOrdinal(pod)
return parent
Expand All @@ -80,26 +80,26 @@ func getOrdinal(pod *v1.Pod) int {

// getPodName gets the name of set's child Pod with an ordinal index of ordinal
func getPodName(set *appsv1beta1.StatefulSet, ordinal int) string {
return fmt.Sprintf("%s-%d", set.Name, ordinal)
return fmt.Sprintf("%s-%d", set.GenerateName, ordinal)
}

// getPersistentVolumeClaimName gets the name of PersistentVolumeClaim for a Pod with an ordinal index of ordinal. claim
// must be a PersistentVolumeClaim from set's VolumeClaims template.
func getPersistentVolumeClaimName(set *appsv1beta1.StatefulSet, claim *v1.PersistentVolumeClaim, ordinal int) string {
// NOTE: This name format is used by the heuristics for zone spreading in ChooseZoneForVolume
return fmt.Sprintf("%s-%s-%d", claim.Name, set.Name, ordinal)
return fmt.Sprintf("%s-%s-%d", claim.Name, set.GenerateName, ordinal)
}

// isMemberOf tests if pod is a member of set.
func isMemberOf(set *appsv1beta1.StatefulSet, pod *v1.Pod) bool {
return getParentName(pod) == set.Name
return getParentName(pod) == set.GenerateName
}

// identityMatches returns true if pod has a valid identity and network identity for a member of set.
func identityMatches(set *appsv1beta1.StatefulSet, pod *v1.Pod) bool {
parent, ordinal := getParentNameAndOrdinal(pod)
return ordinal >= 0 &&
set.Name == parent &&
set.GenerateName == parent &&
pod.Name == getPodName(set, ordinal) &&
pod.Namespace == set.Namespace &&
pod.Labels[apps.StatefulSetPodNameLabel] == pod.Name
Expand Down
14 changes: 8 additions & 6 deletions pkg/controller/statefulset/stateful_set_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (o overlappingStatefulSets) Less(i, j int) bool {
func TestGetParentNameAndOrdinal(t *testing.T) {
set := newStatefulSet(3)
pod := newStatefulSetPod(set, 1)
if parent, ordinal := getParentNameAndOrdinal(pod); parent != set.Name {
if parent, ordinal := getParentNameAndOrdinal(pod); parent != set.GenerateName {
t.Errorf("Extracted the wrong parent name expected %s found %s", set.Name, parent)
} else if ordinal != 1 {
t.Errorf("Extracted the wrong ordinal expected %d found %d", 1, ordinal)
Expand Down Expand Up @@ -118,6 +118,7 @@ func TestIsMemberOf(t *testing.T) {
set := newStatefulSet(3)
set2 := newStatefulSet(3)
set2.Name = "foo2"
set2.GenerateName = "foo2-gen"
pod := newStatefulSetPod(set, 1)
if !isMemberOf(set, pod) {
t.Error("isMemberOf returned false negative")
Expand Down Expand Up @@ -801,7 +802,7 @@ func newPVC(name string) corev1.PersistentVolumeClaim {
}
}

func newStatefulSetWithVolumes(replicas int, name string, petMounts []corev1.VolumeMount, podMounts []corev1.VolumeMount) *appsv1beta1.StatefulSet {
func newStatefulSetWithVolumes(replicas int, name string, generateName string, petMounts []corev1.VolumeMount, podMounts []corev1.VolumeMount) *appsv1beta1.StatefulSet {
mounts := append(petMounts, podMounts...)
claims := []corev1.PersistentVolumeClaim{}
for _, m := range petMounts {
Expand Down Expand Up @@ -841,9 +842,10 @@ func newStatefulSetWithVolumes(replicas int, name string, petMounts []corev1.Vol
APIVersion: "apps.kruise.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: corev1.NamespaceDefault,
UID: "test",
Name: name,
GenerateName: generateName,
Namespace: corev1.NamespaceDefault,
UID: "test",
},
Spec: appsv1beta1.StatefulSetSpec{
Selector: &metav1.LabelSelector{
Expand Down Expand Up @@ -873,5 +875,5 @@ func newStatefulSet(replicas int) *appsv1beta1.StatefulSet {
podMounts := []corev1.VolumeMount{
{Name: "home", MountPath: "/home"},
}
return newStatefulSetWithVolumes(replicas, "foo", petMounts, podMounts)
return newStatefulSetWithVolumes(replicas, "foo", "foo-gen", petMounts, podMounts)
}
Loading