github.com/openshift/installer@v1.4.17/pkg/explain/schema.go (about)

     1  package explain
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
     6  	"k8s.io/apimachinery/pkg/runtime"
     7  	"k8s.io/apimachinery/pkg/runtime/serializer"
     8  )
     9  
    10  func loadSchema(b []byte) (*apiextv1.JSONSchemaProps, error) {
    11  	scheme := runtime.NewScheme()
    12  	codecs := serializer.NewCodecFactory(scheme)
    13  	apiextv1.AddToScheme(scheme)
    14  	obj, err := runtime.Decode(codecs.UniversalDecoder(apiextv1.SchemeGroupVersion), b)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	crd, ok := obj.(*apiextv1.CustomResourceDefinition)
    20  	if !ok {
    21  		return nil, errors.Errorf("invalid object, should be *apiextv1.CustomResourceDefinition but found %T", obj)
    22  	}
    23  	if len(crd.Spec.Versions) != 1 {
    24  		return nil, errors.New("missing versions in CRD")
    25  	}
    26  	return crd.Spec.Versions[0].Schema.OpenAPIV3Schema, nil
    27  }