generated_clientset.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 apimachinery
  14. import (
  15. "context"
  16. "strconv"
  17. "time"
  18. batchv1 "k8s.io/api/batch/v1"
  19. batchv1beta1 "k8s.io/api/batch/v1beta1"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/util/intstr"
  25. "k8s.io/apimachinery/pkg/util/uuid"
  26. "k8s.io/apimachinery/pkg/watch"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  29. "github.com/onsi/ginkgo"
  30. imageutils "k8s.io/kubernetes/test/utils/image"
  31. )
  32. func testingPod(name, value string) v1.Pod {
  33. return v1.Pod{
  34. ObjectMeta: metav1.ObjectMeta{
  35. Name: name,
  36. Labels: map[string]string{
  37. "name": "foo",
  38. "time": value,
  39. },
  40. },
  41. Spec: v1.PodSpec{
  42. Containers: []v1.Container{
  43. {
  44. Name: "nginx",
  45. Image: imageutils.GetE2EImage(imageutils.Nginx),
  46. Ports: []v1.ContainerPort{{ContainerPort: 80}},
  47. LivenessProbe: &v1.Probe{
  48. Handler: v1.Handler{
  49. HTTPGet: &v1.HTTPGetAction{
  50. Path: "/index.html",
  51. Port: intstr.FromInt(8080),
  52. },
  53. },
  54. InitialDelaySeconds: 30,
  55. },
  56. },
  57. },
  58. },
  59. }
  60. }
  61. func observeCreation(w watch.Interface) {
  62. select {
  63. case event := <-w.ResultChan():
  64. if event.Type != watch.Added {
  65. framework.Failf("Failed to observe the creation: %v", event)
  66. }
  67. case <-time.After(30 * time.Second):
  68. framework.Failf("Timeout while waiting for observing the creation")
  69. }
  70. }
  71. func observerUpdate(w watch.Interface, expectedUpdate func(runtime.Object) bool) {
  72. timer := time.After(30 * time.Second)
  73. updated := false
  74. timeout := false
  75. for !updated && !timeout {
  76. select {
  77. case event := <-w.ResultChan():
  78. if event.Type == watch.Modified {
  79. if expectedUpdate(event.Object) {
  80. updated = true
  81. }
  82. }
  83. case <-timer:
  84. timeout = true
  85. }
  86. }
  87. if !updated {
  88. framework.Failf("Failed to observe pod update")
  89. }
  90. }
  91. var _ = SIGDescribe("Generated clientset", func() {
  92. f := framework.NewDefaultFramework("clientset")
  93. ginkgo.It("should create pods, set the deletionTimestamp and deletionGracePeriodSeconds of the pod", func() {
  94. podClient := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
  95. ginkgo.By("constructing the pod")
  96. name := "pod" + string(uuid.NewUUID())
  97. value := strconv.Itoa(time.Now().Nanosecond())
  98. podCopy := testingPod(name, value)
  99. pod := &podCopy
  100. ginkgo.By("setting up watch")
  101. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
  102. options := metav1.ListOptions{LabelSelector: selector}
  103. pods, err := podClient.List(context.TODO(), options)
  104. if err != nil {
  105. framework.Failf("Failed to query for pods: %v", err)
  106. }
  107. framework.ExpectEqual(len(pods.Items), 0)
  108. options = metav1.ListOptions{
  109. LabelSelector: selector,
  110. ResourceVersion: pods.ListMeta.ResourceVersion,
  111. }
  112. w, err := podClient.Watch(context.TODO(), options)
  113. if err != nil {
  114. framework.Failf("Failed to set up watch: %v", err)
  115. }
  116. ginkgo.By("creating the pod")
  117. pod, err = podClient.Create(context.TODO(), pod, metav1.CreateOptions{})
  118. if err != nil {
  119. framework.Failf("Failed to create pod: %v", err)
  120. }
  121. ginkgo.By("verifying the pod is in kubernetes")
  122. options = metav1.ListOptions{
  123. LabelSelector: selector,
  124. ResourceVersion: pod.ResourceVersion,
  125. }
  126. pods, err = podClient.List(context.TODO(), options)
  127. if err != nil {
  128. framework.Failf("Failed to query for pods: %v", err)
  129. }
  130. framework.ExpectEqual(len(pods.Items), 1)
  131. ginkgo.By("verifying pod creation was observed")
  132. observeCreation(w)
  133. // We need to wait for the pod to be scheduled, otherwise the deletion
  134. // will be carried out immediately rather than gracefully.
  135. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  136. ginkgo.By("deleting the pod gracefully")
  137. gracePeriod := int64(31)
  138. if err := podClient.Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(gracePeriod)); err != nil {
  139. framework.Failf("Failed to delete pod: %v", err)
  140. }
  141. ginkgo.By("verifying the deletionTimestamp and deletionGracePeriodSeconds of the pod is set")
  142. observerUpdate(w, func(obj runtime.Object) bool {
  143. pod := obj.(*v1.Pod)
  144. return pod.ObjectMeta.DeletionTimestamp != nil && *pod.ObjectMeta.DeletionGracePeriodSeconds == gracePeriod
  145. })
  146. })
  147. })
  148. func newTestingCronJob(name string, value string) *batchv1beta1.CronJob {
  149. parallelism := int32(1)
  150. completions := int32(1)
  151. return &batchv1beta1.CronJob{
  152. ObjectMeta: metav1.ObjectMeta{
  153. Name: name,
  154. Labels: map[string]string{
  155. "time": value,
  156. },
  157. },
  158. Spec: batchv1beta1.CronJobSpec{
  159. Schedule: "*/1 * * * ?",
  160. ConcurrencyPolicy: batchv1beta1.AllowConcurrent,
  161. JobTemplate: batchv1beta1.JobTemplateSpec{
  162. Spec: batchv1.JobSpec{
  163. Parallelism: &parallelism,
  164. Completions: &completions,
  165. Template: v1.PodTemplateSpec{
  166. Spec: v1.PodSpec{
  167. RestartPolicy: v1.RestartPolicyOnFailure,
  168. Volumes: []v1.Volume{
  169. {
  170. Name: "data",
  171. VolumeSource: v1.VolumeSource{
  172. EmptyDir: &v1.EmptyDirVolumeSource{},
  173. },
  174. },
  175. },
  176. Containers: []v1.Container{
  177. {
  178. Name: "c",
  179. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  180. VolumeMounts: []v1.VolumeMount{
  181. {
  182. MountPath: "/data",
  183. Name: "data",
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. },
  192. },
  193. }
  194. }
  195. var _ = SIGDescribe("Generated clientset", func() {
  196. f := framework.NewDefaultFramework("clientset")
  197. ginkgo.BeforeEach(func() {
  198. e2eskipper.SkipIfMissingResource(f.DynamicClient, CronJobGroupVersionResource, f.Namespace.Name)
  199. })
  200. ginkgo.It("should create v1beta1 cronJobs, delete cronJobs, watch cronJobs", func() {
  201. cronJobClient := f.ClientSet.BatchV1beta1().CronJobs(f.Namespace.Name)
  202. ginkgo.By("constructing the cronJob")
  203. name := "cronjob" + string(uuid.NewUUID())
  204. value := strconv.Itoa(time.Now().Nanosecond())
  205. cronJob := newTestingCronJob(name, value)
  206. ginkgo.By("setting up watch")
  207. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
  208. options := metav1.ListOptions{LabelSelector: selector}
  209. cronJobs, err := cronJobClient.List(context.TODO(), options)
  210. if err != nil {
  211. framework.Failf("Failed to query for cronJobs: %v", err)
  212. }
  213. framework.ExpectEqual(len(cronJobs.Items), 0)
  214. options = metav1.ListOptions{
  215. LabelSelector: selector,
  216. ResourceVersion: cronJobs.ListMeta.ResourceVersion,
  217. }
  218. w, err := cronJobClient.Watch(context.TODO(), options)
  219. if err != nil {
  220. framework.Failf("Failed to set up watch: %v", err)
  221. }
  222. ginkgo.By("creating the cronJob")
  223. cronJob, err = cronJobClient.Create(context.TODO(), cronJob, metav1.CreateOptions{})
  224. if err != nil {
  225. framework.Failf("Failed to create cronJob: %v", err)
  226. }
  227. ginkgo.By("verifying the cronJob is in kubernetes")
  228. options = metav1.ListOptions{
  229. LabelSelector: selector,
  230. ResourceVersion: cronJob.ResourceVersion,
  231. }
  232. cronJobs, err = cronJobClient.List(context.TODO(), options)
  233. if err != nil {
  234. framework.Failf("Failed to query for cronJobs: %v", err)
  235. }
  236. framework.ExpectEqual(len(cronJobs.Items), 1)
  237. ginkgo.By("verifying cronJob creation was observed")
  238. observeCreation(w)
  239. ginkgo.By("deleting the cronJob")
  240. // Use DeletePropagationBackground so the CronJob is really gone when the call returns.
  241. propagationPolicy := metav1.DeletePropagationBackground
  242. if err := cronJobClient.Delete(context.TODO(), cronJob.Name, &metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}); err != nil {
  243. framework.Failf("Failed to delete cronJob: %v", err)
  244. }
  245. options = metav1.ListOptions{LabelSelector: selector}
  246. cronJobs, err = cronJobClient.List(context.TODO(), options)
  247. if err != nil {
  248. framework.Failf("Failed to list cronJobs to verify deletion: %v", err)
  249. }
  250. framework.ExpectEqual(len(cronJobs.Items), 0)
  251. })
  252. })