|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package apps |
| 18 | + |
| 19 | +import ( |
| 20 | + appsv1 "k8s.io/api/apps/v1" |
| 21 | + v1 "k8s.io/api/core/v1" |
| 22 | + clientset "k8s.io/client-go/kubernetes" |
| 23 | + |
| 24 | + podutil "k8s.io/kubernetes/pkg/api/v1/pod" |
| 25 | + "k8s.io/kubernetes/test/e2e/framework" |
| 26 | + e2esset "k8s.io/kubernetes/test/e2e/framework/statefulset" |
| 27 | +) |
| 28 | + |
| 29 | +// waitForPartitionedRollingUpdate waits for all Pods in set to exist and have the correct revision. set must have |
| 30 | +// a RollingUpdateStatefulSetStrategyType with a non-nil RollingUpdate and Partition. All Pods with ordinals less |
| 31 | +// than or equal to the Partition are expected to be at set's current revision. All other Pods are expected to be |
| 32 | +// at its update revision. |
| 33 | +func waitForPartitionedRollingUpdate(c clientset.Interface, set *appsv1.StatefulSet) (*appsv1.StatefulSet, *v1.PodList) { |
| 34 | + var pods *v1.PodList |
| 35 | + if set.Spec.UpdateStrategy.Type != appsv1.RollingUpdateStatefulSetStrategyType { |
| 36 | + framework.Failf("StatefulSet %s/%s attempt to wait for partitioned update with updateStrategy %s", |
| 37 | + set.Namespace, |
| 38 | + set.Name, |
| 39 | + set.Spec.UpdateStrategy.Type) |
| 40 | + } |
| 41 | + if set.Spec.UpdateStrategy.RollingUpdate == nil || set.Spec.UpdateStrategy.RollingUpdate.Partition == nil { |
| 42 | + framework.Failf("StatefulSet %s/%s attempt to wait for partitioned update with nil RollingUpdate or nil Partition", |
| 43 | + set.Namespace, |
| 44 | + set.Name) |
| 45 | + } |
| 46 | + e2esset.WaitForState(c, set, func(set2 *appsv1.StatefulSet, pods2 *v1.PodList) (bool, error) { |
| 47 | + set = set2 |
| 48 | + pods = pods2 |
| 49 | + partition := int(*set.Spec.UpdateStrategy.RollingUpdate.Partition) |
| 50 | + if len(pods.Items) < int(*set.Spec.Replicas) { |
| 51 | + return false, nil |
| 52 | + } |
| 53 | + if partition <= 0 && set.Status.UpdateRevision != set.Status.CurrentRevision { |
| 54 | + framework.Logf("Waiting for StatefulSet %s/%s to complete update", |
| 55 | + set.Namespace, |
| 56 | + set.Name, |
| 57 | + ) |
| 58 | + e2esset.SortStatefulPods(pods) |
| 59 | + for i := range pods.Items { |
| 60 | + if pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel] != set.Status.UpdateRevision { |
| 61 | + framework.Logf("Waiting for Pod %s/%s to have revision %s update revision %s", |
| 62 | + pods.Items[i].Namespace, |
| 63 | + pods.Items[i].Name, |
| 64 | + set.Status.UpdateRevision, |
| 65 | + pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel]) |
| 66 | + } |
| 67 | + } |
| 68 | + return false, nil |
| 69 | + } |
| 70 | + for i := int(*set.Spec.Replicas) - 1; i >= partition; i-- { |
| 71 | + if pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel] != set.Status.UpdateRevision { |
| 72 | + framework.Logf("Waiting for Pod %s/%s to have revision %s update revision %s", |
| 73 | + pods.Items[i].Namespace, |
| 74 | + pods.Items[i].Name, |
| 75 | + set.Status.UpdateRevision, |
| 76 | + pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel]) |
| 77 | + return false, nil |
| 78 | + } |
| 79 | + } |
| 80 | + return true, nil |
| 81 | + }) |
| 82 | + return set, pods |
| 83 | +} |
| 84 | + |
| 85 | +// waitForStatus waits for the StatefulSetStatus's ObservedGeneration to be greater than or equal to set's Generation. |
| 86 | +// The returned StatefulSet contains such a StatefulSetStatus |
| 87 | +func waitForStatus(c clientset.Interface, set *appsv1.StatefulSet) *appsv1.StatefulSet { |
| 88 | + e2esset.WaitForState(c, set, func(set2 *appsv1.StatefulSet, pods *v1.PodList) (bool, error) { |
| 89 | + if set2.Status.ObservedGeneration >= set.Generation { |
| 90 | + set = set2 |
| 91 | + return true, nil |
| 92 | + } |
| 93 | + return false, nil |
| 94 | + }) |
| 95 | + return set |
| 96 | +} |
| 97 | + |
| 98 | +// waitForPodNotReady waits for the Pod named podName in set to exist and to not have a Ready condition. |
| 99 | +func waitForPodNotReady(c clientset.Interface, set *appsv1.StatefulSet, podName string) (*appsv1.StatefulSet, *v1.PodList) { |
| 100 | + var pods *v1.PodList |
| 101 | + e2esset.WaitForState(c, set, func(set2 *appsv1.StatefulSet, pods2 *v1.PodList) (bool, error) { |
| 102 | + set = set2 |
| 103 | + pods = pods2 |
| 104 | + for i := range pods.Items { |
| 105 | + if pods.Items[i].Name == podName { |
| 106 | + return !podutil.IsPodReady(&pods.Items[i]), nil |
| 107 | + } |
| 108 | + } |
| 109 | + return false, nil |
| 110 | + }) |
| 111 | + return set, pods |
| 112 | +} |
| 113 | + |
| 114 | +// waitForRollingUpdate waits for all Pods in set to exist and have the correct revision and for the RollingUpdate to |
| 115 | +// complete. set must have a RollingUpdateStatefulSetStrategyType. |
| 116 | +func waitForRollingUpdate(c clientset.Interface, set *appsv1.StatefulSet) (*appsv1.StatefulSet, *v1.PodList) { |
| 117 | + var pods *v1.PodList |
| 118 | + if set.Spec.UpdateStrategy.Type != appsv1.RollingUpdateStatefulSetStrategyType { |
| 119 | + framework.Failf("StatefulSet %s/%s attempt to wait for rolling update with updateStrategy %s", |
| 120 | + set.Namespace, |
| 121 | + set.Name, |
| 122 | + set.Spec.UpdateStrategy.Type) |
| 123 | + } |
| 124 | + e2esset.WaitForState(c, set, func(set2 *appsv1.StatefulSet, pods2 *v1.PodList) (bool, error) { |
| 125 | + set = set2 |
| 126 | + pods = pods2 |
| 127 | + if len(pods.Items) < int(*set.Spec.Replicas) { |
| 128 | + return false, nil |
| 129 | + } |
| 130 | + if set.Status.UpdateRevision != set.Status.CurrentRevision { |
| 131 | + framework.Logf("Waiting for StatefulSet %s/%s to complete update", |
| 132 | + set.Namespace, |
| 133 | + set.Name, |
| 134 | + ) |
| 135 | + e2esset.SortStatefulPods(pods) |
| 136 | + for i := range pods.Items { |
| 137 | + if pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel] != set.Status.UpdateRevision { |
| 138 | + framework.Logf("Waiting for Pod %s/%s to have revision %s update revision %s", |
| 139 | + pods.Items[i].Namespace, |
| 140 | + pods.Items[i].Name, |
| 141 | + set.Status.UpdateRevision, |
| 142 | + pods.Items[i].Labels[appsv1.StatefulSetRevisionLabel]) |
| 143 | + } |
| 144 | + } |
| 145 | + return false, nil |
| 146 | + } |
| 147 | + return true, nil |
| 148 | + }) |
| 149 | + return set, pods |
| 150 | +} |
| 151 | + |
| 152 | +// waitForRunningAndNotReady waits for numStatefulPods in ss to be Running and not Ready. |
| 153 | +func waitForRunningAndNotReady(c clientset.Interface, numStatefulPods int32, ss *appsv1.StatefulSet) { |
| 154 | + e2esset.WaitForRunning(c, numStatefulPods, 0, ss) |
| 155 | +} |
0 commit comments