Skip to content

Commit 1ce4ef8

Browse files
author
Kenneth Owens
committed
Adds the statefulset.kubernetes.io/pod-name label allowing users to
attach a Service to an individual Pod.
1 parent 98fb71e commit 1ce4ef8

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)