configmap.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "k8s.io/api/admission/v1beta1"
  16. corev1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/klog"
  19. )
  20. const (
  21. configMapPatch1 string = `[
  22. { "op": "add", "path": "/data/mutation-stage-1", "value": "yes" }
  23. ]`
  24. configMapPatch2 string = `[
  25. { "op": "add", "path": "/data/mutation-stage-2", "value": "yes" }
  26. ]`
  27. )
  28. // deny configmaps with specific key-value pair.
  29. func admitConfigMaps(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  30. klog.V(2).Info("admitting configmaps")
  31. configMapResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
  32. if ar.Request.Resource != configMapResource {
  33. klog.Errorf("expect resource to be %s", configMapResource)
  34. return nil
  35. }
  36. var raw []byte
  37. if ar.Request.Operation == v1beta1.Delete {
  38. raw = ar.Request.OldObject.Raw
  39. } else {
  40. raw = ar.Request.Object.Raw
  41. }
  42. configmap := corev1.ConfigMap{}
  43. deserializer := codecs.UniversalDeserializer()
  44. if _, _, err := deserializer.Decode(raw, nil, &configmap); err != nil {
  45. klog.Error(err)
  46. return toAdmissionResponse(err)
  47. }
  48. reviewResponse := v1beta1.AdmissionResponse{}
  49. reviewResponse.Allowed = true
  50. for k, v := range configmap.Data {
  51. if k == "webhook-e2e-test" && v == "webhook-disallow" &&
  52. (ar.Request.Operation == v1beta1.Create || ar.Request.Operation == v1beta1.Update) {
  53. reviewResponse.Allowed = false
  54. reviewResponse.Result = &metav1.Status{
  55. Reason: "the configmap contains unwanted key and value",
  56. }
  57. }
  58. if k == "webhook-e2e-test" && v == "webhook-nondeletable" && ar.Request.Operation == v1beta1.Delete {
  59. reviewResponse.Allowed = false
  60. reviewResponse.Result = &metav1.Status{
  61. Reason: "the configmap cannot be deleted because it contains unwanted key and value",
  62. }
  63. }
  64. }
  65. return &reviewResponse
  66. }
  67. func mutateConfigmaps(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  68. klog.V(2).Info("mutating configmaps")
  69. configMapResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
  70. if ar.Request.Resource != configMapResource {
  71. klog.Errorf("expect resource to be %s", configMapResource)
  72. return nil
  73. }
  74. raw := ar.Request.Object.Raw
  75. configmap := corev1.ConfigMap{}
  76. deserializer := codecs.UniversalDeserializer()
  77. if _, _, err := deserializer.Decode(raw, nil, &configmap); err != nil {
  78. klog.Error(err)
  79. return toAdmissionResponse(err)
  80. }
  81. reviewResponse := v1beta1.AdmissionResponse{}
  82. reviewResponse.Allowed = true
  83. if configmap.Data["mutation-start"] == "yes" {
  84. reviewResponse.Patch = []byte(configMapPatch1)
  85. }
  86. if configmap.Data["mutation-stage-1"] == "yes" {
  87. reviewResponse.Patch = []byte(configMapPatch2)
  88. }
  89. pt := v1beta1.PatchTypeJSONPatch
  90. reviewResponse.PatchType = &pt
  91. return &reviewResponse
  92. }