podpreset.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 servicecatalog
  14. import (
  15. "context"
  16. "reflect"
  17. "strconv"
  18. "time"
  19. "k8s.io/api/core/v1"
  20. settingsv1alpha1 "k8s.io/api/settings/v1alpha1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apimachinery/pkg/watch"
  25. clientset "k8s.io/client-go/kubernetes"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  28. imageutils "k8s.io/kubernetes/test/utils/image"
  29. "github.com/onsi/ginkgo"
  30. )
  31. var _ = SIGDescribe("[Feature:PodPreset] PodPreset", func() {
  32. f := framework.NewDefaultFramework("podpreset")
  33. var podClient *framework.PodClient
  34. ginkgo.BeforeEach(func() {
  35. // only run on gce for the time being til we find an easier way to update
  36. // the admission controllers used on the others
  37. e2eskipper.SkipUnlessProviderIs("gce")
  38. podClient = f.PodClient()
  39. })
  40. // Simplest case: all pods succeed promptly
  41. ginkgo.It("should create a pod preset", func() {
  42. ginkgo.By("Creating a pod preset")
  43. pip := &settingsv1alpha1.PodPreset{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: "hello",
  46. Namespace: f.Namespace.Name,
  47. },
  48. Spec: settingsv1alpha1.PodPresetSpec{
  49. Selector: metav1.LabelSelector{
  50. MatchExpressions: []metav1.LabelSelectorRequirement{
  51. {
  52. Key: "security",
  53. Operator: metav1.LabelSelectorOpIn,
  54. Values: []string{"S2"},
  55. },
  56. },
  57. },
  58. Volumes: []v1.Volume{{Name: "vol", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}},
  59. VolumeMounts: []v1.VolumeMount{
  60. {Name: "vol", MountPath: "/foo"},
  61. },
  62. Env: []v1.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
  63. },
  64. }
  65. _, err := createPodPreset(f.ClientSet, f.Namespace.Name, pip)
  66. if apierrors.IsNotFound(err) {
  67. e2eskipper.Skipf("podpresets requires k8s.io/api/settings/v1alpha1 to be enabled")
  68. }
  69. framework.ExpectNoError(err)
  70. ginkgo.By("creating the pod")
  71. name := "pod-preset-pod"
  72. value := strconv.Itoa(time.Now().Nanosecond())
  73. pod := &v1.Pod{
  74. ObjectMeta: metav1.ObjectMeta{
  75. Name: name,
  76. Namespace: f.Namespace.Name,
  77. Labels: map[string]string{
  78. "name": "foo",
  79. "time": value,
  80. "security": "S2",
  81. },
  82. },
  83. Spec: v1.PodSpec{
  84. Containers: []v1.Container{
  85. {
  86. Name: "nginx",
  87. Image: imageutils.GetE2EImage(imageutils.Nginx),
  88. },
  89. },
  90. InitContainers: []v1.Container{
  91. {
  92. Name: "init1",
  93. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  94. Command: []string{"/bin/true"},
  95. },
  96. },
  97. },
  98. }
  99. ginkgo.By("setting up watch")
  100. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  101. options := metav1.ListOptions{LabelSelector: selector.String()}
  102. pods, err := podClient.List(context.TODO(), options)
  103. framework.ExpectNoError(err, "failed to query for pod")
  104. framework.ExpectEqual(len(pods.Items), 0)
  105. options = metav1.ListOptions{
  106. LabelSelector: selector.String(),
  107. ResourceVersion: pods.ListMeta.ResourceVersion,
  108. }
  109. w, err := podClient.Watch(context.TODO(), options)
  110. framework.ExpectNoError(err, "failed to set up watch")
  111. ginkgo.By("submitting the pod to kubernetes")
  112. podClient.Create(pod)
  113. ginkgo.By("verifying the pod is in kubernetes")
  114. selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  115. options = metav1.ListOptions{LabelSelector: selector.String()}
  116. pods, err = podClient.List(context.TODO(), options)
  117. framework.ExpectNoError(err, "failed to query for pod")
  118. framework.ExpectEqual(len(pods.Items), 1)
  119. ginkgo.By("verifying pod creation was observed")
  120. select {
  121. case event, _ := <-w.ResultChan():
  122. if event.Type != watch.Added {
  123. framework.Failf("Failed to observe pod creation: %v", event)
  124. }
  125. case <-time.After(framework.PodStartTimeout):
  126. framework.Failf("Timeout while waiting for pod creation")
  127. }
  128. // We need to wait for the pod to be running, otherwise the deletion
  129. // may be carried out immediately rather than gracefully.
  130. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  131. ginkgo.By("ensuring pod is modified")
  132. // save the running pod
  133. pod, err = podClient.Get(context.TODO(), pod.Name, metav1.GetOptions{})
  134. framework.ExpectNoError(err, "failed to GET scheduled pod")
  135. // check the annotation is there
  136. if _, ok := pod.Annotations["podpreset.admission.kubernetes.io/podpreset-hello"]; !ok {
  137. framework.Failf("Annotation not found in pod annotations: \n%v\n", pod.Annotations)
  138. }
  139. // verify the env is the same
  140. if !reflect.DeepEqual(pip.Spec.Env, pod.Spec.Containers[0].Env) {
  141. framework.Failf("env of pod container does not match the env of the pip: expected %#v, got: %#v", pip.Spec.Env, pod.Spec.Containers[0].Env)
  142. }
  143. if !reflect.DeepEqual(pip.Spec.Env, pod.Spec.InitContainers[0].Env) {
  144. framework.Failf("env of pod init container does not match the env of the pip: expected %#v, got: %#v", pip.Spec.Env, pod.Spec.InitContainers[0].Env)
  145. }
  146. })
  147. ginkgo.It("should not modify the pod on conflict", func() {
  148. ginkgo.By("Creating a pod preset")
  149. pip := &settingsv1alpha1.PodPreset{
  150. ObjectMeta: metav1.ObjectMeta{
  151. Name: "hello",
  152. Namespace: f.Namespace.Name,
  153. },
  154. Spec: settingsv1alpha1.PodPresetSpec{
  155. Selector: metav1.LabelSelector{
  156. MatchExpressions: []metav1.LabelSelectorRequirement{
  157. {
  158. Key: "security",
  159. Operator: metav1.LabelSelectorOpIn,
  160. Values: []string{"S2"},
  161. },
  162. },
  163. },
  164. Volumes: []v1.Volume{{Name: "vol", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}},
  165. VolumeMounts: []v1.VolumeMount{
  166. {Name: "vol", MountPath: "/foo"},
  167. },
  168. Env: []v1.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
  169. },
  170. }
  171. _, err := createPodPreset(f.ClientSet, f.Namespace.Name, pip)
  172. if apierrors.IsNotFound(err) {
  173. e2eskipper.Skipf("podpresets requires k8s.io/api/settings/v1alpha1 to be enabled")
  174. }
  175. framework.ExpectNoError(err)
  176. ginkgo.By("creating the pod")
  177. name := "pod-preset-pod"
  178. value := strconv.Itoa(time.Now().Nanosecond())
  179. originalPod := &v1.Pod{
  180. ObjectMeta: metav1.ObjectMeta{
  181. Name: name,
  182. Namespace: f.Namespace.Name,
  183. Labels: map[string]string{
  184. "name": "foo",
  185. "time": value,
  186. "security": "S2",
  187. },
  188. },
  189. Spec: v1.PodSpec{
  190. Containers: []v1.Container{
  191. {
  192. Name: "nginx",
  193. Image: imageutils.GetE2EImage(imageutils.Nginx),
  194. Env: []v1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value"}},
  195. },
  196. },
  197. InitContainers: []v1.Container{
  198. {
  199. Name: "init1",
  200. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  201. Env: []v1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value"}},
  202. Command: []string{"/bin/true"},
  203. },
  204. },
  205. },
  206. }
  207. ginkgo.By("setting up watch")
  208. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  209. options := metav1.ListOptions{LabelSelector: selector.String()}
  210. pods, err := podClient.List(context.TODO(), options)
  211. framework.ExpectNoError(err, "failed to query for pod")
  212. framework.ExpectEqual(len(pods.Items), 0)
  213. options = metav1.ListOptions{
  214. LabelSelector: selector.String(),
  215. ResourceVersion: pods.ListMeta.ResourceVersion,
  216. }
  217. w, err := podClient.Watch(context.TODO(), options)
  218. framework.ExpectNoError(err, "failed to set up watch")
  219. ginkgo.By("submitting the pod to kubernetes")
  220. podClient.Create(originalPod)
  221. ginkgo.By("verifying the pod is in kubernetes")
  222. selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  223. options = metav1.ListOptions{LabelSelector: selector.String()}
  224. pods, err = podClient.List(context.TODO(), options)
  225. framework.ExpectNoError(err, "failed to query for pod")
  226. framework.ExpectEqual(len(pods.Items), 1)
  227. ginkgo.By("verifying pod creation was observed")
  228. select {
  229. case event, _ := <-w.ResultChan():
  230. if event.Type != watch.Added {
  231. framework.Failf("Failed to observe pod creation: %v", event)
  232. }
  233. case <-time.After(framework.PodStartTimeout):
  234. framework.Failf("Timeout while waiting for pod creation")
  235. }
  236. // We need to wait for the pod to be running, otherwise the deletion
  237. // may be carried out immediately rather than gracefully.
  238. framework.ExpectNoError(f.WaitForPodRunning(originalPod.Name))
  239. ginkgo.By("ensuring pod is modified")
  240. // save the running pod
  241. pod, err := podClient.Get(context.TODO(), originalPod.Name, metav1.GetOptions{})
  242. framework.ExpectNoError(err, "failed to GET scheduled pod")
  243. // check the annotation is not there
  244. if _, ok := pod.Annotations["podpreset.admission.kubernetes.io/podpreset-hello"]; ok {
  245. framework.Failf("Annotation found in pod annotations and should not be: \n%v\n", pod.Annotations)
  246. }
  247. // verify the env is the same
  248. if !reflect.DeepEqual(originalPod.Spec.Containers[0].Env, pod.Spec.Containers[0].Env) {
  249. framework.Failf("env of pod container does not match the env of the original pod: expected %#v, got: %#v", originalPod.Spec.Containers[0].Env, pod.Spec.Containers[0].Env)
  250. }
  251. if !reflect.DeepEqual(originalPod.Spec.InitContainers[0].Env, pod.Spec.InitContainers[0].Env) {
  252. framework.Failf("env of pod init container does not match the env of the original pod: expected %#v, got: %#v", originalPod.Spec.InitContainers[0].Env, pod.Spec.InitContainers[0].Env)
  253. }
  254. })
  255. })
  256. func createPodPreset(c clientset.Interface, ns string, job *settingsv1alpha1.PodPreset) (*settingsv1alpha1.PodPreset, error) {
  257. return c.SettingsV1alpha1().PodPresets(ns).Create(context.TODO(), job, metav1.CreateOptions{})
  258. }