...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/validation.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package framework
     5  
     6  import (
     7  	"k8s.io/kube-openapi/pkg/validation/spec"
     8  	"sigs.k8s.io/kustomize/kyaml/errors"
     9  	"sigs.k8s.io/kustomize/kyaml/resid"
    10  	k8syaml "sigs.k8s.io/yaml"
    11  )
    12  
    13  // SchemaFromFunctionDefinition extracts the schema for a particular GVK from the provided KRMFunctionDefinition
    14  // Since the relevant fields of KRMFunctionDefinition exactly match the ones in CustomResourceDefinition,
    15  // this helper can also load CRDs (e.g. produced by KubeBuilder) transparently.
    16  func SchemaFromFunctionDefinition(gvk resid.Gvk, data string) (*spec.Schema, error) {
    17  	var def KRMFunctionDefinition
    18  	// need to use sigs yaml because spec.Schema type only has json tags
    19  	if err := k8syaml.Unmarshal([]byte(data), &def); err != nil {
    20  		return nil, errors.WrapPrefixf(err, "unmarshalling %s", FunctionDefinitionKind)
    21  	}
    22  	var foundGVKs []*resid.Gvk
    23  	var schema *spec.Schema
    24  	for i, version := range def.Spec.Versions {
    25  		versionGVK := resid.Gvk{Group: def.Spec.Group, Kind: def.Spec.Names.Kind, Version: version.Name}
    26  		if gvk.Equals(versionGVK) {
    27  			schema = def.Spec.Versions[i].Schema.OpenAPIV3Schema
    28  			break
    29  		}
    30  		foundGVKs = append(foundGVKs, &versionGVK)
    31  	}
    32  	if schema == nil {
    33  		return nil, errors.Errorf("%s does not define %s (defines: %s)", FunctionDefinitionKind, gvk, foundGVKs)
    34  	}
    35  	return schema, nil
    36  }
    37  

View as plain text