petset.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 e2e
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "time"
  21. inf "gopkg.in/inf.v0"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. "k8s.io/kubernetes/pkg/api"
  25. apierrs "k8s.io/kubernetes/pkg/api/errors"
  26. "k8s.io/kubernetes/pkg/api/resource"
  27. "k8s.io/kubernetes/pkg/api/unversioned"
  28. "k8s.io/kubernetes/pkg/apis/apps"
  29. client "k8s.io/kubernetes/pkg/client/unversioned"
  30. "k8s.io/kubernetes/pkg/controller/petset"
  31. "k8s.io/kubernetes/pkg/labels"
  32. "k8s.io/kubernetes/pkg/runtime"
  33. "k8s.io/kubernetes/pkg/util/sets"
  34. "k8s.io/kubernetes/pkg/util/wait"
  35. utilyaml "k8s.io/kubernetes/pkg/util/yaml"
  36. "k8s.io/kubernetes/test/e2e/framework"
  37. )
  38. const (
  39. petsetPoll = 10 * time.Second
  40. // Some pets install base packages via wget
  41. petsetTimeout = 10 * time.Minute
  42. zookeeperManifestPath = "test/e2e/testing-manifests/petset/zookeeper"
  43. mysqlGaleraManifestPath = "test/e2e/testing-manifests/petset/mysql-galera"
  44. redisManifestPath = "test/e2e/testing-manifests/petset/redis"
  45. // Should the test restart petset clusters?
  46. // TODO: enable when we've productionzed bringup of pets in this e2e.
  47. restartCluster = false
  48. )
  49. // Time: 25m, slow by design.
  50. // GCE Quota requirements: 3 pds, one per pet manifest declared above.
  51. // GCE Api requirements: nodes and master need storage r/w permissions.
  52. var _ = framework.KubeDescribe("PetSet [Slow] [Feature:PetSet]", func() {
  53. f := framework.NewDefaultFramework("petset")
  54. var ns string
  55. var c *client.Client
  56. BeforeEach(func() {
  57. // PetSet is in alpha, so it's disabled on all platforms except GCE.
  58. // In theory, tests that restart pets should pass on any platform with a
  59. // dynamic volume provisioner.
  60. framework.SkipUnlessProviderIs("gce")
  61. c = f.Client
  62. ns = f.Namespace.Name
  63. })
  64. framework.KubeDescribe("Basic PetSet functionality", func() {
  65. psName := "pet"
  66. labels := map[string]string{
  67. "foo": "bar",
  68. "baz": "blah",
  69. }
  70. headlessSvcName := "test"
  71. BeforeEach(func() {
  72. By("creating service " + headlessSvcName + " in namespace " + ns)
  73. headlessService := createServiceSpec(headlessSvcName, "", true, labels)
  74. _, err := c.Services(ns).Create(headlessService)
  75. Expect(err).NotTo(HaveOccurred())
  76. })
  77. AfterEach(func() {
  78. if CurrentGinkgoTestDescription().Failed {
  79. dumpDebugInfo(c, ns)
  80. }
  81. framework.Logf("Deleting all petset in ns %v", ns)
  82. deleteAllPetSets(c, ns)
  83. })
  84. It("should provide basic identity [Feature:PetSet]", func() {
  85. By("creating petset " + psName + " in namespace " + ns)
  86. petMounts := []api.VolumeMount{{Name: "datadir", MountPath: "/data/"}}
  87. podMounts := []api.VolumeMount{{Name: "home", MountPath: "/home"}}
  88. ps := newPetSet(psName, ns, headlessSvcName, 3, petMounts, podMounts, labels)
  89. _, err := c.Apps().PetSets(ns).Create(ps)
  90. Expect(err).NotTo(HaveOccurred())
  91. pst := petSetTester{c: c}
  92. By("Saturating pet set " + ps.Name)
  93. pst.saturate(ps)
  94. By("Verifying petset mounted data directory is usable")
  95. ExpectNoError(pst.checkMount(ps, "/data"))
  96. cmd := "echo $(hostname) > /data/hostname; sync;"
  97. By("Running " + cmd + " in all pets")
  98. ExpectNoError(pst.execInPets(ps, cmd))
  99. By("Restarting pet set " + ps.Name)
  100. pst.restart(ps)
  101. pst.saturate(ps)
  102. By("Verifying petset mounted data directory is usable")
  103. ExpectNoError(pst.checkMount(ps, "/data"))
  104. cmd = "if [ \"$(cat /data/hostname)\" = \"$(hostname)\" ]; then exit 0; else exit 1; fi"
  105. By("Running " + cmd + " in all pets")
  106. ExpectNoError(pst.execInPets(ps, cmd))
  107. })
  108. It("should handle healthy pet restarts during scale [Feature:PetSet]", func() {
  109. By("creating petset " + psName + " in namespace " + ns)
  110. petMounts := []api.VolumeMount{{Name: "datadir", MountPath: "/data/"}}
  111. podMounts := []api.VolumeMount{{Name: "home", MountPath: "/home"}}
  112. ps := newPetSet(psName, ns, headlessSvcName, 2, petMounts, podMounts, labels)
  113. _, err := c.Apps().PetSets(ns).Create(ps)
  114. Expect(err).NotTo(HaveOccurred())
  115. pst := petSetTester{c: c}
  116. pst.waitForRunning(1, ps)
  117. By("Marking pet at index 0 as healthy.")
  118. pst.setHealthy(ps)
  119. By("Waiting for pet at index 1 to enter running.")
  120. pst.waitForRunning(2, ps)
  121. // Now we have 1 healthy and 1 unhealthy pet. Deleting the healthy pet should *not*
  122. // create a new pet till the remaining pet becomes healthy, which won't happen till
  123. // we set the healthy bit.
  124. By("Deleting healthy pet at index 0.")
  125. pst.deletePetAtIndex(0, ps)
  126. By("Confirming pet at index 0 is not recreated.")
  127. pst.confirmPetCount(1, ps, 10*time.Second)
  128. By("Deleting unhealthy pet at index 1.")
  129. pst.deletePetAtIndex(1, ps)
  130. By("Confirming all pets in petset are created.")
  131. pst.saturate(ps)
  132. })
  133. })
  134. framework.KubeDescribe("Deploy clustered applications [Slow] [Feature:PetSet]", func() {
  135. BeforeEach(func() {
  136. framework.SkipUnlessProviderIs("gce")
  137. })
  138. AfterEach(func() {
  139. if CurrentGinkgoTestDescription().Failed {
  140. dumpDebugInfo(c, ns)
  141. }
  142. framework.Logf("Deleting all petset in ns %v", ns)
  143. deleteAllPetSets(c, ns)
  144. })
  145. It("should creating a working zookeeper cluster [Feature:PetSet]", func() {
  146. pst := &petSetTester{c: c}
  147. pet := &zookeeperTester{tester: pst}
  148. By("Deploying " + pet.name())
  149. ps := pet.deploy(ns)
  150. By("Creating foo:bar in member with index 0")
  151. pet.write(0, map[string]string{"foo": "bar"})
  152. if restartCluster {
  153. By("Restarting pet set " + ps.Name)
  154. pst.restart(ps)
  155. pst.waitForRunning(ps.Spec.Replicas, ps)
  156. }
  157. By("Reading value under foo from member with index 2")
  158. if v := pet.read(2, "foo"); v != "bar" {
  159. framework.Failf("Read unexpected value %v, expected bar under key foo", v)
  160. }
  161. })
  162. It("should creating a working redis cluster [Feature:PetSet]", func() {
  163. pst := &petSetTester{c: c}
  164. pet := &redisTester{tester: pst}
  165. By("Deploying " + pet.name())
  166. ps := pet.deploy(ns)
  167. By("Creating foo:bar in member with index 0")
  168. pet.write(0, map[string]string{"foo": "bar"})
  169. if restartCluster {
  170. By("Restarting pet set " + ps.Name)
  171. pst.restart(ps)
  172. pst.waitForRunning(ps.Spec.Replicas, ps)
  173. }
  174. By("Reading value under foo from member with index 2")
  175. if v := pet.read(2, "foo"); v != "bar" {
  176. framework.Failf("Read unexpected value %v, expected bar under key foo", v)
  177. }
  178. })
  179. It("should creating a working mysql cluster [Feature:PetSet]", func() {
  180. pst := &petSetTester{c: c}
  181. pet := &mysqlGaleraTester{tester: pst}
  182. By("Deploying " + pet.name())
  183. ps := pet.deploy(ns)
  184. By("Creating foo:bar in member with index 0")
  185. pet.write(0, map[string]string{"foo": "bar"})
  186. if restartCluster {
  187. By("Restarting pet set " + ps.Name)
  188. pst.restart(ps)
  189. pst.waitForRunning(ps.Spec.Replicas, ps)
  190. }
  191. By("Reading value under foo from member with index 2")
  192. if v := pet.read(2, "foo"); v != "bar" {
  193. framework.Failf("Read unexpected value %v, expected bar under key foo", v)
  194. }
  195. })
  196. })
  197. })
  198. func dumpDebugInfo(c *client.Client, ns string) {
  199. pl, _ := c.Pods(ns).List(api.ListOptions{LabelSelector: labels.Everything()})
  200. for _, p := range pl.Items {
  201. desc, _ := framework.RunKubectl("describe", "po", p.Name, fmt.Sprintf("--namespace=%v", ns))
  202. framework.Logf("\nOutput of kubectl describe %v:\n%v", p.Name, desc)
  203. l, _ := framework.RunKubectl("logs", p.Name, fmt.Sprintf("--namespace=%v", ns), "--tail=100")
  204. framework.Logf("\nLast 100 log lines of %v:\n%v", p.Name, l)
  205. }
  206. }
  207. func kubectlExecWithRetries(args ...string) (out string) {
  208. var err error
  209. for i := 0; i < 3; i++ {
  210. if out, err = framework.RunKubectl(args...); err == nil {
  211. return
  212. }
  213. framework.Logf("Retrying %v:\nerror %v\nstdout %v", args, err, out)
  214. }
  215. framework.Failf("Failed to execute \"%v\" with retries: %v", args, err)
  216. return
  217. }
  218. type petTester interface {
  219. deploy(ns string) *apps.PetSet
  220. write(petIndex int, kv map[string]string)
  221. read(petIndex int, key string) string
  222. name() string
  223. }
  224. type zookeeperTester struct {
  225. ps *apps.PetSet
  226. tester *petSetTester
  227. }
  228. func (z *zookeeperTester) name() string {
  229. return "zookeeper"
  230. }
  231. func (z *zookeeperTester) deploy(ns string) *apps.PetSet {
  232. z.ps = z.tester.createPetSet(zookeeperManifestPath, ns)
  233. return z.ps
  234. }
  235. func (z *zookeeperTester) write(petIndex int, kv map[string]string) {
  236. name := fmt.Sprintf("%v-%d", z.ps.Name, petIndex)
  237. ns := fmt.Sprintf("--namespace=%v", z.ps.Namespace)
  238. for k, v := range kv {
  239. cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh create /%v %v", k, v)
  240. framework.Logf(framework.RunKubectlOrDie("exec", ns, name, "--", "/bin/sh", "-c", cmd))
  241. }
  242. }
  243. func (z *zookeeperTester) read(petIndex int, key string) string {
  244. name := fmt.Sprintf("%v-%d", z.ps.Name, petIndex)
  245. ns := fmt.Sprintf("--namespace=%v", z.ps.Namespace)
  246. cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh get /%v", key)
  247. return lastLine(framework.RunKubectlOrDie("exec", ns, name, "--", "/bin/sh", "-c", cmd))
  248. }
  249. type mysqlGaleraTester struct {
  250. ps *apps.PetSet
  251. tester *petSetTester
  252. }
  253. func (m *mysqlGaleraTester) name() string {
  254. return "mysql: galera"
  255. }
  256. func (m *mysqlGaleraTester) mysqlExec(cmd, ns, podName string) string {
  257. cmd = fmt.Sprintf("/usr/bin/mysql -u root -B -e '%v'", cmd)
  258. // TODO: Find a readiness probe for mysql that guarantees writes will
  259. // succeed and ditch retries. Current probe only reads, so there's a window
  260. // for a race.
  261. return kubectlExecWithRetries(fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
  262. }
  263. func (m *mysqlGaleraTester) deploy(ns string) *apps.PetSet {
  264. m.ps = m.tester.createPetSet(mysqlGaleraManifestPath, ns)
  265. framework.Logf("Deployed petset %v, initializing database", m.ps.Name)
  266. for _, cmd := range []string{
  267. "create database petset;",
  268. "use petset; create table pet (k varchar(20), v varchar(20));",
  269. } {
  270. framework.Logf(m.mysqlExec(cmd, ns, fmt.Sprintf("%v-0", m.ps.Name)))
  271. }
  272. return m.ps
  273. }
  274. func (m *mysqlGaleraTester) write(petIndex int, kv map[string]string) {
  275. name := fmt.Sprintf("%v-%d", m.ps.Name, petIndex)
  276. for k, v := range kv {
  277. cmd := fmt.Sprintf("use petset; insert into pet (k, v) values (\"%v\", \"%v\");", k, v)
  278. framework.Logf(m.mysqlExec(cmd, m.ps.Namespace, name))
  279. }
  280. }
  281. func (m *mysqlGaleraTester) read(petIndex int, key string) string {
  282. name := fmt.Sprintf("%v-%d", m.ps.Name, petIndex)
  283. return lastLine(m.mysqlExec(fmt.Sprintf("use petset; select v from pet where k=\"%v\";", key), m.ps.Namespace, name))
  284. }
  285. type redisTester struct {
  286. ps *apps.PetSet
  287. tester *petSetTester
  288. }
  289. func (m *redisTester) name() string {
  290. return "redis: master/slave"
  291. }
  292. func (m *redisTester) redisExec(cmd, ns, podName string) string {
  293. cmd = fmt.Sprintf("/opt/redis/redis-cli -h %v %v", podName, cmd)
  294. return framework.RunKubectlOrDie(fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
  295. }
  296. func (m *redisTester) deploy(ns string) *apps.PetSet {
  297. m.ps = m.tester.createPetSet(redisManifestPath, ns)
  298. return m.ps
  299. }
  300. func (m *redisTester) write(petIndex int, kv map[string]string) {
  301. name := fmt.Sprintf("%v-%d", m.ps.Name, petIndex)
  302. for k, v := range kv {
  303. framework.Logf(m.redisExec(fmt.Sprintf("SET %v %v", k, v), m.ps.Namespace, name))
  304. }
  305. }
  306. func (m *redisTester) read(petIndex int, key string) string {
  307. name := fmt.Sprintf("%v-%d", m.ps.Name, petIndex)
  308. return lastLine(m.redisExec(fmt.Sprintf("GET %v", key), m.ps.Namespace, name))
  309. }
  310. func lastLine(out string) string {
  311. outLines := strings.Split(strings.Trim(out, "\n"), "\n")
  312. return outLines[len(outLines)-1]
  313. }
  314. func petSetFromManifest(fileName, ns string) *apps.PetSet {
  315. var ps apps.PetSet
  316. framework.Logf("Parsing petset from %v", fileName)
  317. data, err := ioutil.ReadFile(fileName)
  318. Expect(err).NotTo(HaveOccurred())
  319. json, err := utilyaml.ToJSON(data)
  320. Expect(err).NotTo(HaveOccurred())
  321. Expect(runtime.DecodeInto(api.Codecs.UniversalDecoder(), json, &ps)).NotTo(HaveOccurred())
  322. ps.Namespace = ns
  323. if ps.Spec.Selector == nil {
  324. ps.Spec.Selector = &unversioned.LabelSelector{
  325. MatchLabels: ps.Spec.Template.Labels,
  326. }
  327. }
  328. return &ps
  329. }
  330. // petSetTester has all methods required to test a single petset.
  331. type petSetTester struct {
  332. c *client.Client
  333. }
  334. func (p *petSetTester) createPetSet(manifestPath, ns string) *apps.PetSet {
  335. mkpath := func(file string) string {
  336. return filepath.Join(framework.TestContext.RepoRoot, manifestPath, file)
  337. }
  338. ps := petSetFromManifest(mkpath("petset.yaml"), ns)
  339. framework.Logf(fmt.Sprintf("creating " + ps.Name + " service"))
  340. framework.RunKubectlOrDie("create", "-f", mkpath("service.yaml"), fmt.Sprintf("--namespace=%v", ns))
  341. framework.Logf(fmt.Sprintf("creating petset %v/%v with %d replicas and selector %+v", ps.Namespace, ps.Name, ps.Spec.Replicas, ps.Spec.Selector))
  342. framework.RunKubectlOrDie("create", "-f", mkpath("petset.yaml"), fmt.Sprintf("--namespace=%v", ns))
  343. p.waitForRunning(ps.Spec.Replicas, ps)
  344. return ps
  345. }
  346. func (p *petSetTester) checkMount(ps *apps.PetSet, mountPath string) error {
  347. for _, cmd := range []string{
  348. // Print inode, size etc
  349. fmt.Sprintf("ls -idlh %v", mountPath),
  350. // Print subdirs
  351. fmt.Sprintf("find %v", mountPath),
  352. // Try writing
  353. fmt.Sprintf("touch %v", filepath.Join(mountPath, fmt.Sprintf("%v", time.Now().UnixNano()))),
  354. } {
  355. if err := p.execInPets(ps, cmd); err != nil {
  356. return fmt.Errorf("failed to execute %v, error: %v", cmd, err)
  357. }
  358. }
  359. return nil
  360. }
  361. func (p *petSetTester) execInPets(ps *apps.PetSet, cmd string) error {
  362. podList := p.getPodList(ps)
  363. for _, pet := range podList.Items {
  364. stdout, err := framework.RunHostCmd(pet.Namespace, pet.Name, cmd)
  365. framework.Logf("stdout of %v on %v: %v", cmd, pet.Name, stdout)
  366. if err != nil {
  367. return err
  368. }
  369. }
  370. return nil
  371. }
  372. func (p *petSetTester) saturate(ps *apps.PetSet) {
  373. // TODO: Watch events and check that creation timestamps don't overlap
  374. for i := 0; i < ps.Spec.Replicas; i++ {
  375. framework.Logf("Waiting for pet at index " + fmt.Sprintf("%v", i+1) + " to enter Running")
  376. p.waitForRunning(i+1, ps)
  377. framework.Logf("Marking pet at index " + fmt.Sprintf("%v", i) + " healthy")
  378. p.setHealthy(ps)
  379. }
  380. }
  381. func (p *petSetTester) deletePetAtIndex(index int, ps *apps.PetSet) {
  382. // TODO: we won't use "-index" as the name strategy forever,
  383. // pull the name out from an identity mapper.
  384. name := fmt.Sprintf("%v-%v", ps.Name, index)
  385. noGrace := int64(0)
  386. if err := p.c.Pods(ps.Namespace).Delete(name, &api.DeleteOptions{GracePeriodSeconds: &noGrace}); err != nil {
  387. framework.Failf("Failed to delete pet %v for PetSet %v: %v", name, ps.Name, ps.Namespace, err)
  388. }
  389. }
  390. func (p *petSetTester) scale(ps *apps.PetSet, count int) error {
  391. name := ps.Name
  392. ns := ps.Namespace
  393. p.update(ns, name, func(ps *apps.PetSet) { ps.Spec.Replicas = count })
  394. var petList *api.PodList
  395. pollErr := wait.PollImmediate(petsetPoll, petsetTimeout, func() (bool, error) {
  396. petList = p.getPodList(ps)
  397. if len(petList.Items) == count {
  398. return true, nil
  399. }
  400. return false, nil
  401. })
  402. if pollErr != nil {
  403. unhealthy := []string{}
  404. for _, pet := range petList.Items {
  405. delTs, phase, readiness := pet.DeletionTimestamp, pet.Status.Phase, api.IsPodReady(&pet)
  406. if delTs != nil || phase != api.PodRunning || !readiness {
  407. unhealthy = append(unhealthy, fmt.Sprintf("%v: deletion %v, phase %v, readiness %v", pet.Name, delTs, phase, readiness))
  408. }
  409. }
  410. return fmt.Errorf("Failed to scale petset to %d in %v. Remaining pods:\n%v", count, petsetTimeout, unhealthy)
  411. }
  412. return nil
  413. }
  414. func (p *petSetTester) restart(ps *apps.PetSet) {
  415. oldReplicas := ps.Spec.Replicas
  416. ExpectNoError(p.scale(ps, 0))
  417. p.update(ps.Namespace, ps.Name, func(ps *apps.PetSet) { ps.Spec.Replicas = oldReplicas })
  418. }
  419. func (p *petSetTester) update(ns, name string, update func(ps *apps.PetSet)) {
  420. for i := 0; i < 3; i++ {
  421. ps, err := p.c.Apps().PetSets(ns).Get(name)
  422. if err != nil {
  423. framework.Failf("failed to get petset %q: %v", name, err)
  424. }
  425. update(ps)
  426. ps, err = p.c.Apps().PetSets(ns).Update(ps)
  427. if err == nil {
  428. return
  429. }
  430. if !apierrs.IsConflict(err) && !apierrs.IsServerTimeout(err) {
  431. framework.Failf("failed to update petset %q: %v", name, err)
  432. }
  433. }
  434. framework.Failf("too many retries draining petset %q", name)
  435. }
  436. func (p *petSetTester) getPodList(ps *apps.PetSet) *api.PodList {
  437. selector, err := unversioned.LabelSelectorAsSelector(ps.Spec.Selector)
  438. ExpectNoError(err)
  439. podList, err := p.c.Pods(ps.Namespace).List(api.ListOptions{LabelSelector: selector})
  440. ExpectNoError(err)
  441. return podList
  442. }
  443. func (p *petSetTester) confirmPetCount(count int, ps *apps.PetSet, timeout time.Duration) {
  444. start := time.Now()
  445. deadline := start.Add(timeout)
  446. for t := time.Now(); t.Before(deadline); t = time.Now() {
  447. podList := p.getPodList(ps)
  448. petCount := len(podList.Items)
  449. if petCount != count {
  450. framework.Failf("PetSet %v scaled unexpectedly scaled to %d -> %d replicas: %+v", ps.Name, count, len(podList.Items), podList)
  451. }
  452. framework.Logf("Verifying petset %v doesn't scale past %d for another %+v", ps.Name, count, deadline.Sub(t))
  453. time.Sleep(1 * time.Second)
  454. }
  455. }
  456. func (p *petSetTester) waitForRunning(numPets int, ps *apps.PetSet) {
  457. pollErr := wait.PollImmediate(petsetPoll, petsetTimeout,
  458. func() (bool, error) {
  459. podList := p.getPodList(ps)
  460. if len(podList.Items) < numPets {
  461. framework.Logf("Found %d pets, waiting for %d", len(podList.Items), numPets)
  462. return false, nil
  463. }
  464. if len(podList.Items) > numPets {
  465. return false, fmt.Errorf("Too many pods scheduled, expected %d got %d", numPets, len(podList.Items))
  466. }
  467. for _, p := range podList.Items {
  468. isReady := api.IsPodReady(&p)
  469. if p.Status.Phase != api.PodRunning || !isReady {
  470. framework.Logf("Waiting for pod %v to enter %v - Ready=True, currently %v - Ready=%v", p.Name, api.PodRunning, p.Status.Phase, isReady)
  471. return false, nil
  472. }
  473. }
  474. return true, nil
  475. })
  476. if pollErr != nil {
  477. framework.Failf("Failed waiting for pods to enter running: %v", pollErr)
  478. }
  479. }
  480. func (p *petSetTester) setHealthy(ps *apps.PetSet) {
  481. podList := p.getPodList(ps)
  482. markedHealthyPod := ""
  483. for _, pod := range podList.Items {
  484. if pod.Status.Phase != api.PodRunning {
  485. framework.Failf("Found pod in %v cannot set health", pod.Status.Phase)
  486. }
  487. if isInitialized(pod) {
  488. continue
  489. }
  490. if markedHealthyPod != "" {
  491. framework.Failf("Found multiple non-healthy pets: %v and %v", pod.Name, markedHealthyPod)
  492. }
  493. p, err := framework.UpdatePodWithRetries(p.c, pod.Namespace, pod.Name, func(up *api.Pod) {
  494. up.Annotations[petset.PetSetInitAnnotation] = "true"
  495. })
  496. ExpectNoError(err)
  497. framework.Logf("Set annotation %v to %v on pod %v", petset.PetSetInitAnnotation, p.Annotations[petset.PetSetInitAnnotation], pod.Name)
  498. markedHealthyPod = pod.Name
  499. }
  500. }
  501. func deleteAllPetSets(c *client.Client, ns string) {
  502. pst := &petSetTester{c: c}
  503. psList, err := c.Apps().PetSets(ns).List(api.ListOptions{LabelSelector: labels.Everything()})
  504. ExpectNoError(err)
  505. // Scale down each petset, then delete it completely.
  506. // Deleting a pvc without doing this will leak volumes, #25101.
  507. errList := []string{}
  508. for _, ps := range psList.Items {
  509. framework.Logf("Scaling petset %v to 0", ps.Name)
  510. if err := pst.scale(&ps, 0); err != nil {
  511. errList = append(errList, fmt.Sprintf("%v", err))
  512. }
  513. framework.Logf("Deleting petset %v", ps.Name)
  514. if err := c.Apps().PetSets(ps.Namespace).Delete(ps.Name, nil); err != nil {
  515. errList = append(errList, fmt.Sprintf("%v", err))
  516. }
  517. }
  518. // pvs are global, so we need to wait for the exact ones bound to the petset pvcs.
  519. pvNames := sets.NewString()
  520. // TODO: Don't assume all pvcs in the ns belong to a petset
  521. pvcPollErr := wait.PollImmediate(petsetPoll, petsetTimeout, func() (bool, error) {
  522. pvcList, err := c.PersistentVolumeClaims(ns).List(api.ListOptions{LabelSelector: labels.Everything()})
  523. if err != nil {
  524. framework.Logf("WARNING: Failed to list pvcs, retrying %v", err)
  525. return false, nil
  526. }
  527. for _, pvc := range pvcList.Items {
  528. pvNames.Insert(pvc.Spec.VolumeName)
  529. // TODO: Double check that there are no pods referencing the pvc
  530. framework.Logf("Deleting pvc: %v with volume %v", pvc.Name, pvc.Spec.VolumeName)
  531. if err := c.PersistentVolumeClaims(ns).Delete(pvc.Name); err != nil {
  532. return false, nil
  533. }
  534. }
  535. return true, nil
  536. })
  537. if pvcPollErr != nil {
  538. errList = append(errList, fmt.Sprintf("Timeout waiting for pvc deletion."))
  539. }
  540. pollErr := wait.PollImmediate(petsetPoll, petsetTimeout, func() (bool, error) {
  541. pvList, err := c.PersistentVolumes().List(api.ListOptions{LabelSelector: labels.Everything()})
  542. if err != nil {
  543. framework.Logf("WARNING: Failed to list pvs, retrying %v", err)
  544. return false, nil
  545. }
  546. waitingFor := []string{}
  547. for _, pv := range pvList.Items {
  548. if pvNames.Has(pv.Name) {
  549. waitingFor = append(waitingFor, fmt.Sprintf("%v: %+v", pv.Name, pv.Status))
  550. }
  551. }
  552. if len(waitingFor) == 0 {
  553. return true, nil
  554. }
  555. framework.Logf("Still waiting for pvs of petset to disappear:\n%v", strings.Join(waitingFor, "\n"))
  556. return false, nil
  557. })
  558. if pollErr != nil {
  559. errList = append(errList, fmt.Sprintf("Timeout waiting for pv provisioner to delete pvs, this might mean the test leaked pvs."))
  560. }
  561. if len(errList) != 0 {
  562. ExpectNoError(fmt.Errorf("%v", strings.Join(errList, "\n")))
  563. }
  564. }
  565. func ExpectNoError(err error) {
  566. Expect(err).NotTo(HaveOccurred())
  567. }
  568. func isInitialized(pod api.Pod) bool {
  569. initialized, ok := pod.Annotations[petset.PetSetInitAnnotation]
  570. if !ok {
  571. return false
  572. }
  573. inited, err := strconv.ParseBool(initialized)
  574. if err != nil {
  575. framework.Failf("Couldn't parse petset init annotations %v", initialized)
  576. }
  577. return inited
  578. }
  579. func dec(i int64, exponent int) *inf.Dec {
  580. return inf.NewDec(i, inf.Scale(-exponent))
  581. }
  582. func newPVC(name string) api.PersistentVolumeClaim {
  583. return api.PersistentVolumeClaim{
  584. ObjectMeta: api.ObjectMeta{
  585. Name: name,
  586. Annotations: map[string]string{
  587. "volume.alpha.kubernetes.io/storage-class": "anything",
  588. },
  589. },
  590. Spec: api.PersistentVolumeClaimSpec{
  591. AccessModes: []api.PersistentVolumeAccessMode{
  592. api.ReadWriteOnce,
  593. },
  594. Resources: api.ResourceRequirements{
  595. Requests: api.ResourceList{
  596. api.ResourceStorage: *resource.NewQuantity(1, resource.BinarySI),
  597. },
  598. },
  599. },
  600. }
  601. }
  602. func newPetSet(name, ns, governingSvcName string, replicas int, petMounts []api.VolumeMount, podMounts []api.VolumeMount, labels map[string]string) *apps.PetSet {
  603. mounts := append(petMounts, podMounts...)
  604. claims := []api.PersistentVolumeClaim{}
  605. for _, m := range petMounts {
  606. claims = append(claims, newPVC(m.Name))
  607. }
  608. vols := []api.Volume{}
  609. for _, m := range podMounts {
  610. vols = append(vols, api.Volume{
  611. Name: m.Name,
  612. VolumeSource: api.VolumeSource{
  613. HostPath: &api.HostPathVolumeSource{
  614. Path: fmt.Sprintf("/tmp/%v", m.Name),
  615. },
  616. },
  617. })
  618. }
  619. return &apps.PetSet{
  620. TypeMeta: unversioned.TypeMeta{
  621. Kind: "PetSet",
  622. APIVersion: "apps/v1beta1",
  623. },
  624. ObjectMeta: api.ObjectMeta{
  625. Name: name,
  626. Namespace: ns,
  627. },
  628. Spec: apps.PetSetSpec{
  629. Selector: &unversioned.LabelSelector{
  630. MatchLabels: labels,
  631. },
  632. Replicas: replicas,
  633. Template: api.PodTemplateSpec{
  634. ObjectMeta: api.ObjectMeta{
  635. Labels: labels,
  636. },
  637. Spec: api.PodSpec{
  638. Containers: []api.Container{
  639. {
  640. Name: "nginx",
  641. Image: "gcr.io/google_containers/nginx-slim:0.7",
  642. VolumeMounts: mounts,
  643. },
  644. },
  645. Volumes: vols,
  646. },
  647. },
  648. VolumeClaimTemplates: claims,
  649. ServiceName: governingSvcName,
  650. },
  651. }
  652. }