github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/crd/conv.go (about) 1 package crd 2 3 import ( 4 "fmt" 5 6 apiextinternal "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" 7 apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 8 apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" 9 "k8s.io/apimachinery/pkg/runtime" 10 "k8s.io/apimachinery/pkg/runtime/schema" 11 ) 12 13 var ( 14 conversionScheme = runtime.NewScheme() 15 ) 16 17 func init() { 18 if err := apiextinternal.AddToScheme(conversionScheme); err != nil { 19 panic("must be able to add internal apiextensions to the CRD conversion Scheme") 20 } 21 if err := apiext.AddToScheme(conversionScheme); err != nil { 22 panic("must be able to add apiextensions/v1 to the CRD conversion Scheme") 23 } 24 } 25 26 // AsVersion converts a CRD from the canonical internal form (currently v1) to some external form. 27 func AsVersion(original apiext.CustomResourceDefinition, gv schema.GroupVersion) (runtime.Object, error) { 28 // TODO: Do we need to keep maintaining this conversion function 29 // post 1.22 when only CRDv1 is served by the apiserver? 30 if gv == apiextv1beta1.SchemeGroupVersion { 31 return nil, fmt.Errorf("apiVersion %q is not supported", gv.String()) 32 } 33 // We can use the internal versions an existing conversions from kubernetes, since they're not in k/k itself. 34 // This punts the problem of conversion down the road for a future maintainer (or future instance of @directxman12) 35 // when we have to support older versions that get removed, or when API machinery decides to yell at us for this 36 // questionable decision. 37 intVer, err := conversionScheme.ConvertToVersion(&original, apiextinternal.SchemeGroupVersion) 38 if err != nil { 39 return nil, fmt.Errorf("unable to convert to internal CRD version: %w", err) 40 } 41 42 return conversionScheme.ConvertToVersion(intVer, gv) 43 }