admission_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 alwayspullimages
  14. import (
  15. "context"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apiserver/pkg/admission"
  20. admissiontesting "k8s.io/apiserver/pkg/admission/testing"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. )
  23. // TestAdmission verifies all create requests for pods result in every container's image pull policy
  24. // set to Always
  25. func TestAdmission(t *testing.T) {
  26. namespace := "test"
  27. handler := admissiontesting.WithReinvocationTesting(t, &AlwaysPullImages{})
  28. pod := api.Pod{
  29. ObjectMeta: metav1.ObjectMeta{Name: "123", Namespace: namespace},
  30. Spec: api.PodSpec{
  31. InitContainers: []api.Container{
  32. {Name: "init1", Image: "image"},
  33. {Name: "init2", Image: "image", ImagePullPolicy: api.PullNever},
  34. {Name: "init3", Image: "image", ImagePullPolicy: api.PullIfNotPresent},
  35. {Name: "init4", Image: "image", ImagePullPolicy: api.PullAlways},
  36. },
  37. Containers: []api.Container{
  38. {Name: "ctr1", Image: "image"},
  39. {Name: "ctr2", Image: "image", ImagePullPolicy: api.PullNever},
  40. {Name: "ctr3", Image: "image", ImagePullPolicy: api.PullIfNotPresent},
  41. {Name: "ctr4", Image: "image", ImagePullPolicy: api.PullAlways},
  42. },
  43. },
  44. }
  45. err := handler.Admit(context.TODO(), admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
  46. if err != nil {
  47. t.Errorf("Unexpected error returned from admission handler")
  48. }
  49. for _, c := range pod.Spec.InitContainers {
  50. if c.ImagePullPolicy != api.PullAlways {
  51. t.Errorf("Container %v: expected pull always, got %v", c, c.ImagePullPolicy)
  52. }
  53. }
  54. for _, c := range pod.Spec.Containers {
  55. if c.ImagePullPolicy != api.PullAlways {
  56. t.Errorf("Container %v: expected pull always, got %v", c, c.ImagePullPolicy)
  57. }
  58. }
  59. }
  60. func TestValidate(t *testing.T) {
  61. namespace := "test"
  62. handler := &AlwaysPullImages{}
  63. pod := api.Pod{
  64. ObjectMeta: metav1.ObjectMeta{Name: "123", Namespace: namespace},
  65. Spec: api.PodSpec{
  66. InitContainers: []api.Container{
  67. {Name: "init1", Image: "image"},
  68. {Name: "init2", Image: "image", ImagePullPolicy: api.PullNever},
  69. {Name: "init3", Image: "image", ImagePullPolicy: api.PullIfNotPresent},
  70. {Name: "init4", Image: "image", ImagePullPolicy: api.PullAlways},
  71. },
  72. Containers: []api.Container{
  73. {Name: "ctr1", Image: "image"},
  74. {Name: "ctr2", Image: "image", ImagePullPolicy: api.PullNever},
  75. {Name: "ctr3", Image: "image", ImagePullPolicy: api.PullIfNotPresent},
  76. {Name: "ctr4", Image: "image", ImagePullPolicy: api.PullAlways},
  77. },
  78. },
  79. }
  80. expectedError := `[` +
  81. `pods "123" is forbidden: spec.initContainers[0].imagePullPolicy: Unsupported value: "": supported values: "Always", ` +
  82. `pods "123" is forbidden: spec.initContainers[1].imagePullPolicy: Unsupported value: "Never": supported values: "Always", ` +
  83. `pods "123" is forbidden: spec.initContainers[2].imagePullPolicy: Unsupported value: "IfNotPresent": supported values: "Always", ` +
  84. `pods "123" is forbidden: spec.containers[0].imagePullPolicy: Unsupported value: "": supported values: "Always", ` +
  85. `pods "123" is forbidden: spec.containers[1].imagePullPolicy: Unsupported value: "Never": supported values: "Always", ` +
  86. `pods "123" is forbidden: spec.containers[2].imagePullPolicy: Unsupported value: "IfNotPresent": supported values: "Always"]`
  87. err := handler.Validate(context.TODO(), admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
  88. if err == nil {
  89. t.Fatal("missing expected error")
  90. }
  91. if err.Error() != expectedError {
  92. t.Fatal(err)
  93. }
  94. }
  95. // TestOtherResources ensures that this admission controller is a no-op for other resources,
  96. // subresources, and non-pods.
  97. func TestOtherResources(t *testing.T) {
  98. namespace := "testnamespace"
  99. name := "testname"
  100. pod := &api.Pod{
  101. ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
  102. Spec: api.PodSpec{
  103. Containers: []api.Container{
  104. {Name: "ctr2", Image: "image", ImagePullPolicy: api.PullNever},
  105. },
  106. },
  107. }
  108. tests := []struct {
  109. name string
  110. kind string
  111. resource string
  112. subresource string
  113. object runtime.Object
  114. expectError bool
  115. }{
  116. {
  117. name: "non-pod resource",
  118. kind: "Foo",
  119. resource: "foos",
  120. object: pod,
  121. },
  122. {
  123. name: "pod subresource",
  124. kind: "Pod",
  125. resource: "pods",
  126. subresource: "exec",
  127. object: pod,
  128. },
  129. {
  130. name: "non-pod object",
  131. kind: "Pod",
  132. resource: "pods",
  133. object: &api.Service{},
  134. expectError: true,
  135. },
  136. }
  137. for _, tc := range tests {
  138. handler := admissiontesting.WithReinvocationTesting(t, &AlwaysPullImages{})
  139. err := handler.Admit(context.TODO(), admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)
  140. if tc.expectError {
  141. if err == nil {
  142. t.Errorf("%s: unexpected nil error", tc.name)
  143. }
  144. continue
  145. }
  146. if err != nil {
  147. t.Errorf("%s: unexpected error: %v", tc.name, err)
  148. continue
  149. }
  150. if e, a := api.PullNever, pod.Spec.Containers[0].ImagePullPolicy; e != a {
  151. t.Errorf("%s: image pull policy was changed to %s", tc.name, a)
  152. }
  153. }
  154. }