Skip to content

Commit

Permalink
update statefulset pod's image in updateStatefulSet
Browse files Browse the repository at this point in the history
  • Loading branch information
chenkaiyue committed Oct 14, 2020
1 parent b2de4a6 commit 92250c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/controller/statefulset/stateful_pod_control.go
Expand Up @@ -108,6 +108,12 @@ func (spc *realStatefulPodControl) UpdateStatefulPod(set *apps.StatefulSet, pod
return err
}
}
// if the Pod does not conform to the StatefulSet's image, update the Pod's image
if !imageMatches(set, pod) {
updateImage(set, pod)
consistent = false
}

// if the Pod is not dirty, do nothing
if consistent {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/statefulset/stateful_set_control.go
Expand Up @@ -452,7 +452,7 @@ func (ssc *defaultStatefulSetControl) updateStatefulSet(
return &status, nil
}
// Enforce the StatefulSet invariants
if identityMatches(set, replicas[i]) && storageMatches(set, replicas[i]) {
if identityMatches(set, replicas[i]) && storageMatches(set, replicas[i]) && imageMatches(set, replicas[i]) {
continue
}
// Make a deep copy so we don't mutate the shared cache
Expand Down
27 changes: 27 additions & 0 deletions pkg/controller/statefulset/stateful_set_utils.go
Expand Up @@ -132,6 +132,20 @@ func storageMatches(set *apps.StatefulSet, pod *v1.Pod) bool {
return true
}

// imageMatches returns true if pod's image is the same with StatefulSet's image
func imageMatches(set *apps.StatefulSet, pod *v1.Pod) bool {
for _, cs := range set.Spec.Template.Spec.Containers {
for _, cp := range pod.Spec.Containers {
if cs.Name == cp.Name {
if cs.Image != cp.Image {
return false
}
}
}
}
return true
}

// getPersistentVolumeClaims gets a map of PersistentVolumeClaims to their template names, as defined in set. The
// returned PersistentVolumeClaims are each constructed with a the name specific to the Pod. This name is determined
// by getPersistentVolumeClaimName.
Expand Down Expand Up @@ -199,6 +213,19 @@ func updateIdentity(set *apps.StatefulSet, pod *v1.Pod) {
pod.Labels[apps.StatefulSetPodNameLabel] = pod.Name
}

// updateImage updates pod's image to conform to set's image
func updateImage(set *apps.StatefulSet, pod *v1.Pod) {
for si, sc := range set.Spec.Template.Spec.Containers {
for pi, pc := range pod.Spec.Containers {
if sc.Name == pc.Name {
if sc.Image != pc.Image {
pod.Spec.Containers[pi].Image = set.Spec.Template.Spec.Containers[si].Image
}
}
}
}
}

// isRunningAndReady returns true if pod is in the PodRunning Phase, if it has a condition of PodReady.
func isRunningAndReady(pod *v1.Pod) bool {
return pod.Status.Phase == v1.PodRunning && podutil.IsPodReady(pod)
Expand Down

0 comments on commit 92250c3

Please sign in to comment.