admission_test.go 5.3 KB

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