Skip to content

Commit

Permalink
Merge pull request #51149 from kow3ns/cherry-pick-fix-51043
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Manual cherrypick of #51043

StatefulSet controller no longer attempts to mutate v1.PodSpec.Hostname or v1.PodSpec.Subdomain
fixes: #51043 
Partial fix for #48327 

Manual cherrypick of #51044

```release-note
StatefulSet: Fix "forbidden pod updates" error on Pods created prior to upgrading to 1.7. (#48327)
```
  • Loading branch information
Kubernetes Submit Queue committed Aug 23, 2017
2 parents 917fea0 + 23e671d commit e2b776a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 62 deletions.
28 changes: 0 additions & 28 deletions pkg/controller/statefulset/stateful_pod_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,34 +391,6 @@ func TestStatefulPodControlUpdatePodConflictSuccess(t *testing.T) {
}
}

func TestStatefulPodControlUpdatePodConflictFailure(t *testing.T) {
recorder := record.NewFakeRecorder(10)
set := newStatefulSet(3)
pod := newStatefulSetPod(set, 0)
fakeClient := &fake.Clientset{}
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
updatedPod := newStatefulSetPod(set, 0)
updatedPod.Spec.Hostname = "wrong"
indexer.Add(updatedPod)
podLister := corelisters.NewPodLister(indexer)
control := NewRealStatefulPodControl(fakeClient, nil, podLister, nil, recorder)
fakeClient.AddReactor("update", "pods", func(action core.Action) (bool, runtime.Object, error) {
update := action.(core.UpdateAction)
return true, update.GetObject(), apierrors.NewConflict(action.GetResource().GroupResource(), pod.Name, errors.New("conflict"))

})
pod.Name = "goo-0"
if err := control.UpdateStatefulPod(set, pod); err == nil {
t.Error("Failed update did not return an error")
}
events := collectEvents(recorder.Events)
if eventCount := len(events); eventCount != 1 {
t.Errorf("Expected 1 event for failed Pod update found %d", eventCount)
} else if !strings.Contains(events[0], v1.EventTypeWarning) {
t.Errorf("Expected normal event found %s", events[0])
}
}

func TestStatefulPodControlDeletesStatefulPod(t *testing.T) {
recorder := record.NewFakeRecorder(10)
set := newStatefulSet(3)
Expand Down
16 changes: 10 additions & 6 deletions pkg/controller/statefulset/stateful_set_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ func identityMatches(set *apps.StatefulSet, pod *v1.Pod) bool {
return ordinal >= 0 &&
set.Name == parent &&
pod.Name == getPodName(set, ordinal) &&
pod.Namespace == set.Namespace &&
pod.Spec.Hostname == pod.Name &&
pod.Spec.Subdomain == set.Spec.ServiceName
pod.Namespace == set.Namespace
}

// storageMatches returns true if pod's Volumes cover the set of PersistentVolumeClaims
Expand Down Expand Up @@ -186,12 +184,18 @@ func updateStorage(set *apps.StatefulSet, pod *v1.Pod) {
pod.Spec.Volumes = newVolumes
}

func initIdentity(set *apps.StatefulSet, pod *v1.Pod) {
updateIdentity(set, pod)
// Set these immutable fields only on initial Pod creation, not updates.
pod.Spec.Hostname = pod.Name
pod.Spec.Subdomain = set.Spec.ServiceName
}

// updateIdentity updates pod's name, hostname, and subdomain to conform to set's name and headless service.
func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
pod.Name = getPodName(set, getOrdinal(pod))
pod.Namespace = set.Namespace
pod.Spec.Hostname = pod.Name
pod.Spec.Subdomain = set.Spec.ServiceName

}

// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady, and if the init
Expand Down Expand Up @@ -276,7 +280,7 @@ func getPodRevision(pod *v1.Pod) string {
func newStatefulSetPod(set *apps.StatefulSet, ordinal int) *v1.Pod {
pod, _ := controller.GetPodFromTemplate(&set.Spec.Template, set, newControllerRef(set))
pod.Name = getPodName(set, ordinal)
updateIdentity(set, pod)
initIdentity(set, pod)
updateStorage(set, pod)
return pod
}
Expand Down
28 changes: 0 additions & 28 deletions pkg/controller/statefulset/stateful_set_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ func TestIdentityMatches(t *testing.T) {
if identityMatches(set, pod) {
t.Error("identity matches for a Pod with the wrong namespace")
}
pod = newStatefulSetPod(set, 1)
pod.Spec.Hostname = ""
if identityMatches(set, pod) {
t.Error("identity matches for a Pod with no hostname")
}
pod = newStatefulSetPod(set, 1)
pod.Spec.Subdomain = ""
if identityMatches(set, pod) {
t.Error("identity matches for a Pod with no subdomain")
}
}

func TestStorageMatches(t *testing.T) {
Expand Down Expand Up @@ -138,24 +128,6 @@ func TestUpdateIdentity(t *testing.T) {
if !identityMatches(set, pod) {
t.Error("updateIdentity failed to update the Pods namespace")
}
pod = newStatefulSetPod(set, 1)
pod.Spec.Hostname = ""
if identityMatches(set, pod) {
t.Error("identity matches for a Pod with no hostname")
}
updateIdentity(set, pod)
if !identityMatches(set, pod) {
t.Error("updateIdentity failed to update the Pod's hostname")
}
pod = newStatefulSetPod(set, 1)
pod.Spec.Subdomain = ""
if identityMatches(set, pod) {
t.Error("identity matches for a Pod with no subdomain")
}
updateIdentity(set, pod)
if !identityMatches(set, pod) {
t.Error("updateIdentity failed to update the Pod's subdomain")
}
}

func TestUpdateStorage(t *testing.T) {
Expand Down

0 comments on commit e2b776a

Please sign in to comment.