pods.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2016 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 framework
  14. import (
  15. "fmt"
  16. "sync"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/errors"
  20. "k8s.io/kubernetes/pkg/client/unversioned"
  21. "k8s.io/kubernetes/pkg/util/wait"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. // Convenience method for getting a pod client interface in the framework's namespace.
  26. func (f *Framework) PodClient() *PodClient {
  27. return &PodClient{
  28. f: f,
  29. PodInterface: f.Client.Pods(f.Namespace.Name),
  30. }
  31. }
  32. type PodClient struct {
  33. f *Framework
  34. unversioned.PodInterface
  35. }
  36. // Create creates a new pod according to the framework specifications (don't wait for it to start).
  37. func (c *PodClient) Create(pod *api.Pod) *api.Pod {
  38. c.mungeSpec(pod)
  39. p, err := c.PodInterface.Create(pod)
  40. ExpectNoError(err, "Error creating Pod")
  41. return p
  42. }
  43. // CreateSync creates a new pod according to the framework specifications, and wait for it to start.
  44. func (c *PodClient) CreateSync(pod *api.Pod) *api.Pod {
  45. p := c.Create(pod)
  46. ExpectNoError(c.f.WaitForPodRunning(p.Name))
  47. // Get the newest pod after it becomes running, some status may change after pod created, such as pod ip.
  48. p, err := c.Get(p.Name)
  49. ExpectNoError(err)
  50. return p
  51. }
  52. // CreateBatch create a batch of pods. All pods are created before waiting.
  53. func (c *PodClient) CreateBatch(pods []*api.Pod) []*api.Pod {
  54. ps := make([]*api.Pod, len(pods))
  55. var wg sync.WaitGroup
  56. for i, pod := range pods {
  57. wg.Add(1)
  58. go func(i int, pod *api.Pod) {
  59. defer wg.Done()
  60. defer GinkgoRecover()
  61. ps[i] = c.CreateSync(pod)
  62. }(i, pod)
  63. }
  64. wg.Wait()
  65. return ps
  66. }
  67. // Update updates the pod object. It retries if there is a conflict, throw out error if
  68. // there is any other errors. name is the pod name, updateFn is the function updating the
  69. // pod object.
  70. func (c *PodClient) Update(name string, updateFn func(pod *api.Pod)) {
  71. ExpectNoError(wait.Poll(time.Millisecond*500, time.Second*30, func() (bool, error) {
  72. pod, err := c.PodInterface.Get(name)
  73. if err != nil {
  74. return false, fmt.Errorf("failed to get pod %q: %v", name, err)
  75. }
  76. updateFn(pod)
  77. _, err = c.PodInterface.Update(pod)
  78. if err == nil {
  79. Logf("Successfully updated pod %q", name)
  80. return true, nil
  81. }
  82. if errors.IsConflict(err) {
  83. Logf("Conflicting update to pod %q, re-get and re-update: %v", name, err)
  84. return false, nil
  85. }
  86. return false, fmt.Errorf("failed to update pod %q: %v", name, err)
  87. }))
  88. }
  89. // mungeSpec apply test-suite specific transformations to the pod spec.
  90. func (c *PodClient) mungeSpec(pod *api.Pod) {
  91. if TestContext.NodeName != "" {
  92. Expect(pod.Spec.NodeName).To(Or(BeZero(), Equal(TestContext.NodeName)), "Test misconfigured")
  93. pod.Spec.NodeName = TestContext.NodeName
  94. }
  95. }
  96. // TODO(random-liu): Move pod wait function into this file