customresource.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 webhook
  14. import (
  15. "encoding/json"
  16. v1 "k8s.io/api/admission/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/klog"
  19. )
  20. const (
  21. customResourcePatch1 string = `[
  22. { "op": "add", "path": "/data/mutation-stage-1", "value": "yes" }
  23. ]`
  24. customResourcePatch2 string = `[
  25. { "op": "add", "path": "/data/mutation-stage-2", "value": "yes" }
  26. ]`
  27. )
  28. func mutateCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse {
  29. klog.V(2).Info("mutating custom resource")
  30. cr := struct {
  31. metav1.ObjectMeta
  32. Data map[string]string
  33. }{}
  34. raw := ar.Request.Object.Raw
  35. err := json.Unmarshal(raw, &cr)
  36. if err != nil {
  37. klog.Error(err)
  38. return toV1AdmissionResponse(err)
  39. }
  40. reviewResponse := v1.AdmissionResponse{}
  41. reviewResponse.Allowed = true
  42. if cr.Data["mutation-start"] == "yes" {
  43. reviewResponse.Patch = []byte(customResourcePatch1)
  44. }
  45. if cr.Data["mutation-stage-1"] == "yes" {
  46. reviewResponse.Patch = []byte(customResourcePatch2)
  47. }
  48. if len(reviewResponse.Patch) != 0 {
  49. pt := v1.PatchTypeJSONPatch
  50. reviewResponse.PatchType = &pt
  51. }
  52. return &reviewResponse
  53. }
  54. func admitCustomResource(ar v1.AdmissionReview) *v1.AdmissionResponse {
  55. klog.V(2).Info("admitting custom resource")
  56. cr := struct {
  57. metav1.ObjectMeta
  58. Data map[string]string
  59. }{}
  60. var raw []byte
  61. if ar.Request.Operation == v1.Delete {
  62. raw = ar.Request.OldObject.Raw
  63. } else {
  64. raw = ar.Request.Object.Raw
  65. }
  66. err := json.Unmarshal(raw, &cr)
  67. if err != nil {
  68. klog.Error(err)
  69. return toV1AdmissionResponse(err)
  70. }
  71. reviewResponse := v1.AdmissionResponse{}
  72. reviewResponse.Allowed = true
  73. for k, v := range cr.Data {
  74. if k == "webhook-e2e-test" && v == "webhook-disallow" &&
  75. (ar.Request.Operation == v1.Create || ar.Request.Operation == v1.Update) {
  76. reviewResponse.Allowed = false
  77. reviewResponse.Result = &metav1.Status{
  78. Reason: "the custom resource contains unwanted data",
  79. }
  80. }
  81. if k == "webhook-e2e-test" && v == "webhook-nondeletable" && ar.Request.Operation == v1.Delete {
  82. reviewResponse.Allowed = false
  83. reviewResponse.Result = &metav1.Status{
  84. Reason: "the custom resource cannot be deleted because it contains unwanted key and value",
  85. }
  86. }
  87. }
  88. return &reviewResponse
  89. }