customresource.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "encoding/json"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/api/admission/v1beta1"
  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 v1beta1.AdmissionReview) *v1beta1.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 toAdmissionResponse(err)
  39. }
  40. reviewResponse := v1beta1.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. pt := v1beta1.PatchTypeJSONPatch
  49. reviewResponse.PatchType = &pt
  50. return &reviewResponse
  51. }
  52. func admitCustomResource(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  53. klog.V(2).Info("admitting custom resource")
  54. cr := struct {
  55. metav1.ObjectMeta
  56. Data map[string]string
  57. }{}
  58. var raw []byte
  59. if ar.Request.Operation == v1beta1.Delete {
  60. raw = ar.Request.OldObject.Raw
  61. } else {
  62. raw = ar.Request.Object.Raw
  63. }
  64. err := json.Unmarshal(raw, &cr)
  65. if err != nil {
  66. klog.Error(err)
  67. return toAdmissionResponse(err)
  68. }
  69. reviewResponse := v1beta1.AdmissionResponse{}
  70. reviewResponse.Allowed = true
  71. for k, v := range cr.Data {
  72. if k == "webhook-e2e-test" && v == "webhook-disallow" &&
  73. (ar.Request.Operation == v1beta1.Create || ar.Request.Operation == v1beta1.Update) {
  74. reviewResponse.Allowed = false
  75. reviewResponse.Result = &metav1.Status{
  76. Reason: "the custom resource contains unwanted data",
  77. }
  78. }
  79. if k == "webhook-e2e-test" && v == "webhook-nondeletable" && ar.Request.Operation == v1beta1.Delete {
  80. reviewResponse.Allowed = false
  81. reviewResponse.Result = &metav1.Status{
  82. Reason: "the custom resource cannot be deleted because it contains unwanted key and value",
  83. }
  84. }
  85. }
  86. return &reviewResponse
  87. }