...

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

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

     1  // Copyright 2023 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  	"k8s.io/apimachinery/pkg/runtime/schema"
    22  )
    23  
    24  type SchemaReference struct {
    25  	CRD        *apiextensions.CustomResourceDefinition
    26  	JsonSchema *apiextensions.JSONSchemaProps
    27  	GVK        schema.GroupVersionKind
    28  }
    29  
    30  type SchemaReferenceUpdater interface {
    31  	// UpdateSchema updates the schema reference of the controller using
    32  	// its corresponding CRD.
    33  	UpdateSchema(crd *apiextensions.CustomResourceDefinition) error
    34  }
    35  
    36  // UpdateSchema is a helper function to update the input schema reference using the input crd.
    37  func UpdateSchema(schemaRef *SchemaReference, crd *apiextensions.CustomResourceDefinition) error {
    38  	gvk := schema.GroupVersionKind{
    39  		Group:   crd.Spec.Group,
    40  		Version: GetVersionFromCRD(crd),
    41  		Kind:    crd.Spec.Names.Kind,
    42  	}
    43  	if schemaRef.GVK.String() != gvk.String() {
    44  		return fmt.Errorf("unexpected mismatch of GVK when updating schema reference for controller, old GVK = %s, new GVK = %s", schemaRef.GVK.String(), gvk.String())
    45  	}
    46  	schemaRef.CRD = crd
    47  	schemaRef.JsonSchema = GetOpenAPIV3SchemaFromCRD(crd)
    48  	return nil
    49  }
    50  

View as plain text