istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/codegen/templates/gvk.go.tmpl (about)

     1  // Code generated by pkg/config/schema/codegen/tools/collections.main.go. DO NOT EDIT.
     2  
     3  package {{.PackageName}}
     4  
     5  import (
     6  	"k8s.io/apimachinery/pkg/runtime/schema"
     7  	"istio.io/istio/pkg/config"
     8  	"istio.io/istio/pkg/config/schema/gvr"
     9  )
    10  
    11  var (
    12  {{- range $entry := .Entries }}
    13  	{{$entry.Resource.Identifier}} = config.GroupVersionKind{Group: "{{$entry.Resource.Group}}", Version: "{{$entry.Resource.Version}}", Kind: "{{$entry.Resource.Kind}}"}
    14  	{{- range $alias := .Resource.VersionAliases }}
    15  	{{$entry.Resource.Identifier}}_{{$alias}} = config.GroupVersionKind{Group: "{{$entry.Resource.Group}}", Version: "{{$alias}}", Kind: "{{$entry.Resource.Kind}}"}
    16      {{- end }}
    17  {{- end }}
    18  )
    19  
    20  // ToGVR converts a GVK to a GVR.
    21  func ToGVR(g config.GroupVersionKind) (schema.GroupVersionResource, bool) {
    22  	switch g {
    23  {{- range $entry := .Entries }}
    24  		case {{$entry.Resource.Identifier}}:
    25  			return gvr.{{$entry.Resource.Identifier}}, true
    26  	{{- range $alias := .Resource.VersionAliases }}
    27  		case {{$entry.Resource.Identifier}}_{{$alias}}:
    28  			return gvr.{{$entry.Resource.Identifier}}_{{$alias}}, true
    29      {{- end }}
    30  {{- end }}
    31  	}
    32  
    33  	return schema.GroupVersionResource{}, false
    34  }
    35  
    36  // MustToGVR converts a GVK to a GVR, and panics if it cannot be converted
    37  // Warning: this is only safe for known types; do not call on arbitrary GVKs
    38  func MustToGVR(g config.GroupVersionKind) schema.GroupVersionResource {
    39  	r, ok := ToGVR(g)
    40  	if !ok {
    41  		panic("unknown kind: " + g.String())
    42  	}
    43  	return r
    44  }
    45  
    46  // FromGVR converts a GVR to a GVK.
    47  func FromGVR(g schema.GroupVersionResource) (config.GroupVersionKind, bool) {
    48  	switch g {
    49  {{- range .Entries }}
    50  		case gvr.{{.Resource.Identifier}}:
    51  			return {{.Resource.Identifier}}, true
    52  {{- end }}
    53  	}
    54  
    55  	return config.GroupVersionKind{}, false
    56  }
    57  
    58  // FromGVR converts a GVR to a GVK, and panics if it cannot be converted
    59  // Warning: this is only safe for known types; do not call on arbitrary GVRs
    60  func MustFromGVR(g schema.GroupVersionResource) config.GroupVersionKind {
    61  	r, ok := FromGVR(g)
    62  	if !ok {
    63  		panic("unknown kind: " + g.String())
    64  	}
    65  	return r
    66  }