...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/crds.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s

     1  // Copyright 2022 Google LLC
     2  //
     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  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s
    16  
    17  import (
    18  	"fmt"
    19  
    20  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    21  )
    22  
    23  func GetAPIVersionFromCRD(crd *apiextensions.CustomResourceDefinition) string {
    24  	panicIfNoVersionPresent(crd)
    25  	// Currently KCC CRDs only support one version.
    26  	return fmt.Sprintf("%v/%v", crd.Spec.Group, crd.Spec.Versions[0].Name)
    27  }
    28  
    29  func GetVersionFromCRD(crd *apiextensions.CustomResourceDefinition) string {
    30  	panicIfNoVersionPresent(crd)
    31  	// Currently KCC CRDs only support one version.
    32  	return crd.Spec.Versions[0].Name
    33  }
    34  
    35  func GetOpenAPIV3SchemaFromCRD(crd *apiextensions.CustomResourceDefinition) *apiextensions.JSONSchemaProps {
    36  	panicIfNoVersionPresent(crd)
    37  	// Currently KCC CRDs only support one version.
    38  	return crd.Spec.Versions[0].Schema.OpenAPIV3Schema
    39  }
    40  
    41  func panicIfNoVersionPresent(crd *apiextensions.CustomResourceDefinition) {
    42  	if len(crd.Spec.Versions) == 0 {
    43  		panic(fmt.Sprintf("no versions present in CRD %v\n", crd.GetName()))
    44  	}
    45  }
    46  

View as plain text