k8s.io/kubernetes@v1.29.3/test/images/agnhost/webhook/crd.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package webhook
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/api/admission/v1"
    23  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    24  	apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/klog/v2"
    27  )
    28  
    29  // This function expects all CRDs submitted to it to be apiextensions.k8s.io/v1beta1 or apiextensions.k8s.io/v1.
    30  func admitCRD(ar v1.AdmissionReview) *v1.AdmissionResponse {
    31  	klog.V(2).Info("admitting crd")
    32  
    33  	resource := "customresourcedefinitions"
    34  	v1beta1GVR := metav1.GroupVersionResource{Group: apiextensionsv1beta1.GroupName, Version: "v1beta1", Resource: resource}
    35  	v1GVR := metav1.GroupVersionResource{Group: apiextensionsv1.GroupName, Version: "v1", Resource: resource}
    36  
    37  	reviewResponse := v1.AdmissionResponse{}
    38  	reviewResponse.Allowed = true
    39  
    40  	raw := ar.Request.Object.Raw
    41  	var labels map[string]string
    42  
    43  	switch ar.Request.Resource {
    44  	case v1beta1GVR:
    45  		crd := apiextensionsv1beta1.CustomResourceDefinition{}
    46  		deserializer := codecs.UniversalDeserializer()
    47  		if _, _, err := deserializer.Decode(raw, nil, &crd); err != nil {
    48  			klog.Error(err)
    49  			return toV1AdmissionResponse(err)
    50  		}
    51  		labels = crd.Labels
    52  	case v1GVR:
    53  		crd := apiextensionsv1.CustomResourceDefinition{}
    54  		deserializer := codecs.UniversalDeserializer()
    55  		if _, _, err := deserializer.Decode(raw, nil, &crd); err != nil {
    56  			klog.Error(err)
    57  			return toV1AdmissionResponse(err)
    58  		}
    59  		labels = crd.Labels
    60  	default:
    61  		err := fmt.Errorf("expect resource to be one of [%v, %v] but got %v", v1beta1GVR, v1GVR, ar.Request.Resource)
    62  		klog.Error(err)
    63  		return toV1AdmissionResponse(err)
    64  	}
    65  
    66  	if v, ok := labels["webhook-e2e-test"]; ok {
    67  		if v == "webhook-disallow" {
    68  			reviewResponse.Allowed = false
    69  			reviewResponse.Result = &metav1.Status{Message: "the crd contains unwanted label"}
    70  		}
    71  	}
    72  	return &reviewResponse
    73  
    74  }