Skip to content

Commit 5ac4f17

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#55329 from kow3ns/sts-name-label
Automatic merge from submit-queue (batch tested with PRs 55340, 55329, 56168, 56170, 56105). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Sts per Pod Name Label **What this PR does / why we need it**: StatefulSet controller will add a label for each Pod in the StatefulSet. The label is of the form `statefulset.kubernetes.io/pod-name: <pod.Name>`. This allows a unique service to be created for each Pod in the StatefulSet. Fixes kubernetes#44103, kubernetes#28660 ```release-note StatefulSet controller will create a label for each Pod in a StatefulSet. The label is named statefulset.kubernetes.io/pod-name and it is equal to the name of the Pod. This allows users to create a Service per Pod to expose a connection to individual Pods. ```
2 parents 3bb6eee + 1ce4ef8 commit 5ac4f17

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

pkg/controller/statefulset/stateful_set_utils.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ func identityMatches(set *apps.StatefulSet, pod *v1.Pod) bool {
112112
return ordinal >= 0 &&
113113
set.Name == parent &&
114114
pod.Name == getPodName(set, ordinal) &&
115-
pod.Namespace == set.Namespace
115+
pod.Namespace == set.Namespace &&
116+
pod.Labels[apps.StatefulSetPodNameLabel] == pod.Name
116117
}
117118

118119
// storageMatches returns true if pod's Volumes cover the set of PersistentVolumeClaims
@@ -187,11 +188,15 @@ func initIdentity(set *apps.StatefulSet, pod *v1.Pod) {
187188
pod.Spec.Subdomain = set.Spec.ServiceName
188189
}
189190

190-
// updateIdentity updates pod's name, hostname, and subdomain to conform to set's name and headless service.
191+
// updateIdentity updates pod's name, hostname, and subdomain, and StatefulSetPodNameLabel to conform to set's name
192+
// and headless service.
191193
func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
192194
pod.Name = getPodName(set, getOrdinal(pod))
193195
pod.Namespace = set.Namespace
194-
196+
if pod.Labels == nil {
197+
pod.Labels = make(map[string]string)
198+
}
199+
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
195200
}
196201

197202
// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady.

pkg/controller/statefulset/stateful_set_utils_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ func TestIdentityMatches(t *testing.T) {
7878
if identityMatches(set, pod) {
7979
t.Error("identity matches for a Pod with the wrong namespace")
8080
}
81+
pod = newStatefulSetPod(set, 1)
82+
delete(pod.Labels, apps.StatefulSetPodNameLabel)
83+
if identityMatches(set, pod) {
84+
t.Error("identity matches for a Pod with the wrong statefulSetPodNameLabel")
85+
}
8186
}
8287

8388
func TestStorageMatches(t *testing.T) {
@@ -127,6 +132,11 @@ func TestUpdateIdentity(t *testing.T) {
127132
if !identityMatches(set, pod) {
128133
t.Error("updateIdentity failed to update the Pods namespace")
129134
}
135+
delete(pod.Labels, apps.StatefulSetPodNameLabel)
136+
updateIdentity(set, pod)
137+
if !identityMatches(set, pod) {
138+
t.Error("updateIdentity failed to restore the statefulSetPodName label")
139+
}
130140
}
131141

132142
func TestUpdateStorage(t *testing.T) {

staging/src/k8s.io/api/apps/v1/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
2929
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
3030
DeprecatedTemplateGeneration = "deprecated.daemonset.template.generation"
31+
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
3132
)
3233

3334
// +genclient

staging/src/k8s.io/api/apps/v1beta1/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
const (
2727
ControllerRevisionHashLabelKey = "controller-revision-hash"
2828
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
29+
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
2930
)
3031

3132
// ScaleSpec describes the attributes of a scale subresource

staging/src/k8s.io/api/apps/v1beta2/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
2929
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
3030
DeprecatedTemplateGeneration = "deprecated.daemonset.template.generation"
31+
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
3132
)
3233

3334
// ScaleSpec describes the attributes of a scale subresource

0 commit comments

Comments
 (0)