| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package restmapper |
| 18 |
|
| 19 |
import ( |
| 20 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 21 |
"k8s.io/client-go/discovery" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
type CategoryExpander interface { |
| 27 |
Expand(category string) ([]schema.GroupResource, bool) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type SimpleCategoryExpander struct { |
| 33 |
Expansions map[string][]schema.GroupResource |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (e SimpleCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) { |
| 38 |
ret, ok := e.Expansions[category] |
| 39 |
return ret, ok |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type discoveryCategoryExpander struct { |
| 45 |
discoveryClient discovery.DiscoveryInterface |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func NewDiscoveryCategoryExpander(client discovery.DiscoveryInterface) CategoryExpander { |
| 52 |
if client == nil { |
| 53 |
panic("Please provide discovery client to shortcut expander") |
| 54 |
} |
| 55 |
return discoveryCategoryExpander{discoveryClient: client} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func (e discoveryCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) { |
| 60 |
|
| 61 |
_, apiResourceLists, _ := e.discoveryClient.ServerGroupsAndResources() |
| 62 |
if len(apiResourceLists) == 0 { |
| 63 |
return nil, false |
| 64 |
} |
| 65 |
|
| 66 |
discoveredExpansions := map[string][]schema.GroupResource{} |
| 67 |
for _, apiResourceList := range apiResourceLists { |
| 68 |
gv, err := schema.ParseGroupVersion(apiResourceList.GroupVersion) |
| 69 |
if err != nil { |
| 70 |
continue |
| 71 |
} |
| 72 |
|
| 73 |
for _, apiResource := range apiResourceList.APIResources { |
| 74 |
if categories := apiResource.Categories; len(categories) > 0 { |
| 75 |
for _, category := range categories { |
| 76 |
groupResource := schema.GroupResource{ |
| 77 |
Group: gv.Group, |
| 78 |
Resource: apiResource.Name, |
| 79 |
} |
| 80 |
discoveredExpansions[category] = append(discoveredExpansions[category], groupResource) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
ret, ok := discoveredExpansions[category] |
| 87 |
return ret, ok |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
type UnionCategoryExpander []CategoryExpander |
| 93 |
|
| 94 |
|
| 95 |
func (u UnionCategoryExpander) Expand(category string) ([]schema.GroupResource, bool) { |
| 96 |
ret := []schema.GroupResource{} |
| 97 |
ok := false |
| 98 |
|
| 99 |
|
| 100 |
for _, expansion := range u { |
| 101 |
curr, currOk := expansion.Expand(category) |
| 102 |
|
| 103 |
for _, currGR := range curr { |
| 104 |
found := false |
| 105 |
for _, existing := range ret { |
| 106 |
if existing == currGR { |
| 107 |
found = true |
| 108 |
break |
| 109 |
} |
| 110 |
} |
| 111 |
if !found { |
| 112 |
ret = append(ret, currGR) |
| 113 |
} |
| 114 |
} |
| 115 |
ok = ok || currOk |
| 116 |
} |
| 117 |
|
| 118 |
return ret, ok |
| 119 |
} |