statefulset.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apps
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "time"
  19. "github.com/onsi/ginkgo"
  20. "github.com/onsi/gomega"
  21. apps "k8s.io/api/apps/v1"
  22. "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. klabels "k8s.io/apimachinery/pkg/labels"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "k8s.io/apimachinery/pkg/watch"
  28. clientset "k8s.io/client-go/kubernetes"
  29. watchtools "k8s.io/client-go/tools/watch"
  30. "k8s.io/kubernetes/test/e2e/framework"
  31. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  32. imageutils "k8s.io/kubernetes/test/utils/image"
  33. )
  34. const (
  35. zookeeperManifestPath = "test/e2e/testing-manifests/statefulset/zookeeper"
  36. mysqlGaleraManifestPath = "test/e2e/testing-manifests/statefulset/mysql-galera"
  37. redisManifestPath = "test/e2e/testing-manifests/statefulset/redis"
  38. cockroachDBManifestPath = "test/e2e/testing-manifests/statefulset/cockroachdb"
  39. // We don't restart MySQL cluster regardless of restartCluster, since MySQL doesn't handle restart well
  40. restartCluster = true
  41. // Timeout for reads from databases running on stateful pods.
  42. readTimeout = 60 * time.Second
  43. )
  44. // GCE Quota requirements: 3 pds, one per stateful pod manifest declared above.
  45. // GCE Api requirements: nodes and master need storage r/w permissions.
  46. var _ = SIGDescribe("StatefulSet", func() {
  47. f := framework.NewDefaultFramework("statefulset")
  48. var ns string
  49. var c clientset.Interface
  50. ginkgo.BeforeEach(func() {
  51. c = f.ClientSet
  52. ns = f.Namespace.Name
  53. })
  54. framework.KubeDescribe("Basic StatefulSet functionality [StatefulSetBasic]", func() {
  55. ssName := "ss"
  56. labels := map[string]string{
  57. "foo": "bar",
  58. "baz": "blah",
  59. }
  60. headlessSvcName := "test"
  61. var statefulPodMounts, podMounts []v1.VolumeMount
  62. var ss *apps.StatefulSet
  63. ginkgo.BeforeEach(func() {
  64. statefulPodMounts = []v1.VolumeMount{{Name: "datadir", MountPath: "/data/"}}
  65. podMounts = []v1.VolumeMount{{Name: "home", MountPath: "/home"}}
  66. ss = framework.NewStatefulSet(ssName, ns, headlessSvcName, 2, statefulPodMounts, podMounts, labels)
  67. ginkgo.By("Creating service " + headlessSvcName + " in namespace " + ns)
  68. headlessService := framework.CreateServiceSpec(headlessSvcName, "", true, labels)
  69. _, err := c.CoreV1().Services(ns).Create(headlessService)
  70. framework.ExpectNoError(err)
  71. })
  72. ginkgo.AfterEach(func() {
  73. if ginkgo.CurrentGinkgoTestDescription().Failed {
  74. framework.DumpDebugInfo(c, ns)
  75. }
  76. e2elog.Logf("Deleting all statefulset in ns %v", ns)
  77. framework.DeleteAllStatefulSets(c, ns)
  78. })
  79. // This can't be Conformance yet because it depends on a default
  80. // StorageClass and a dynamic provisioner.
  81. ginkgo.It("should provide basic identity", func() {
  82. ginkgo.By("Creating statefulset " + ssName + " in namespace " + ns)
  83. framework.SkipIfNoDefaultStorageClass(c)
  84. *(ss.Spec.Replicas) = 3
  85. sst := framework.NewStatefulSetTester(c)
  86. sst.PauseNewPods(ss)
  87. _, err := c.AppsV1().StatefulSets(ns).Create(ss)
  88. framework.ExpectNoError(err)
  89. ginkgo.By("Saturating stateful set " + ss.Name)
  90. sst.Saturate(ss)
  91. ginkgo.By("Verifying statefulset mounted data directory is usable")
  92. framework.ExpectNoError(sst.CheckMount(ss, "/data"))
  93. ginkgo.By("Verifying statefulset provides a stable hostname for each pod")
  94. framework.ExpectNoError(sst.CheckHostname(ss))
  95. ginkgo.By("Verifying statefulset set proper service name")
  96. framework.ExpectNoError(sst.CheckServiceName(ss, headlessSvcName))
  97. cmd := "echo $(hostname) | dd of=/data/hostname conv=fsync"
  98. ginkgo.By("Running " + cmd + " in all stateful pods")
  99. framework.ExpectNoError(sst.ExecInStatefulPods(ss, cmd))
  100. ginkgo.By("Restarting statefulset " + ss.Name)
  101. sst.Restart(ss)
  102. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  103. ginkgo.By("Verifying statefulset mounted data directory is usable")
  104. framework.ExpectNoError(sst.CheckMount(ss, "/data"))
  105. cmd = "if [ \"$(cat /data/hostname)\" = \"$(hostname)\" ]; then exit 0; else exit 1; fi"
  106. ginkgo.By("Running " + cmd + " in all stateful pods")
  107. framework.ExpectNoError(sst.ExecInStatefulPods(ss, cmd))
  108. })
  109. // This can't be Conformance yet because it depends on a default
  110. // StorageClass and a dynamic provisioner.
  111. ginkgo.It("should adopt matching orphans and release non-matching pods", func() {
  112. ginkgo.By("Creating statefulset " + ssName + " in namespace " + ns)
  113. framework.SkipIfNoDefaultStorageClass(c)
  114. *(ss.Spec.Replicas) = 1
  115. sst := framework.NewStatefulSetTester(c)
  116. sst.PauseNewPods(ss)
  117. // Replace ss with the one returned from Create() so it has the UID.
  118. // Save Kind since it won't be populated in the returned ss.
  119. kind := ss.Kind
  120. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  121. framework.ExpectNoError(err)
  122. ss.Kind = kind
  123. ginkgo.By("Saturating stateful set " + ss.Name)
  124. sst.Saturate(ss)
  125. pods := sst.GetPodList(ss)
  126. gomega.Expect(pods.Items).To(gomega.HaveLen(int(*ss.Spec.Replicas)))
  127. ginkgo.By("Checking that stateful set pods are created with ControllerRef")
  128. pod := pods.Items[0]
  129. controllerRef := metav1.GetControllerOf(&pod)
  130. gomega.Expect(controllerRef).ToNot(gomega.BeNil())
  131. gomega.Expect(controllerRef.Kind).To(gomega.Equal(ss.Kind))
  132. gomega.Expect(controllerRef.Name).To(gomega.Equal(ss.Name))
  133. gomega.Expect(controllerRef.UID).To(gomega.Equal(ss.UID))
  134. ginkgo.By("Orphaning one of the stateful set's pods")
  135. f.PodClient().Update(pod.Name, func(pod *v1.Pod) {
  136. pod.OwnerReferences = nil
  137. })
  138. ginkgo.By("Checking that the stateful set readopts the pod")
  139. gomega.Expect(framework.WaitForPodCondition(c, pod.Namespace, pod.Name, "adopted", framework.StatefulSetTimeout,
  140. func(pod *v1.Pod) (bool, error) {
  141. controllerRef := metav1.GetControllerOf(pod)
  142. if controllerRef == nil {
  143. return false, nil
  144. }
  145. if controllerRef.Kind != ss.Kind || controllerRef.Name != ss.Name || controllerRef.UID != ss.UID {
  146. return false, fmt.Errorf("pod has wrong controllerRef: %v", controllerRef)
  147. }
  148. return true, nil
  149. },
  150. )).To(gomega.Succeed(), "wait for pod %q to be readopted", pod.Name)
  151. ginkgo.By("Removing the labels from one of the stateful set's pods")
  152. prevLabels := pod.Labels
  153. f.PodClient().Update(pod.Name, func(pod *v1.Pod) {
  154. pod.Labels = nil
  155. })
  156. ginkgo.By("Checking that the stateful set releases the pod")
  157. gomega.Expect(framework.WaitForPodCondition(c, pod.Namespace, pod.Name, "released", framework.StatefulSetTimeout,
  158. func(pod *v1.Pod) (bool, error) {
  159. controllerRef := metav1.GetControllerOf(pod)
  160. if controllerRef != nil {
  161. return false, nil
  162. }
  163. return true, nil
  164. },
  165. )).To(gomega.Succeed(), "wait for pod %q to be released", pod.Name)
  166. // If we don't do this, the test leaks the Pod and PVC.
  167. ginkgo.By("Readding labels to the stateful set's pod")
  168. f.PodClient().Update(pod.Name, func(pod *v1.Pod) {
  169. pod.Labels = prevLabels
  170. })
  171. ginkgo.By("Checking that the stateful set readopts the pod")
  172. gomega.Expect(framework.WaitForPodCondition(c, pod.Namespace, pod.Name, "adopted", framework.StatefulSetTimeout,
  173. func(pod *v1.Pod) (bool, error) {
  174. controllerRef := metav1.GetControllerOf(pod)
  175. if controllerRef == nil {
  176. return false, nil
  177. }
  178. if controllerRef.Kind != ss.Kind || controllerRef.Name != ss.Name || controllerRef.UID != ss.UID {
  179. return false, fmt.Errorf("pod has wrong controllerRef: %v", controllerRef)
  180. }
  181. return true, nil
  182. },
  183. )).To(gomega.Succeed(), "wait for pod %q to be readopted", pod.Name)
  184. })
  185. // This can't be Conformance yet because it depends on a default
  186. // StorageClass and a dynamic provisioner.
  187. ginkgo.It("should not deadlock when a pod's predecessor fails", func() {
  188. ginkgo.By("Creating statefulset " + ssName + " in namespace " + ns)
  189. framework.SkipIfNoDefaultStorageClass(c)
  190. *(ss.Spec.Replicas) = 2
  191. sst := framework.NewStatefulSetTester(c)
  192. sst.PauseNewPods(ss)
  193. _, err := c.AppsV1().StatefulSets(ns).Create(ss)
  194. framework.ExpectNoError(err)
  195. sst.WaitForRunning(1, 0, ss)
  196. ginkgo.By("Resuming stateful pod at index 0.")
  197. sst.ResumeNextPod(ss)
  198. ginkgo.By("Waiting for stateful pod at index 1 to enter running.")
  199. sst.WaitForRunning(2, 1, ss)
  200. // Now we have 1 healthy and 1 unhealthy stateful pod. Deleting the healthy stateful pod should *not*
  201. // create a new stateful pod till the remaining stateful pod becomes healthy, which won't happen till
  202. // we set the healthy bit.
  203. ginkgo.By("Deleting healthy stateful pod at index 0.")
  204. sst.DeleteStatefulPodAtIndex(0, ss)
  205. ginkgo.By("Confirming stateful pod at index 0 is recreated.")
  206. sst.WaitForRunning(2, 1, ss)
  207. ginkgo.By("Resuming stateful pod at index 1.")
  208. sst.ResumeNextPod(ss)
  209. ginkgo.By("Confirming all stateful pods in statefulset are created.")
  210. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  211. })
  212. // This can't be Conformance yet because it depends on a default
  213. // StorageClass and a dynamic provisioner.
  214. ginkgo.It("should perform rolling updates and roll backs of template modifications with PVCs", func() {
  215. ginkgo.By("Creating a new StatefulSet with PVCs")
  216. framework.SkipIfNoDefaultStorageClass(c)
  217. *(ss.Spec.Replicas) = 3
  218. rollbackTest(c, ns, ss)
  219. })
  220. /*
  221. Release : v1.9
  222. Testname: StatefulSet, Rolling Update
  223. Description: StatefulSet MUST support the RollingUpdate strategy to automatically replace Pods one at a time when the Pod template changes. The StatefulSet's status MUST indicate the CurrentRevision and UpdateRevision. If the template is changed to match a prior revision, StatefulSet MUST detect this as a rollback instead of creating a new revision. This test does not depend on a preexisting default StorageClass or a dynamic provisioner.
  224. */
  225. framework.ConformanceIt("should perform rolling updates and roll backs of template modifications", func() {
  226. ginkgo.By("Creating a new StatefulSet")
  227. ss := framework.NewStatefulSet("ss2", ns, headlessSvcName, 3, nil, nil, labels)
  228. rollbackTest(c, ns, ss)
  229. })
  230. /*
  231. Release : v1.9
  232. Testname: StatefulSet, Rolling Update with Partition
  233. Description: StatefulSet's RollingUpdate strategy MUST support the Partition parameter for canaries and phased rollouts. If a Pod is deleted while a rolling update is in progress, StatefulSet MUST restore the Pod without violating the Partition. This test does not depend on a preexisting default StorageClass or a dynamic provisioner.
  234. */
  235. framework.ConformanceIt("should perform canary updates and phased rolling updates of template modifications", func() {
  236. ginkgo.By("Creating a new StatefulSet")
  237. ss := framework.NewStatefulSet("ss2", ns, headlessSvcName, 3, nil, nil, labels)
  238. sst := framework.NewStatefulSetTester(c)
  239. sst.SetHTTPProbe(ss)
  240. ss.Spec.UpdateStrategy = apps.StatefulSetUpdateStrategy{
  241. Type: apps.RollingUpdateStatefulSetStrategyType,
  242. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  243. return &apps.RollingUpdateStatefulSetStrategy{
  244. Partition: func() *int32 {
  245. i := int32(3)
  246. return &i
  247. }()}
  248. }(),
  249. }
  250. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  251. framework.ExpectNoError(err)
  252. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  253. ss = sst.WaitForStatus(ss)
  254. currentRevision, updateRevision := ss.Status.CurrentRevision, ss.Status.UpdateRevision
  255. gomega.Expect(currentRevision).To(gomega.Equal(updateRevision),
  256. fmt.Sprintf("StatefulSet %s/%s created with update revision %s not equal to current revision %s",
  257. ss.Namespace, ss.Name, updateRevision, currentRevision))
  258. pods := sst.GetPodList(ss)
  259. for i := range pods.Items {
  260. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  261. fmt.Sprintf("Pod %s/%s revision %s is not equal to currentRevision %s",
  262. pods.Items[i].Namespace,
  263. pods.Items[i].Name,
  264. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  265. currentRevision))
  266. }
  267. newImage := NewNginxImage
  268. oldImage := ss.Spec.Template.Spec.Containers[0].Image
  269. ginkgo.By(fmt.Sprintf("Updating stateful set template: update image from %s to %s", oldImage, newImage))
  270. gomega.Expect(oldImage).NotTo(gomega.Equal(newImage), "Incorrect test setup: should update to a different image")
  271. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  272. update.Spec.Template.Spec.Containers[0].Image = newImage
  273. })
  274. framework.ExpectNoError(err)
  275. ginkgo.By("Creating a new revision")
  276. ss = sst.WaitForStatus(ss)
  277. currentRevision, updateRevision = ss.Status.CurrentRevision, ss.Status.UpdateRevision
  278. gomega.Expect(currentRevision).NotTo(gomega.Equal(updateRevision),
  279. "Current revision should not equal update revision during rolling update")
  280. ginkgo.By("Not applying an update when the partition is greater than the number of replicas")
  281. for i := range pods.Items {
  282. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(oldImage),
  283. fmt.Sprintf("Pod %s/%s has image %s not equal to current image %s",
  284. pods.Items[i].Namespace,
  285. pods.Items[i].Name,
  286. pods.Items[i].Spec.Containers[0].Image,
  287. oldImage))
  288. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  289. fmt.Sprintf("Pod %s/%s has revision %s not equal to current revision %s",
  290. pods.Items[i].Namespace,
  291. pods.Items[i].Name,
  292. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  293. currentRevision))
  294. }
  295. ginkgo.By("Performing a canary update")
  296. ss.Spec.UpdateStrategy = apps.StatefulSetUpdateStrategy{
  297. Type: apps.RollingUpdateStatefulSetStrategyType,
  298. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  299. return &apps.RollingUpdateStatefulSetStrategy{
  300. Partition: func() *int32 {
  301. i := int32(2)
  302. return &i
  303. }()}
  304. }(),
  305. }
  306. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  307. update.Spec.UpdateStrategy = apps.StatefulSetUpdateStrategy{
  308. Type: apps.RollingUpdateStatefulSetStrategyType,
  309. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  310. return &apps.RollingUpdateStatefulSetStrategy{
  311. Partition: func() *int32 {
  312. i := int32(2)
  313. return &i
  314. }()}
  315. }(),
  316. }
  317. })
  318. framework.ExpectNoError(err)
  319. ss, pods = sst.WaitForPartitionedRollingUpdate(ss)
  320. for i := range pods.Items {
  321. if i < int(*ss.Spec.UpdateStrategy.RollingUpdate.Partition) {
  322. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(oldImage),
  323. fmt.Sprintf("Pod %s/%s has image %s not equal to current image %s",
  324. pods.Items[i].Namespace,
  325. pods.Items[i].Name,
  326. pods.Items[i].Spec.Containers[0].Image,
  327. oldImage))
  328. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  329. fmt.Sprintf("Pod %s/%s has revision %s not equal to current revision %s",
  330. pods.Items[i].Namespace,
  331. pods.Items[i].Name,
  332. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  333. currentRevision))
  334. } else {
  335. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(newImage),
  336. fmt.Sprintf("Pod %s/%s has image %s not equal to new image %s",
  337. pods.Items[i].Namespace,
  338. pods.Items[i].Name,
  339. pods.Items[i].Spec.Containers[0].Image,
  340. newImage))
  341. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(updateRevision),
  342. fmt.Sprintf("Pod %s/%s has revision %s not equal to new revision %s",
  343. pods.Items[i].Namespace,
  344. pods.Items[i].Name,
  345. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  346. updateRevision))
  347. }
  348. }
  349. ginkgo.By("Restoring Pods to the correct revision when they are deleted")
  350. sst.DeleteStatefulPodAtIndex(0, ss)
  351. sst.DeleteStatefulPodAtIndex(2, ss)
  352. sst.WaitForRunningAndReady(3, ss)
  353. ss = sst.GetStatefulSet(ss.Namespace, ss.Name)
  354. pods = sst.GetPodList(ss)
  355. for i := range pods.Items {
  356. if i < int(*ss.Spec.UpdateStrategy.RollingUpdate.Partition) {
  357. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(oldImage),
  358. fmt.Sprintf("Pod %s/%s has image %s not equal to current image %s",
  359. pods.Items[i].Namespace,
  360. pods.Items[i].Name,
  361. pods.Items[i].Spec.Containers[0].Image,
  362. oldImage))
  363. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  364. fmt.Sprintf("Pod %s/%s has revision %s not equal to current revision %s",
  365. pods.Items[i].Namespace,
  366. pods.Items[i].Name,
  367. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  368. currentRevision))
  369. } else {
  370. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(newImage),
  371. fmt.Sprintf("Pod %s/%s has image %s not equal to new image %s",
  372. pods.Items[i].Namespace,
  373. pods.Items[i].Name,
  374. pods.Items[i].Spec.Containers[0].Image,
  375. newImage))
  376. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(updateRevision),
  377. fmt.Sprintf("Pod %s/%s has revision %s not equal to new revision %s",
  378. pods.Items[i].Namespace,
  379. pods.Items[i].Name,
  380. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  381. updateRevision))
  382. }
  383. }
  384. ginkgo.By("Performing a phased rolling update")
  385. for i := int(*ss.Spec.UpdateStrategy.RollingUpdate.Partition) - 1; i >= 0; i-- {
  386. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  387. update.Spec.UpdateStrategy = apps.StatefulSetUpdateStrategy{
  388. Type: apps.RollingUpdateStatefulSetStrategyType,
  389. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  390. j := int32(i)
  391. return &apps.RollingUpdateStatefulSetStrategy{
  392. Partition: &j,
  393. }
  394. }(),
  395. }
  396. })
  397. framework.ExpectNoError(err)
  398. ss, pods = sst.WaitForPartitionedRollingUpdate(ss)
  399. for i := range pods.Items {
  400. if i < int(*ss.Spec.UpdateStrategy.RollingUpdate.Partition) {
  401. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(oldImage),
  402. fmt.Sprintf("Pod %s/%s has image %s not equal to current image %s",
  403. pods.Items[i].Namespace,
  404. pods.Items[i].Name,
  405. pods.Items[i].Spec.Containers[0].Image,
  406. oldImage))
  407. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  408. fmt.Sprintf("Pod %s/%s has revision %s not equal to current revision %s",
  409. pods.Items[i].Namespace,
  410. pods.Items[i].Name,
  411. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  412. currentRevision))
  413. } else {
  414. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(newImage),
  415. fmt.Sprintf("Pod %s/%s has image %s not equal to new image %s",
  416. pods.Items[i].Namespace,
  417. pods.Items[i].Name,
  418. pods.Items[i].Spec.Containers[0].Image,
  419. newImage))
  420. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(updateRevision),
  421. fmt.Sprintf("Pod %s/%s has revision %s not equal to new revision %s",
  422. pods.Items[i].Namespace,
  423. pods.Items[i].Name,
  424. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  425. updateRevision))
  426. }
  427. }
  428. }
  429. gomega.Expect(ss.Status.CurrentRevision).To(gomega.Equal(updateRevision),
  430. fmt.Sprintf("StatefulSet %s/%s current revision %s does not equal update revision %s on update completion",
  431. ss.Namespace,
  432. ss.Name,
  433. ss.Status.CurrentRevision,
  434. updateRevision))
  435. })
  436. // Do not mark this as Conformance.
  437. // The legacy OnDelete strategy only exists for backward compatibility with pre-v1 APIs.
  438. ginkgo.It("should implement legacy replacement when the update strategy is OnDelete", func() {
  439. ginkgo.By("Creating a new StatefulSet")
  440. ss := framework.NewStatefulSet("ss2", ns, headlessSvcName, 3, nil, nil, labels)
  441. sst := framework.NewStatefulSetTester(c)
  442. sst.SetHTTPProbe(ss)
  443. ss.Spec.UpdateStrategy = apps.StatefulSetUpdateStrategy{
  444. Type: apps.OnDeleteStatefulSetStrategyType,
  445. }
  446. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  447. framework.ExpectNoError(err)
  448. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  449. ss = sst.WaitForStatus(ss)
  450. currentRevision, updateRevision := ss.Status.CurrentRevision, ss.Status.UpdateRevision
  451. gomega.Expect(currentRevision).To(gomega.Equal(updateRevision),
  452. fmt.Sprintf("StatefulSet %s/%s created with update revision %s not equal to current revision %s",
  453. ss.Namespace, ss.Name, updateRevision, currentRevision))
  454. pods := sst.GetPodList(ss)
  455. for i := range pods.Items {
  456. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  457. fmt.Sprintf("Pod %s/%s revision %s is not equal to current revision %s",
  458. pods.Items[i].Namespace,
  459. pods.Items[i].Name,
  460. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  461. currentRevision))
  462. }
  463. ginkgo.By("Restoring Pods to the current revision")
  464. sst.DeleteStatefulPodAtIndex(0, ss)
  465. sst.DeleteStatefulPodAtIndex(1, ss)
  466. sst.DeleteStatefulPodAtIndex(2, ss)
  467. sst.WaitForRunningAndReady(3, ss)
  468. ss = sst.GetStatefulSet(ss.Namespace, ss.Name)
  469. pods = sst.GetPodList(ss)
  470. for i := range pods.Items {
  471. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  472. fmt.Sprintf("Pod %s/%s revision %s is not equal to current revision %s",
  473. pods.Items[i].Namespace,
  474. pods.Items[i].Name,
  475. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  476. currentRevision))
  477. }
  478. newImage := NewNginxImage
  479. oldImage := ss.Spec.Template.Spec.Containers[0].Image
  480. ginkgo.By(fmt.Sprintf("Updating stateful set template: update image from %s to %s", oldImage, newImage))
  481. gomega.Expect(oldImage).NotTo(gomega.Equal(newImage), "Incorrect test setup: should update to a different image")
  482. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  483. update.Spec.Template.Spec.Containers[0].Image = newImage
  484. })
  485. framework.ExpectNoError(err)
  486. ginkgo.By("Creating a new revision")
  487. ss = sst.WaitForStatus(ss)
  488. currentRevision, updateRevision = ss.Status.CurrentRevision, ss.Status.UpdateRevision
  489. gomega.Expect(currentRevision).NotTo(gomega.Equal(updateRevision),
  490. "Current revision should not equal update revision during rolling update")
  491. ginkgo.By("Recreating Pods at the new revision")
  492. sst.DeleteStatefulPodAtIndex(0, ss)
  493. sst.DeleteStatefulPodAtIndex(1, ss)
  494. sst.DeleteStatefulPodAtIndex(2, ss)
  495. sst.WaitForRunningAndReady(3, ss)
  496. ss = sst.GetStatefulSet(ss.Namespace, ss.Name)
  497. pods = sst.GetPodList(ss)
  498. for i := range pods.Items {
  499. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(newImage),
  500. fmt.Sprintf("Pod %s/%s has image %s not equal to new image %s",
  501. pods.Items[i].Namespace,
  502. pods.Items[i].Name,
  503. pods.Items[i].Spec.Containers[0].Image,
  504. newImage))
  505. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(updateRevision),
  506. fmt.Sprintf("Pod %s/%s has revision %s not equal to current revision %s",
  507. pods.Items[i].Namespace,
  508. pods.Items[i].Name,
  509. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  510. updateRevision))
  511. }
  512. })
  513. /*
  514. Release : v1.9
  515. Testname: StatefulSet, Scaling
  516. Description: StatefulSet MUST create Pods in ascending order by ordinal index when scaling up, and delete Pods in descending order when scaling down. Scaling up or down MUST pause if any Pods belonging to the StatefulSet are unhealthy. This test does not depend on a preexisting default StorageClass or a dynamic provisioner.
  517. */
  518. framework.ConformanceIt("Scaling should happen in predictable order and halt if any stateful pod is unhealthy", func() {
  519. psLabels := klabels.Set(labels)
  520. ginkgo.By("Initializing watcher for selector " + psLabels.String())
  521. watcher, err := f.ClientSet.CoreV1().Pods(ns).Watch(metav1.ListOptions{
  522. LabelSelector: psLabels.AsSelector().String(),
  523. })
  524. framework.ExpectNoError(err)
  525. ginkgo.By("Creating stateful set " + ssName + " in namespace " + ns)
  526. ss := framework.NewStatefulSet(ssName, ns, headlessSvcName, 1, nil, nil, psLabels)
  527. sst := framework.NewStatefulSetTester(c)
  528. sst.SetHTTPProbe(ss)
  529. ss, err = c.AppsV1().StatefulSets(ns).Create(ss)
  530. framework.ExpectNoError(err)
  531. ginkgo.By("Waiting until all stateful set " + ssName + " replicas will be running in namespace " + ns)
  532. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  533. ginkgo.By("Confirming that stateful set scale up will halt with unhealthy stateful pod")
  534. sst.BreakHTTPProbe(ss)
  535. sst.WaitForRunningAndNotReady(*ss.Spec.Replicas, ss)
  536. sst.WaitForStatusReadyReplicas(ss, 0)
  537. sst.UpdateReplicas(ss, 3)
  538. sst.ConfirmStatefulPodCount(1, ss, 10*time.Second, true)
  539. ginkgo.By("Scaling up stateful set " + ssName + " to 3 replicas and waiting until all of them will be running in namespace " + ns)
  540. sst.RestoreHTTPProbe(ss)
  541. sst.WaitForRunningAndReady(3, ss)
  542. ginkgo.By("Verifying that stateful set " + ssName + " was scaled up in order")
  543. expectedOrder := []string{ssName + "-0", ssName + "-1", ssName + "-2"}
  544. ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), framework.StatefulSetTimeout)
  545. defer cancel()
  546. _, err = watchtools.UntilWithoutRetry(ctx, watcher, func(event watch.Event) (bool, error) {
  547. if event.Type != watch.Added {
  548. return false, nil
  549. }
  550. pod := event.Object.(*v1.Pod)
  551. if pod.Name == expectedOrder[0] {
  552. expectedOrder = expectedOrder[1:]
  553. }
  554. return len(expectedOrder) == 0, nil
  555. })
  556. framework.ExpectNoError(err)
  557. ginkgo.By("Scale down will halt with unhealthy stateful pod")
  558. watcher, err = f.ClientSet.CoreV1().Pods(ns).Watch(metav1.ListOptions{
  559. LabelSelector: psLabels.AsSelector().String(),
  560. })
  561. framework.ExpectNoError(err)
  562. sst.BreakHTTPProbe(ss)
  563. sst.WaitForStatusReadyReplicas(ss, 0)
  564. sst.WaitForRunningAndNotReady(3, ss)
  565. sst.UpdateReplicas(ss, 0)
  566. sst.ConfirmStatefulPodCount(3, ss, 10*time.Second, true)
  567. ginkgo.By("Scaling down stateful set " + ssName + " to 0 replicas and waiting until none of pods will run in namespace" + ns)
  568. sst.RestoreHTTPProbe(ss)
  569. sst.Scale(ss, 0)
  570. ginkgo.By("Verifying that stateful set " + ssName + " was scaled down in reverse order")
  571. expectedOrder = []string{ssName + "-2", ssName + "-1", ssName + "-0"}
  572. ctx, cancel = watchtools.ContextWithOptionalTimeout(context.Background(), framework.StatefulSetTimeout)
  573. defer cancel()
  574. _, err = watchtools.UntilWithoutRetry(ctx, watcher, func(event watch.Event) (bool, error) {
  575. if event.Type != watch.Deleted {
  576. return false, nil
  577. }
  578. pod := event.Object.(*v1.Pod)
  579. if pod.Name == expectedOrder[0] {
  580. expectedOrder = expectedOrder[1:]
  581. }
  582. return len(expectedOrder) == 0, nil
  583. })
  584. framework.ExpectNoError(err)
  585. })
  586. /*
  587. Release : v1.9
  588. Testname: StatefulSet, Burst Scaling
  589. Description: StatefulSet MUST support the Parallel PodManagementPolicy for burst scaling. This test does not depend on a preexisting default StorageClass or a dynamic provisioner.
  590. */
  591. framework.ConformanceIt("Burst scaling should run to completion even with unhealthy pods", func() {
  592. psLabels := klabels.Set(labels)
  593. ginkgo.By("Creating stateful set " + ssName + " in namespace " + ns)
  594. ss := framework.NewStatefulSet(ssName, ns, headlessSvcName, 1, nil, nil, psLabels)
  595. ss.Spec.PodManagementPolicy = apps.ParallelPodManagement
  596. sst := framework.NewStatefulSetTester(c)
  597. sst.SetHTTPProbe(ss)
  598. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  599. framework.ExpectNoError(err)
  600. ginkgo.By("Waiting until all stateful set " + ssName + " replicas will be running in namespace " + ns)
  601. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  602. ginkgo.By("Confirming that stateful set scale up will not halt with unhealthy stateful pod")
  603. sst.BreakHTTPProbe(ss)
  604. sst.WaitForRunningAndNotReady(*ss.Spec.Replicas, ss)
  605. sst.WaitForStatusReadyReplicas(ss, 0)
  606. sst.UpdateReplicas(ss, 3)
  607. sst.ConfirmStatefulPodCount(3, ss, 10*time.Second, false)
  608. ginkgo.By("Scaling up stateful set " + ssName + " to 3 replicas and waiting until all of them will be running in namespace " + ns)
  609. sst.RestoreHTTPProbe(ss)
  610. sst.WaitForRunningAndReady(3, ss)
  611. ginkgo.By("Scale down will not halt with unhealthy stateful pod")
  612. sst.BreakHTTPProbe(ss)
  613. sst.WaitForStatusReadyReplicas(ss, 0)
  614. sst.WaitForRunningAndNotReady(3, ss)
  615. sst.UpdateReplicas(ss, 0)
  616. sst.ConfirmStatefulPodCount(0, ss, 10*time.Second, false)
  617. ginkgo.By("Scaling down stateful set " + ssName + " to 0 replicas and waiting until none of pods will run in namespace" + ns)
  618. sst.RestoreHTTPProbe(ss)
  619. sst.Scale(ss, 0)
  620. sst.WaitForStatusReplicas(ss, 0)
  621. })
  622. /*
  623. Release : v1.9
  624. Testname: StatefulSet, Recreate Failed Pod
  625. Description: StatefulSet MUST delete and recreate Pods it owns that go into a Failed state, such as when they are rejected or evicted by a Node. This test does not depend on a preexisting default StorageClass or a dynamic provisioner.
  626. */
  627. framework.ConformanceIt("Should recreate evicted statefulset", func() {
  628. podName := "test-pod"
  629. statefulPodName := ssName + "-0"
  630. ginkgo.By("Looking for a node to schedule stateful set and pod")
  631. nodes := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  632. node := nodes.Items[0]
  633. ginkgo.By("Creating pod with conflicting port in namespace " + f.Namespace.Name)
  634. conflictingPort := v1.ContainerPort{HostPort: 21017, ContainerPort: 21017, Name: "conflict"}
  635. pod := &v1.Pod{
  636. ObjectMeta: metav1.ObjectMeta{
  637. Name: podName,
  638. },
  639. Spec: v1.PodSpec{
  640. Containers: []v1.Container{
  641. {
  642. Name: "nginx",
  643. Image: imageutils.GetE2EImage(imageutils.Nginx),
  644. Ports: []v1.ContainerPort{conflictingPort},
  645. },
  646. },
  647. NodeName: node.Name,
  648. },
  649. }
  650. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
  651. framework.ExpectNoError(err)
  652. ginkgo.By("Creating statefulset with conflicting port in namespace " + f.Namespace.Name)
  653. ss := framework.NewStatefulSet(ssName, f.Namespace.Name, headlessSvcName, 1, nil, nil, labels)
  654. statefulPodContainer := &ss.Spec.Template.Spec.Containers[0]
  655. statefulPodContainer.Ports = append(statefulPodContainer.Ports, conflictingPort)
  656. ss.Spec.Template.Spec.NodeName = node.Name
  657. _, err = f.ClientSet.AppsV1().StatefulSets(f.Namespace.Name).Create(ss)
  658. framework.ExpectNoError(err)
  659. ginkgo.By("Waiting until pod " + podName + " will start running in namespace " + f.Namespace.Name)
  660. if err := f.WaitForPodRunning(podName); err != nil {
  661. framework.Failf("Pod %v did not start running: %v", podName, err)
  662. }
  663. var initialStatefulPodUID types.UID
  664. ginkgo.By("Waiting until stateful pod " + statefulPodName + " will be recreated and deleted at least once in namespace " + f.Namespace.Name)
  665. w, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Watch(metav1.SingleObject(metav1.ObjectMeta{Name: statefulPodName}))
  666. framework.ExpectNoError(err)
  667. ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), framework.StatefulPodTimeout)
  668. defer cancel()
  669. // we need to get UID from pod in any state and wait until stateful set controller will remove pod at least once
  670. _, err = watchtools.UntilWithoutRetry(ctx, w, func(event watch.Event) (bool, error) {
  671. pod := event.Object.(*v1.Pod)
  672. switch event.Type {
  673. case watch.Deleted:
  674. e2elog.Logf("Observed delete event for stateful pod %v in namespace %v", pod.Name, pod.Namespace)
  675. if initialStatefulPodUID == "" {
  676. return false, nil
  677. }
  678. return true, nil
  679. }
  680. e2elog.Logf("Observed stateful pod in namespace: %v, name: %v, uid: %v, status phase: %v. Waiting for statefulset controller to delete.",
  681. pod.Namespace, pod.Name, pod.UID, pod.Status.Phase)
  682. initialStatefulPodUID = pod.UID
  683. return false, nil
  684. })
  685. if err != nil {
  686. framework.Failf("Pod %v expected to be re-created at least once", statefulPodName)
  687. }
  688. ginkgo.By("Removing pod with conflicting port in namespace " + f.Namespace.Name)
  689. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
  690. framework.ExpectNoError(err)
  691. ginkgo.By("Waiting when stateful pod " + statefulPodName + " will be recreated in namespace " + f.Namespace.Name + " and will be in running state")
  692. // we may catch delete event, that's why we are waiting for running phase like this, and not with watchtools.UntilWithoutRetry
  693. gomega.Eventually(func() error {
  694. statefulPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(statefulPodName, metav1.GetOptions{})
  695. if err != nil {
  696. return err
  697. }
  698. if statefulPod.Status.Phase != v1.PodRunning {
  699. return fmt.Errorf("Pod %v is not in running phase: %v", statefulPod.Name, statefulPod.Status.Phase)
  700. } else if statefulPod.UID == initialStatefulPodUID {
  701. return fmt.Errorf("Pod %v wasn't recreated: %v == %v", statefulPod.Name, statefulPod.UID, initialStatefulPodUID)
  702. }
  703. return nil
  704. }, framework.StatefulPodTimeout, 2*time.Second).Should(gomega.BeNil())
  705. })
  706. ginkgo.It("should have a working scale subresource", func() {
  707. ginkgo.By("Creating statefulset " + ssName + " in namespace " + ns)
  708. ss := framework.NewStatefulSet(ssName, ns, headlessSvcName, 1, nil, nil, labels)
  709. sst := framework.NewStatefulSetTester(c)
  710. sst.SetHTTPProbe(ss)
  711. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  712. framework.ExpectNoError(err)
  713. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  714. ss = sst.WaitForStatus(ss)
  715. ginkgo.By("getting scale subresource")
  716. scale, err := c.AppsV1().StatefulSets(ns).GetScale(ssName, metav1.GetOptions{})
  717. if err != nil {
  718. framework.Failf("Failed to get scale subresource: %v", err)
  719. }
  720. gomega.Expect(scale.Spec.Replicas).To(gomega.Equal(int32(1)))
  721. gomega.Expect(scale.Status.Replicas).To(gomega.Equal(int32(1)))
  722. ginkgo.By("updating a scale subresource")
  723. scale.Spec.Replicas = 2
  724. scaleResult, err := c.AppsV1().StatefulSets(ns).UpdateScale(ssName, scale)
  725. if err != nil {
  726. framework.Failf("Failed to put scale subresource: %v", err)
  727. }
  728. gomega.Expect(scaleResult.Spec.Replicas).To(gomega.Equal(int32(2)))
  729. ginkgo.By("verifying the statefulset Spec.Replicas was modified")
  730. ss, err = c.AppsV1().StatefulSets(ns).Get(ssName, metav1.GetOptions{})
  731. if err != nil {
  732. framework.Failf("Failed to get statefulset resource: %v", err)
  733. }
  734. gomega.Expect(*(ss.Spec.Replicas)).To(gomega.Equal(int32(2)))
  735. })
  736. })
  737. framework.KubeDescribe("Deploy clustered applications [Feature:StatefulSet] [Slow]", func() {
  738. var sst *framework.StatefulSetTester
  739. var appTester *clusterAppTester
  740. ginkgo.BeforeEach(func() {
  741. sst = framework.NewStatefulSetTester(c)
  742. appTester = &clusterAppTester{tester: sst, ns: ns}
  743. })
  744. ginkgo.AfterEach(func() {
  745. if ginkgo.CurrentGinkgoTestDescription().Failed {
  746. framework.DumpDebugInfo(c, ns)
  747. }
  748. e2elog.Logf("Deleting all statefulset in ns %v", ns)
  749. framework.DeleteAllStatefulSets(c, ns)
  750. })
  751. // Do not mark this as Conformance.
  752. // StatefulSet Conformance should not be dependent on specific applications.
  753. ginkgo.It("should creating a working zookeeper cluster", func() {
  754. appTester.statefulPod = &zookeeperTester{tester: sst}
  755. appTester.run()
  756. })
  757. // Do not mark this as Conformance.
  758. // StatefulSet Conformance should not be dependent on specific applications.
  759. ginkgo.It("should creating a working redis cluster", func() {
  760. appTester.statefulPod = &redisTester{tester: sst}
  761. appTester.run()
  762. })
  763. // Do not mark this as Conformance.
  764. // StatefulSet Conformance should not be dependent on specific applications.
  765. ginkgo.It("should creating a working mysql cluster", func() {
  766. appTester.statefulPod = &mysqlGaleraTester{tester: sst}
  767. appTester.run()
  768. })
  769. // Do not mark this as Conformance.
  770. // StatefulSet Conformance should not be dependent on specific applications.
  771. ginkgo.It("should creating a working CockroachDB cluster", func() {
  772. appTester.statefulPod = &cockroachDBTester{tester: sst}
  773. appTester.run()
  774. })
  775. })
  776. })
  777. func kubectlExecWithRetries(args ...string) (out string) {
  778. var err error
  779. for i := 0; i < 3; i++ {
  780. if out, err = framework.RunKubectl(args...); err == nil {
  781. return
  782. }
  783. e2elog.Logf("Retrying %v:\nerror %v\nstdout %v", args, err, out)
  784. }
  785. framework.Failf("Failed to execute \"%v\" with retries: %v", args, err)
  786. return
  787. }
  788. type statefulPodTester interface {
  789. deploy(ns string) *apps.StatefulSet
  790. write(statefulPodIndex int, kv map[string]string)
  791. read(statefulPodIndex int, key string) string
  792. name() string
  793. }
  794. type clusterAppTester struct {
  795. ns string
  796. statefulPod statefulPodTester
  797. tester *framework.StatefulSetTester
  798. }
  799. func (c *clusterAppTester) run() {
  800. ginkgo.By("Deploying " + c.statefulPod.name())
  801. ss := c.statefulPod.deploy(c.ns)
  802. ginkgo.By("Creating foo:bar in member with index 0")
  803. c.statefulPod.write(0, map[string]string{"foo": "bar"})
  804. switch c.statefulPod.(type) {
  805. case *mysqlGaleraTester:
  806. // Don't restart MySQL cluster since it doesn't handle restarts well
  807. default:
  808. if restartCluster {
  809. ginkgo.By("Restarting stateful set " + ss.Name)
  810. c.tester.Restart(ss)
  811. c.tester.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  812. }
  813. }
  814. ginkgo.By("Reading value under foo from member with index 2")
  815. if err := pollReadWithTimeout(c.statefulPod, 2, "foo", "bar"); err != nil {
  816. framework.Failf("%v", err)
  817. }
  818. }
  819. type zookeeperTester struct {
  820. ss *apps.StatefulSet
  821. tester *framework.StatefulSetTester
  822. }
  823. func (z *zookeeperTester) name() string {
  824. return "zookeeper"
  825. }
  826. func (z *zookeeperTester) deploy(ns string) *apps.StatefulSet {
  827. z.ss = z.tester.CreateStatefulSet(zookeeperManifestPath, ns)
  828. return z.ss
  829. }
  830. func (z *zookeeperTester) write(statefulPodIndex int, kv map[string]string) {
  831. name := fmt.Sprintf("%v-%d", z.ss.Name, statefulPodIndex)
  832. ns := fmt.Sprintf("--namespace=%v", z.ss.Namespace)
  833. for k, v := range kv {
  834. cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh create /%v %v", k, v)
  835. e2elog.Logf(framework.RunKubectlOrDie("exec", ns, name, "--", "/bin/sh", "-c", cmd))
  836. }
  837. }
  838. func (z *zookeeperTester) read(statefulPodIndex int, key string) string {
  839. name := fmt.Sprintf("%v-%d", z.ss.Name, statefulPodIndex)
  840. ns := fmt.Sprintf("--namespace=%v", z.ss.Namespace)
  841. cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh get /%v", key)
  842. return lastLine(framework.RunKubectlOrDie("exec", ns, name, "--", "/bin/sh", "-c", cmd))
  843. }
  844. type mysqlGaleraTester struct {
  845. ss *apps.StatefulSet
  846. tester *framework.StatefulSetTester
  847. }
  848. func (m *mysqlGaleraTester) name() string {
  849. return "mysql: galera"
  850. }
  851. func (m *mysqlGaleraTester) mysqlExec(cmd, ns, podName string) string {
  852. cmd = fmt.Sprintf("/usr/bin/mysql -u root -B -e '%v'", cmd)
  853. // TODO: Find a readiness probe for mysql that guarantees writes will
  854. // succeed and ditch retries. Current probe only reads, so there's a window
  855. // for a race.
  856. return kubectlExecWithRetries(fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
  857. }
  858. func (m *mysqlGaleraTester) deploy(ns string) *apps.StatefulSet {
  859. m.ss = m.tester.CreateStatefulSet(mysqlGaleraManifestPath, ns)
  860. e2elog.Logf("Deployed statefulset %v, initializing database", m.ss.Name)
  861. for _, cmd := range []string{
  862. "create database statefulset;",
  863. "use statefulset; create table foo (k varchar(20), v varchar(20));",
  864. } {
  865. e2elog.Logf(m.mysqlExec(cmd, ns, fmt.Sprintf("%v-0", m.ss.Name)))
  866. }
  867. return m.ss
  868. }
  869. func (m *mysqlGaleraTester) write(statefulPodIndex int, kv map[string]string) {
  870. name := fmt.Sprintf("%v-%d", m.ss.Name, statefulPodIndex)
  871. for k, v := range kv {
  872. cmd := fmt.Sprintf("use statefulset; insert into foo (k, v) values (\"%v\", \"%v\");", k, v)
  873. e2elog.Logf(m.mysqlExec(cmd, m.ss.Namespace, name))
  874. }
  875. }
  876. func (m *mysqlGaleraTester) read(statefulPodIndex int, key string) string {
  877. name := fmt.Sprintf("%v-%d", m.ss.Name, statefulPodIndex)
  878. return lastLine(m.mysqlExec(fmt.Sprintf("use statefulset; select v from foo where k=\"%v\";", key), m.ss.Namespace, name))
  879. }
  880. type redisTester struct {
  881. ss *apps.StatefulSet
  882. tester *framework.StatefulSetTester
  883. }
  884. func (m *redisTester) name() string {
  885. return "redis: master/slave"
  886. }
  887. func (m *redisTester) redisExec(cmd, ns, podName string) string {
  888. cmd = fmt.Sprintf("/opt/redis/redis-cli -h %v %v", podName, cmd)
  889. return framework.RunKubectlOrDie(fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
  890. }
  891. func (m *redisTester) deploy(ns string) *apps.StatefulSet {
  892. m.ss = m.tester.CreateStatefulSet(redisManifestPath, ns)
  893. return m.ss
  894. }
  895. func (m *redisTester) write(statefulPodIndex int, kv map[string]string) {
  896. name := fmt.Sprintf("%v-%d", m.ss.Name, statefulPodIndex)
  897. for k, v := range kv {
  898. e2elog.Logf(m.redisExec(fmt.Sprintf("SET %v %v", k, v), m.ss.Namespace, name))
  899. }
  900. }
  901. func (m *redisTester) read(statefulPodIndex int, key string) string {
  902. name := fmt.Sprintf("%v-%d", m.ss.Name, statefulPodIndex)
  903. return lastLine(m.redisExec(fmt.Sprintf("GET %v", key), m.ss.Namespace, name))
  904. }
  905. type cockroachDBTester struct {
  906. ss *apps.StatefulSet
  907. tester *framework.StatefulSetTester
  908. }
  909. func (c *cockroachDBTester) name() string {
  910. return "CockroachDB"
  911. }
  912. func (c *cockroachDBTester) cockroachDBExec(cmd, ns, podName string) string {
  913. cmd = fmt.Sprintf("/cockroach/cockroach sql --insecure --host %s.cockroachdb -e \"%v\"", podName, cmd)
  914. return framework.RunKubectlOrDie(fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
  915. }
  916. func (c *cockroachDBTester) deploy(ns string) *apps.StatefulSet {
  917. c.ss = c.tester.CreateStatefulSet(cockroachDBManifestPath, ns)
  918. e2elog.Logf("Deployed statefulset %v, initializing database", c.ss.Name)
  919. for _, cmd := range []string{
  920. "CREATE DATABASE IF NOT EXISTS foo;",
  921. "CREATE TABLE IF NOT EXISTS foo.bar (k STRING PRIMARY KEY, v STRING);",
  922. } {
  923. e2elog.Logf(c.cockroachDBExec(cmd, ns, fmt.Sprintf("%v-0", c.ss.Name)))
  924. }
  925. return c.ss
  926. }
  927. func (c *cockroachDBTester) write(statefulPodIndex int, kv map[string]string) {
  928. name := fmt.Sprintf("%v-%d", c.ss.Name, statefulPodIndex)
  929. for k, v := range kv {
  930. cmd := fmt.Sprintf("UPSERT INTO foo.bar VALUES ('%v', '%v');", k, v)
  931. e2elog.Logf(c.cockroachDBExec(cmd, c.ss.Namespace, name))
  932. }
  933. }
  934. func (c *cockroachDBTester) read(statefulPodIndex int, key string) string {
  935. name := fmt.Sprintf("%v-%d", c.ss.Name, statefulPodIndex)
  936. return lastLine(c.cockroachDBExec(fmt.Sprintf("SELECT v FROM foo.bar WHERE k='%v';", key), c.ss.Namespace, name))
  937. }
  938. func lastLine(out string) string {
  939. outLines := strings.Split(strings.Trim(out, "\n"), "\n")
  940. return outLines[len(outLines)-1]
  941. }
  942. func pollReadWithTimeout(statefulPod statefulPodTester, statefulPodNumber int, key, expectedVal string) error {
  943. err := wait.PollImmediate(time.Second, readTimeout, func() (bool, error) {
  944. val := statefulPod.read(statefulPodNumber, key)
  945. if val == "" {
  946. return false, nil
  947. } else if val != expectedVal {
  948. return false, fmt.Errorf("expected value %v, found %v", expectedVal, val)
  949. }
  950. return true, nil
  951. })
  952. if err == wait.ErrWaitTimeout {
  953. return fmt.Errorf("timed out when trying to read value for key %v from stateful pod %d", key, statefulPodNumber)
  954. }
  955. return err
  956. }
  957. // This function is used by two tests to test StatefulSet rollbacks: one using
  958. // PVCs and one using no storage.
  959. func rollbackTest(c clientset.Interface, ns string, ss *apps.StatefulSet) {
  960. sst := framework.NewStatefulSetTester(c)
  961. sst.SetHTTPProbe(ss)
  962. ss, err := c.AppsV1().StatefulSets(ns).Create(ss)
  963. framework.ExpectNoError(err)
  964. sst.WaitForRunningAndReady(*ss.Spec.Replicas, ss)
  965. ss = sst.WaitForStatus(ss)
  966. currentRevision, updateRevision := ss.Status.CurrentRevision, ss.Status.UpdateRevision
  967. gomega.Expect(currentRevision).To(gomega.Equal(updateRevision),
  968. fmt.Sprintf("StatefulSet %s/%s created with update revision %s not equal to current revision %s",
  969. ss.Namespace, ss.Name, updateRevision, currentRevision))
  970. pods := sst.GetPodList(ss)
  971. for i := range pods.Items {
  972. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(currentRevision),
  973. fmt.Sprintf("Pod %s/%s revision %s is not equal to current revision %s",
  974. pods.Items[i].Namespace,
  975. pods.Items[i].Name,
  976. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  977. currentRevision))
  978. }
  979. sst.SortStatefulPods(pods)
  980. err = sst.BreakPodHTTPProbe(ss, &pods.Items[1])
  981. framework.ExpectNoError(err)
  982. ss, pods = sst.WaitForPodNotReady(ss, pods.Items[1].Name)
  983. newImage := NewNginxImage
  984. oldImage := ss.Spec.Template.Spec.Containers[0].Image
  985. ginkgo.By(fmt.Sprintf("Updating StatefulSet template: update image from %s to %s", oldImage, newImage))
  986. gomega.Expect(oldImage).NotTo(gomega.Equal(newImage), "Incorrect test setup: should update to a different image")
  987. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  988. update.Spec.Template.Spec.Containers[0].Image = newImage
  989. })
  990. framework.ExpectNoError(err)
  991. ginkgo.By("Creating a new revision")
  992. ss = sst.WaitForStatus(ss)
  993. currentRevision, updateRevision = ss.Status.CurrentRevision, ss.Status.UpdateRevision
  994. gomega.Expect(currentRevision).NotTo(gomega.Equal(updateRevision),
  995. "Current revision should not equal update revision during rolling update")
  996. ginkgo.By("Updating Pods in reverse ordinal order")
  997. pods = sst.GetPodList(ss)
  998. sst.SortStatefulPods(pods)
  999. err = sst.RestorePodHTTPProbe(ss, &pods.Items[1])
  1000. framework.ExpectNoError(err)
  1001. ss, pods = sst.WaitForPodReady(ss, pods.Items[1].Name)
  1002. ss, pods = sst.WaitForRollingUpdate(ss)
  1003. gomega.Expect(ss.Status.CurrentRevision).To(gomega.Equal(updateRevision),
  1004. fmt.Sprintf("StatefulSet %s/%s current revision %s does not equal update revision %s on update completion",
  1005. ss.Namespace,
  1006. ss.Name,
  1007. ss.Status.CurrentRevision,
  1008. updateRevision))
  1009. for i := range pods.Items {
  1010. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(newImage),
  1011. fmt.Sprintf(" Pod %s/%s has image %s not have new image %s",
  1012. pods.Items[i].Namespace,
  1013. pods.Items[i].Name,
  1014. pods.Items[i].Spec.Containers[0].Image,
  1015. newImage))
  1016. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(updateRevision),
  1017. fmt.Sprintf("Pod %s/%s revision %s is not equal to update revision %s",
  1018. pods.Items[i].Namespace,
  1019. pods.Items[i].Name,
  1020. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  1021. updateRevision))
  1022. }
  1023. ginkgo.By("Rolling back to a previous revision")
  1024. err = sst.BreakPodHTTPProbe(ss, &pods.Items[1])
  1025. framework.ExpectNoError(err)
  1026. ss, pods = sst.WaitForPodNotReady(ss, pods.Items[1].Name)
  1027. priorRevision := currentRevision
  1028. currentRevision, updateRevision = ss.Status.CurrentRevision, ss.Status.UpdateRevision
  1029. ss, err = framework.UpdateStatefulSetWithRetries(c, ns, ss.Name, func(update *apps.StatefulSet) {
  1030. update.Spec.Template.Spec.Containers[0].Image = oldImage
  1031. })
  1032. framework.ExpectNoError(err)
  1033. ss = sst.WaitForStatus(ss)
  1034. currentRevision, updateRevision = ss.Status.CurrentRevision, ss.Status.UpdateRevision
  1035. gomega.Expect(currentRevision).NotTo(gomega.Equal(updateRevision),
  1036. "Current revision should not equal update revision during roll back")
  1037. gomega.Expect(priorRevision).To(gomega.Equal(updateRevision),
  1038. "Prior revision should equal update revision during roll back")
  1039. ginkgo.By("Rolling back update in reverse ordinal order")
  1040. pods = sst.GetPodList(ss)
  1041. sst.SortStatefulPods(pods)
  1042. sst.RestorePodHTTPProbe(ss, &pods.Items[1])
  1043. ss, pods = sst.WaitForPodReady(ss, pods.Items[1].Name)
  1044. ss, pods = sst.WaitForRollingUpdate(ss)
  1045. gomega.Expect(ss.Status.CurrentRevision).To(gomega.Equal(priorRevision),
  1046. fmt.Sprintf("StatefulSet %s/%s current revision %s does not equal prior revision %s on rollback completion",
  1047. ss.Namespace,
  1048. ss.Name,
  1049. ss.Status.CurrentRevision,
  1050. updateRevision))
  1051. for i := range pods.Items {
  1052. gomega.Expect(pods.Items[i].Spec.Containers[0].Image).To(gomega.Equal(oldImage),
  1053. fmt.Sprintf("Pod %s/%s has image %s not equal to previous image %s",
  1054. pods.Items[i].Namespace,
  1055. pods.Items[i].Name,
  1056. pods.Items[i].Spec.Containers[0].Image,
  1057. oldImage))
  1058. gomega.Expect(pods.Items[i].Labels[apps.StatefulSetRevisionLabel]).To(gomega.Equal(priorRevision),
  1059. fmt.Sprintf("Pod %s/%s revision %s is not equal to prior revision %s",
  1060. pods.Items[i].Namespace,
  1061. pods.Items[i].Name,
  1062. pods.Items[i].Labels[apps.StatefulSetRevisionLabel],
  1063. priorRevision))
  1064. }
  1065. }