github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/crd/version.go (about)

     1  package crd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     8  	"k8s.io/apimachinery/pkg/util/yaml"
     9  )
    10  
    11  // V1Beta1 refers to the deprecated v1beta1 APIVersion of CRD objects
    12  const V1Beta1Version string = "v1beta1"
    13  
    14  // V1 refers to the new v1 APIVersion of CRD objects
    15  const V1Version string = "v1"
    16  
    17  var supportedCRDVersions = map[string]struct{}{
    18  	V1Beta1Version: {},
    19  	V1Version:      {},
    20  }
    21  
    22  // Version takes a CRD manifest and determines whether it is v1 or v1beta1 type based on the APIVersion.
    23  func Version(manifest *string) (string, error) {
    24  	if manifest == nil {
    25  		return "", fmt.Errorf("empty CRD manifest")
    26  	}
    27  
    28  	dec := yaml.NewYAMLOrJSONDecoder(strings.NewReader(*manifest), 10)
    29  	unst := &unstructured.Unstructured{}
    30  	if err := dec.Decode(unst); err != nil {
    31  		return "", err
    32  	}
    33  
    34  	v := unst.GroupVersionKind().Version
    35  	if _, ok := supportedCRDVersions[v]; !ok {
    36  		return "", fmt.Errorf("CRD APIVersion from manifest not supported: %s", v)
    37  	}
    38  
    39  	return v, nil
    40  }