mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #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 #44103, #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. ```
This commit is contained in:
commit
5ac4f172f9
@ -112,7 +112,8 @@ func identityMatches(set *apps.StatefulSet, pod *v1.Pod) bool {
|
|||||||
return ordinal >= 0 &&
|
return ordinal >= 0 &&
|
||||||
set.Name == parent &&
|
set.Name == parent &&
|
||||||
pod.Name == getPodName(set, ordinal) &&
|
pod.Name == getPodName(set, ordinal) &&
|
||||||
pod.Namespace == set.Namespace
|
pod.Namespace == set.Namespace &&
|
||||||
|
pod.Labels[apps.StatefulSetPodNameLabel] == pod.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// storageMatches returns true if pod's Volumes cover the set of PersistentVolumeClaims
|
// storageMatches returns true if pod's Volumes cover the set of PersistentVolumeClaims
|
||||||
@ -187,11 +188,15 @@ func initIdentity(set *apps.StatefulSet, pod *v1.Pod) {
|
|||||||
pod.Spec.Subdomain = set.Spec.ServiceName
|
pod.Spec.Subdomain = set.Spec.ServiceName
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateIdentity updates pod's name, hostname, and subdomain to conform to set's name and headless service.
|
// 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) {
|
func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
|
||||||
pod.Name = getPodName(set, getOrdinal(pod))
|
pod.Name = getPodName(set, getOrdinal(pod))
|
||||||
pod.Namespace = set.Namespace
|
pod.Namespace = set.Namespace
|
||||||
|
if pod.Labels == nil {
|
||||||
|
pod.Labels = make(map[string]string)
|
||||||
|
}
|
||||||
|
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
@ -78,6 +78,11 @@ func TestIdentityMatches(t *testing.T) {
|
|||||||
if identityMatches(set, pod) {
|
if identityMatches(set, pod) {
|
||||||
t.Error("identity matches for a Pod with the wrong namespace")
|
t.Error("identity matches for a Pod with the wrong namespace")
|
||||||
}
|
}
|
||||||
|
pod = newStatefulSetPod(set, 1)
|
||||||
|
delete(pod.Labels, apps.StatefulSetPodNameLabel)
|
||||||
|
if identityMatches(set, pod) {
|
||||||
|
t.Error("identity matches for a Pod with the wrong statefulSetPodNameLabel")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStorageMatches(t *testing.T) {
|
func TestStorageMatches(t *testing.T) {
|
||||||
@ -127,6 +132,11 @@ func TestUpdateIdentity(t *testing.T) {
|
|||||||
if !identityMatches(set, pod) {
|
if !identityMatches(set, pod) {
|
||||||
t.Error("updateIdentity failed to update the Pods namespace")
|
t.Error("updateIdentity failed to update the Pods namespace")
|
||||||
}
|
}
|
||||||
|
delete(pod.Labels, apps.StatefulSetPodNameLabel)
|
||||||
|
updateIdentity(set, pod)
|
||||||
|
if !identityMatches(set, pod) {
|
||||||
|
t.Error("updateIdentity failed to restore the statefulSetPodName label")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateStorage(t *testing.T) {
|
func TestUpdateStorage(t *testing.T) {
|
||||||
|
@ -28,6 +28,7 @@ const (
|
|||||||
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
||||||
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// +genclient
|
// +genclient
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
ControllerRevisionHashLabelKey = "controller-revision-hash"
|
ControllerRevisionHashLabelKey = "controller-revision-hash"
|
||||||
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
||||||
|
StatefulSetPodNameLabel = "statefulset.kubernetes.io/pod-name"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ScaleSpec describes the attributes of a scale subresource
|
// ScaleSpec describes the attributes of a scale subresource
|
||||||
|
@ -28,6 +28,7 @@ const (
|
|||||||
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
StatefulSetRevisionLabel = ControllerRevisionHashLabelKey
|
||||||
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ScaleSpec describes the attributes of a scale subresource
|
// ScaleSpec describes the attributes of a scale subresource
|
||||||
|
Loading…
Reference in New Issue
Block a user