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

Add StatefulSet pod index as pod label #119232

Merged
merged 4 commits into from Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/controller/statefulset/stateful_set_control_test.go
Expand Up @@ -227,6 +227,31 @@ func CreatesPods(t *testing.T, set *apps.StatefulSet, invariants invariantFunc)
if set.Status.UpdatedReplicas != 3 {
t.Error("Failed to set UpdatedReplicas correctly")
}
// Check all pods have correct pod index label.
if utilfeature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
selector, err := metav1.LabelSelectorAsSelector(set.Spec.Selector)
if err != nil {
t.Error(err)
}
pods, err := om.podsLister.Pods(set.Namespace).List(selector)
if err != nil {
t.Error(err)
}
danielvegamyhre marked this conversation as resolved.
Show resolved Hide resolved
if len(pods) != 3 {
t.Errorf("Expected 3 pods, got %d", len(pods))
}
for _, pod := range pods {
podIndexFromLabel, exists := pod.Labels[apps.StatefulSetPodIndexLabel]
if !exists {
t.Errorf("Missing pod index label: %s", apps.StatefulSetPodIndexLabel)
continue
}
podIndexFromName := strconv.Itoa(getOrdinal(pod))
if podIndexFromLabel != podIndexFromName {
t.Errorf("Pod index label value (%s) does not match pod index in pod name (%s)", podIndexFromLabel, podIndexFromName)
}
}
}
}

func ScalesUp(t *testing.T, set *apps.StatefulSet, invariants invariantFunc) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/statefulset/stateful_set_utils.go
Expand Up @@ -390,12 +390,16 @@ func initIdentity(set *apps.StatefulSet, pod *v1.Pod) {
// updateIdentity updates pod's name, hostname, and subdomain, and StatefulSetPodNameLabel to conform to set's name
// and headless service.
func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
pod.Name = getPodName(set, getOrdinal(pod))
ordinal := getOrdinal(pod)
pod.Name = getPodName(set, ordinal)
pod.Namespace = set.Namespace
if pod.Labels == nil {
pod.Labels = make(map[string]string)
}
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
if utilfeature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
pod.Labels[apps.StatefulSetPodIndexLabel] = strconv.Itoa(ordinal)
}
}

// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady.
Expand Down
3 changes: 2 additions & 1 deletion staging/src/k8s.io/api/apps/v1/types.go
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1

import (
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -29,6 +29,7 @@ const (
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
DeprecatedTemplateGeneration = "deprecated.daemonset.template.generation"
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
StatefulSetPodIndexLabel = "apps.kubernetes.io/pod-index"
liggitt marked this conversation as resolved.
Show resolved Hide resolved
)

// +genclient
Expand Down