github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/api/v1/registry.go (about)

     1  package v1
     2  
     3  import "fmt"
     4  
     5  var (
     6  	scopeKindMap = map[GroupKind]string{}
     7  
     8  	resourceMap = map[GroupKind]string{}
     9  
    10  	gvkSet = map[GroupVersionKind]bool{}
    11  
    12  	plurals = map[string]string{}
    13  )
    14  
    15  // RegisterGVK registers a GroupVersionKind with optional scope and mandatory resource
    16  func RegisterGVK(gvk GroupVersionKind, scopeKind, resource string) {
    17  	if gvk.Group == "" || gvk.Kind == "" || gvk.APIVersion == "" || resource == "" {
    18  		panic(fmt.Sprintf("encountered empty fields while registering gvk to resource: %+v, %s", gvk, resource))
    19  	}
    20  
    21  	if gvkSet[gvk] {
    22  		panic(fmt.Sprint("Attempt to register duplicate gvk: ", gvk))
    23  	}
    24  	gvkSet[gvk] = true
    25  
    26  	if sk, ok := scopeKindMap[gvk.GroupKind]; ok && sk != scopeKind {
    27  		panic(fmt.Sprintf("Attempt to set different scope: %s for gvk: %v. Previously set scope: %s", sk, gvk, sk))
    28  	}
    29  
    30  	scopeKindMap[gvk.GroupKind] = scopeKind
    31  
    32  	if r, ok := resourceMap[gvk.GroupKind]; ok && r != resource {
    33  		panic(fmt.Sprintf("Attempt to register different resurce: %s for gvk: %v. Previously set resource: %s", scopeKind, gvk, r))
    34  	}
    35  
    36  	resourceMap[gvk.GroupKind] = resource
    37  
    38  	plurals[gvk.Kind] = resource
    39  }
    40  
    41  // GetScope return the scope of a Kind
    42  func GetScope(gv GroupKind) (k string, ok bool) {
    43  	k, ok = scopeKindMap[gv]
    44  	return
    45  }
    46  
    47  // GetResource returns the resource name of a Kind
    48  func GetResource(gv GroupKind) (r string, ok bool) {
    49  	r, ok = resourceMap[gv]
    50  	return
    51  }
    52  
    53  // GVKSet -
    54  func GVKSet() []GroupVersionKind {
    55  	res := make([]GroupVersionKind, len(gvkSet))
    56  
    57  	i := 0
    58  	for k := range gvkSet {
    59  		res[i] = k
    60  		i++
    61  	}
    62  
    63  	return res
    64  }
    65  
    66  // GetPluralFromKind
    67  func GetPluralFromKind(kind string) (p string, ok bool) {
    68  	p, ok = plurals[kind]
    69  	return
    70  }