crd.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/api/admission/v1beta1"
  19. "k8s.io/klog"
  20. )
  21. // This function expects all CRDs submitted to it to be apiextensions.k8s.io/v1beta1
  22. // TODO: When apiextensions.k8s.io/v1 is added we will need to update this function.
  23. func admitCRD(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
  24. klog.V(2).Info("admitting crd")
  25. crdResource := metav1.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1beta1", Resource: "customresourcedefinitions"}
  26. if ar.Request.Resource != crdResource {
  27. err := fmt.Errorf("expect resource to be %s", crdResource)
  28. klog.Error(err)
  29. return toAdmissionResponse(err)
  30. }
  31. raw := ar.Request.Object.Raw
  32. crd := apiextensionsv1beta1.CustomResourceDefinition{}
  33. deserializer := codecs.UniversalDeserializer()
  34. if _, _, err := deserializer.Decode(raw, nil, &crd); err != nil {
  35. klog.Error(err)
  36. return toAdmissionResponse(err)
  37. }
  38. reviewResponse := v1beta1.AdmissionResponse{}
  39. reviewResponse.Allowed = true
  40. if v, ok := crd.Labels["webhook-e2e-test"]; ok {
  41. if v == "webhook-disallow" {
  42. reviewResponse.Allowed = false
  43. reviewResponse.Result = &metav1.Status{Message: "the crd contains unwanted label"}
  44. }
  45. }
  46. return &reviewResponse
  47. }