pods.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2018 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 main
  14. import (
  15. "fmt"
  16. "strings"
  17. corev1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/api/admission/v1beta1"
  20. "k8s.io/klog"
  21. )
  22. const (
  23. podsInitContainerPatch string = `[
  24. {"op":"add","path":"/spec/initContainers","value":[{"image":"webhook-added-image","name":"webhook-added-init-container","resources":{}}]}
  25. ]`
  26. )
  27. // only allow pods to pull images from specific registry.
  28. func admitPods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  29. klog.V(2).Info("admitting pods")
  30. podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
  31. if ar.Request.Resource != podResource {
  32. err := fmt.Errorf("expect resource to be %s", podResource)
  33. klog.Error(err)
  34. return toAdmissionResponse(err)
  35. }
  36. raw := ar.Request.Object.Raw
  37. pod := corev1.Pod{}
  38. deserializer := codecs.UniversalDeserializer()
  39. if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
  40. klog.Error(err)
  41. return toAdmissionResponse(err)
  42. }
  43. reviewResponse := v1beta1.AdmissionResponse{}
  44. reviewResponse.Allowed = true
  45. var msg string
  46. if v, ok := pod.Labels["webhook-e2e-test"]; ok {
  47. if v == "webhook-disallow" {
  48. reviewResponse.Allowed = false
  49. msg = msg + "the pod contains unwanted label; "
  50. }
  51. if v == "wait-forever" {
  52. reviewResponse.Allowed = false
  53. msg = msg + "the pod response should not be sent; "
  54. <-make(chan int) // Sleep forever - no one sends to this channel
  55. }
  56. }
  57. for _, container := range pod.Spec.Containers {
  58. if strings.Contains(container.Name, "webhook-disallow") {
  59. reviewResponse.Allowed = false
  60. msg = msg + "the pod contains unwanted container name; "
  61. }
  62. }
  63. if !reviewResponse.Allowed {
  64. reviewResponse.Result = &metav1.Status{Message: strings.TrimSpace(msg)}
  65. }
  66. return &reviewResponse
  67. }
  68. func mutatePods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  69. klog.V(2).Info("mutating pods")
  70. podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
  71. if ar.Request.Resource != podResource {
  72. klog.Errorf("expect resource to be %s", podResource)
  73. return nil
  74. }
  75. raw := ar.Request.Object.Raw
  76. pod := corev1.Pod{}
  77. deserializer := codecs.UniversalDeserializer()
  78. if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
  79. klog.Error(err)
  80. return toAdmissionResponse(err)
  81. }
  82. reviewResponse := v1beta1.AdmissionResponse{}
  83. reviewResponse.Allowed = true
  84. if pod.Name == "webhook-to-be-mutated" {
  85. reviewResponse.Patch = []byte(podsInitContainerPatch)
  86. pt := v1beta1.PatchTypeJSONPatch
  87. reviewResponse.PatchType = &pt
  88. }
  89. return &reviewResponse
  90. }
  91. // denySpecificAttachment denies `kubectl attach to-be-attached-pod -i -c=container1"
  92. // or equivalent client requests.
  93. func denySpecificAttachment(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  94. klog.V(2).Info("handling attaching pods")
  95. if ar.Request.Name != "to-be-attached-pod" {
  96. return &v1beta1.AdmissionResponse{Allowed: true}
  97. }
  98. podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
  99. if e, a := podResource, ar.Request.Resource; e != a {
  100. err := fmt.Errorf("expect resource to be %s, got %s", e, a)
  101. klog.Error(err)
  102. return toAdmissionResponse(err)
  103. }
  104. if e, a := "attach", ar.Request.SubResource; e != a {
  105. err := fmt.Errorf("expect subresource to be %s, got %s", e, a)
  106. klog.Error(err)
  107. return toAdmissionResponse(err)
  108. }
  109. raw := ar.Request.Object.Raw
  110. podAttachOptions := corev1.PodAttachOptions{}
  111. deserializer := codecs.UniversalDeserializer()
  112. if _, _, err := deserializer.Decode(raw, nil, &podAttachOptions); err != nil {
  113. klog.Error(err)
  114. return toAdmissionResponse(err)
  115. }
  116. klog.V(2).Info(fmt.Sprintf("podAttachOptions=%#v\n", podAttachOptions))
  117. if !podAttachOptions.Stdin || podAttachOptions.Container != "container1" {
  118. return &v1beta1.AdmissionResponse{Allowed: true}
  119. }
  120. return &v1beta1.AdmissionResponse{
  121. Allowed: false,
  122. Result: &metav1.Status{
  123. Message: "attaching to pod 'to-be-attached-pod' is not allowed",
  124. },
  125. }
  126. }