mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-20 09:05:26 +00:00
Add StatefulSet pod index as pod label (#119232)
* add statefulset pod index as pod label * change statefulset pod index label name * check 3 pods * change label variable name
This commit is contained in:
parent
56f6030125
commit
7698fe7639
@ -227,6 +227,31 @@ func CreatesPods(t *testing.T, set *apps.StatefulSet, invariants invariantFunc)
|
|||||||
if set.Status.UpdatedReplicas != 3 {
|
if set.Status.UpdatedReplicas != 3 {
|
||||||
t.Error("Failed to set UpdatedReplicas correctly")
|
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)
|
||||||
|
}
|
||||||
|
if len(pods) != 3 {
|
||||||
|
t.Errorf("Expected 3 pods, got %d", len(pods))
|
||||||
|
}
|
||||||
|
for _, pod := range pods {
|
||||||
|
podIndexFromLabel, exists := pod.Labels[apps.PodIndexLabel]
|
||||||
|
if !exists {
|
||||||
|
t.Errorf("Missing pod index label: %s", apps.PodIndexLabel)
|
||||||
|
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) {
|
func ScalesUp(t *testing.T, set *apps.StatefulSet, invariants invariantFunc) {
|
||||||
|
@ -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
|
// updateIdentity updates pod's name, hostname, and subdomain, and StatefulSetPodNameLabel to conform to set's name
|
||||||
// and headless service.
|
// and headless service.
|
||||||
func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
|
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
|
pod.Namespace = set.Namespace
|
||||||
if pod.Labels == nil {
|
if pod.Labels == nil {
|
||||||
pod.Labels = make(map[string]string)
|
pod.Labels = make(map[string]string)
|
||||||
}
|
}
|
||||||
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
|
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
|
||||||
|
if utilfeature.DefaultFeatureGate.Enabled(features.PodIndexLabel) {
|
||||||
|
pod.Labels[apps.PodIndexLabel] = strconv.Itoa(ordinal)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady.
|
// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady.
|
||||||
|
@ -17,7 +17,7 @@ limitations under the License.
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
@ -29,6 +29,7 @@ const (
|
|||||||
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
|
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
|
||||||
DeprecatedTemplateGeneration = "deprecated.daemonset.template.generation"
|
DeprecatedTemplateGeneration = "deprecated.daemonset.template.generation"
|
||||||
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
|
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
|
||||||
|
PodIndexLabel = "apps.kubernetes.io/pod-index"
|
||||||
)
|
)
|
||||||
|
|
||||||
// +genclient
|
// +genclient
|
||||||
|
Loading…
Reference in New Issue
Block a user