rc.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. Copyright 2015 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. "fmt"
  16. "time"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/util/uuid"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. "k8s.io/kubernetes/pkg/controller/replication"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  27. imageutils "k8s.io/kubernetes/test/utils/image"
  28. "github.com/onsi/ginkgo"
  29. )
  30. var _ = SIGDescribe("ReplicationController", func() {
  31. f := framework.NewDefaultFramework("replication-controller")
  32. /*
  33. Release : v1.9
  34. Testname: Replication Controller, run basic image
  35. Description: Replication Controller MUST create a Pod with Basic Image and MUST run the service with the provided image. Image MUST be tested by dialing into the service listening through TCP, UDP and HTTP.
  36. */
  37. framework.ConformanceIt("should serve a basic image on each replica with a public image ", func() {
  38. TestReplicationControllerServeImageOrFail(f, "basic", framework.ServeHostnameImage)
  39. })
  40. ginkgo.It("should serve a basic image on each replica with a private image", func() {
  41. // requires private images
  42. framework.SkipUnlessProviderIs("gce", "gke")
  43. privateimage := imageutils.GetConfig(imageutils.ServeHostname)
  44. privateimage.SetRegistry(imageutils.PrivateRegistry)
  45. TestReplicationControllerServeImageOrFail(f, "private", privateimage.GetE2EImage())
  46. })
  47. /*
  48. Release : v1.15
  49. Testname: Replication Controller, check for issues like exceeding allocated quota
  50. Description: Attempt to create a Replication Controller with pods exceeding the namespace quota. The creation MUST fail
  51. */
  52. framework.ConformanceIt("should surface a failure condition on a common issue like exceeded quota", func() {
  53. testReplicationControllerConditionCheck(f)
  54. })
  55. /*
  56. Release : v1.13
  57. Testname: Replication Controller, adopt matching pods
  58. Description: An ownerless Pod is created, then a Replication Controller (RC) is created whose label selector will match the Pod. The RC MUST either adopt the Pod or delete and replace it with a new Pod
  59. */
  60. framework.ConformanceIt("should adopt matching pods on creation", func() {
  61. testRCAdoptMatchingOrphans(f)
  62. })
  63. /*
  64. Release : v1.13
  65. Testname: Replication Controller, release pods
  66. Description: A Replication Controller (RC) is created, and its Pods are created. When the labels on one of the Pods change to no longer match the RC's label selector, the RC MUST release the Pod and update the Pod's owner references.
  67. */
  68. framework.ConformanceIt("should release no longer matching pods", func() {
  69. testRCReleaseControlledNotMatching(f)
  70. })
  71. })
  72. func newRC(rsName string, replicas int32, rcPodLabels map[string]string, imageName string, image string) *v1.ReplicationController {
  73. zero := int64(0)
  74. return &v1.ReplicationController{
  75. ObjectMeta: metav1.ObjectMeta{
  76. Name: rsName,
  77. },
  78. Spec: v1.ReplicationControllerSpec{
  79. Replicas: func(i int32) *int32 { return &i }(replicas),
  80. Template: &v1.PodTemplateSpec{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Labels: rcPodLabels,
  83. },
  84. Spec: v1.PodSpec{
  85. TerminationGracePeriodSeconds: &zero,
  86. Containers: []v1.Container{
  87. {
  88. Name: imageName,
  89. Image: image,
  90. },
  91. },
  92. },
  93. },
  94. },
  95. }
  96. }
  97. // TestReplicationControllerServeImageOrFail is a basic test to check
  98. // the deployment of an image using a replication controller.
  99. // The image serves its hostname which is checked for each replica.
  100. func TestReplicationControllerServeImageOrFail(f *framework.Framework, test string, image string) {
  101. name := "my-hostname-" + test + "-" + string(uuid.NewUUID())
  102. replicas := int32(1)
  103. // Create a replication controller for a service
  104. // that serves its hostname.
  105. // The source for the Docker container kubernetes/serve_hostname is
  106. // in contrib/for-demos/serve_hostname
  107. ginkgo.By(fmt.Sprintf("Creating replication controller %s", name))
  108. newRC := newRC(name, replicas, map[string]string{"name": name}, name, image)
  109. newRC.Spec.Template.Spec.Containers[0].Ports = []v1.ContainerPort{{ContainerPort: 9376}}
  110. _, err := f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(newRC)
  111. framework.ExpectNoError(err)
  112. // Check that pods for the new RC were created.
  113. // TODO: Maybe switch PodsCreated to just check owner references.
  114. pods, err := framework.PodsCreated(f.ClientSet, f.Namespace.Name, name, replicas)
  115. framework.ExpectNoError(err)
  116. // Wait for the pods to enter the running state. Waiting loops until the pods
  117. // are running so non-running pods cause a timeout for this test.
  118. e2elog.Logf("Ensuring all pods for ReplicationController %q are running", name)
  119. running := int32(0)
  120. for _, pod := range pods.Items {
  121. if pod.DeletionTimestamp != nil {
  122. continue
  123. }
  124. err = f.WaitForPodRunning(pod.Name)
  125. if err != nil {
  126. updatePod, getErr := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
  127. if getErr == nil {
  128. err = fmt.Errorf("Pod %q never run (phase: %s, conditions: %+v): %v", updatePod.Name, updatePod.Status.Phase, updatePod.Status.Conditions, err)
  129. } else {
  130. err = fmt.Errorf("Pod %q never run: %v", pod.Name, err)
  131. }
  132. }
  133. framework.ExpectNoError(err)
  134. e2elog.Logf("Pod %q is running (conditions: %+v)", pod.Name, pod.Status.Conditions)
  135. running++
  136. }
  137. // Sanity check
  138. if running != replicas {
  139. framework.ExpectNoError(fmt.Errorf("unexpected number of running pods: %+v", pods.Items))
  140. }
  141. // Verify that something is listening.
  142. e2elog.Logf("Trying to dial the pod")
  143. retryTimeout := 2 * time.Minute
  144. retryInterval := 5 * time.Second
  145. label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
  146. err = wait.Poll(retryInterval, retryTimeout, framework.NewPodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
  147. if err != nil {
  148. framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
  149. }
  150. }
  151. // 1. Create a quota restricting pods in the current namespace to 2.
  152. // 2. Create a replication controller that wants to run 3 pods.
  153. // 3. Check replication controller conditions for a ReplicaFailure condition.
  154. // 4. Relax quota or scale down the controller and observe the condition is gone.
  155. func testReplicationControllerConditionCheck(f *framework.Framework) {
  156. c := f.ClientSet
  157. namespace := f.Namespace.Name
  158. name := "condition-test"
  159. e2elog.Logf("Creating quota %q that allows only two pods to run in the current namespace", name)
  160. quota := newPodQuota(name, "2")
  161. _, err := c.CoreV1().ResourceQuotas(namespace).Create(quota)
  162. framework.ExpectNoError(err)
  163. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  164. quota, err = c.CoreV1().ResourceQuotas(namespace).Get(name, metav1.GetOptions{})
  165. if err != nil {
  166. return false, err
  167. }
  168. podQuota := quota.Status.Hard[v1.ResourcePods]
  169. quantity := resource.MustParse("2")
  170. return (&podQuota).Cmp(quantity) == 0, nil
  171. })
  172. if err == wait.ErrWaitTimeout {
  173. err = fmt.Errorf("resource quota %q never synced", name)
  174. }
  175. framework.ExpectNoError(err)
  176. ginkgo.By(fmt.Sprintf("Creating rc %q that asks for more than the allowed pod quota", name))
  177. rc := newRC(name, 3, map[string]string{"name": name}, NginxImageName, NginxImage)
  178. rc, err = c.CoreV1().ReplicationControllers(namespace).Create(rc)
  179. framework.ExpectNoError(err)
  180. ginkgo.By(fmt.Sprintf("Checking rc %q has the desired failure condition set", name))
  181. generation := rc.Generation
  182. conditions := rc.Status.Conditions
  183. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  184. rc, err = c.CoreV1().ReplicationControllers(namespace).Get(name, metav1.GetOptions{})
  185. if err != nil {
  186. return false, err
  187. }
  188. if generation > rc.Status.ObservedGeneration {
  189. return false, nil
  190. }
  191. conditions = rc.Status.Conditions
  192. cond := replication.GetCondition(rc.Status, v1.ReplicationControllerReplicaFailure)
  193. return cond != nil, nil
  194. })
  195. if err == wait.ErrWaitTimeout {
  196. err = fmt.Errorf("rc manager never added the failure condition for rc %q: %#v", name, conditions)
  197. }
  198. framework.ExpectNoError(err)
  199. ginkgo.By(fmt.Sprintf("Scaling down rc %q to satisfy pod quota", name))
  200. rc, err = framework.UpdateReplicationControllerWithRetries(c, namespace, name, func(update *v1.ReplicationController) {
  201. x := int32(2)
  202. update.Spec.Replicas = &x
  203. })
  204. framework.ExpectNoError(err)
  205. ginkgo.By(fmt.Sprintf("Checking rc %q has no failure condition set", name))
  206. generation = rc.Generation
  207. conditions = rc.Status.Conditions
  208. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  209. rc, err = c.CoreV1().ReplicationControllers(namespace).Get(name, metav1.GetOptions{})
  210. if err != nil {
  211. return false, err
  212. }
  213. if generation > rc.Status.ObservedGeneration {
  214. return false, nil
  215. }
  216. conditions = rc.Status.Conditions
  217. cond := replication.GetCondition(rc.Status, v1.ReplicationControllerReplicaFailure)
  218. return cond == nil, nil
  219. })
  220. if err == wait.ErrWaitTimeout {
  221. err = fmt.Errorf("rc manager never removed the failure condition for rc %q: %#v", name, conditions)
  222. }
  223. framework.ExpectNoError(err)
  224. }
  225. func testRCAdoptMatchingOrphans(f *framework.Framework) {
  226. name := "pod-adoption"
  227. ginkgo.By(fmt.Sprintf("Given a Pod with a 'name' label %s is created", name))
  228. p := f.PodClient().CreateSync(&v1.Pod{
  229. ObjectMeta: metav1.ObjectMeta{
  230. Name: name,
  231. Labels: map[string]string{
  232. "name": name,
  233. },
  234. },
  235. Spec: v1.PodSpec{
  236. Containers: []v1.Container{
  237. {
  238. Name: name,
  239. Image: NginxImage,
  240. },
  241. },
  242. },
  243. })
  244. ginkgo.By("When a replication controller with a matching selector is created")
  245. replicas := int32(1)
  246. rcSt := newRC(name, replicas, map[string]string{"name": name}, name, NginxImage)
  247. rcSt.Spec.Selector = map[string]string{"name": name}
  248. rc, err := f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(rcSt)
  249. framework.ExpectNoError(err)
  250. ginkgo.By("Then the orphan pod is adopted")
  251. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  252. p2, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
  253. // The Pod p should either be adopted or deleted by the RC
  254. if errors.IsNotFound(err) {
  255. return true, nil
  256. }
  257. framework.ExpectNoError(err)
  258. for _, owner := range p2.OwnerReferences {
  259. if *owner.Controller && owner.UID == rc.UID {
  260. // pod adopted
  261. return true, nil
  262. }
  263. }
  264. // pod still not adopted
  265. return false, nil
  266. })
  267. framework.ExpectNoError(err)
  268. }
  269. func testRCReleaseControlledNotMatching(f *framework.Framework) {
  270. name := "pod-release"
  271. ginkgo.By("Given a ReplicationController is created")
  272. replicas := int32(1)
  273. rcSt := newRC(name, replicas, map[string]string{"name": name}, name, NginxImage)
  274. rcSt.Spec.Selector = map[string]string{"name": name}
  275. rc, err := f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(rcSt)
  276. framework.ExpectNoError(err)
  277. ginkgo.By("When the matched label of one of its pods change")
  278. pods, err := framework.PodsCreated(f.ClientSet, f.Namespace.Name, rc.Name, replicas)
  279. framework.ExpectNoError(err)
  280. p := pods.Items[0]
  281. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  282. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
  283. framework.ExpectNoError(err)
  284. pod.Labels = map[string]string{"name": "not-matching-name"}
  285. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Update(pod)
  286. if err != nil && errors.IsConflict(err) {
  287. return false, nil
  288. }
  289. if err != nil {
  290. return false, err
  291. }
  292. return true, nil
  293. })
  294. framework.ExpectNoError(err)
  295. ginkgo.By("Then the pod is released")
  296. err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
  297. p2, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
  298. framework.ExpectNoError(err)
  299. for _, owner := range p2.OwnerReferences {
  300. if *owner.Controller && owner.UID == rc.UID {
  301. // pod still belonging to the replication controller
  302. return false, nil
  303. }
  304. }
  305. // pod already released
  306. return true, nil
  307. })
  308. framework.ExpectNoError(err)
  309. }