MCPcopy
hub / github.com/ahmetb/kubectl-tree / findAPIs

Function findAPIs

cmd/kubectl-tree/apis.go:115–165  ·  view source on GitHub ↗
(client discovery.DiscoveryInterface, apiGroups, resources []string)

Source from the content-addressed store, hash-verified

113}
114
115func findAPIs(client discovery.DiscoveryInterface, apiGroups, resources []string) (*resourceMap, error) {
116 start := time.Now()
117 resList, err := client.ServerPreferredResources()
118 if err != nil {
119 klog.V(1).Infof("failed to fetch api groups from kubernetes: %v\n", err)
120 }
121 klog.V(2).Infof("queried api discovery in %v", time.Since(start))
122 klog.V(3).Infof("found %d items (groups) in server-preferred APIResourceList", len(resList))
123
124 rm := &resourceMap{
125 m: make(resourceNameLookup),
126 }
127 for _, group := range resList {
128 gv, err := schema.ParseGroupVersion(group.GroupVersion)
129 if err != nil {
130 return nil, fmt.Errorf("%q cannot be parsed into groupversion: %w", group.GroupVersion, err)
131 }
132
133 if !matchGroups(apiGroups, gv.Group) {
134 klog.V(5).Infof("ignoring group %s/%s (%d apis)", group.GroupVersion, group.APIVersion, len(group.APIResources))
135 continue
136 }
137 klog.V(5).Infof("iterating over group %s/%s (%d apis)", group.GroupVersion, group.APIVersion, len(group.APIResources))
138
139 for _, apiRes := range group.APIResources {
140 klog.V(5).Infof(" api=%s namespaced=%v", apiRes.Name, apiRes.Namespaced)
141 if !contains(apiRes.Verbs, "list") {
142 klog.V(4).Infof(" api (%s) doesn't have required verb, skipping: %v", apiRes.Name, apiRes.Verbs)
143 continue
144 }
145 // NOTE: if a intermediate owner is excluded that will break the chain, even if the leaf is included
146 // for example --resources=deployments,pods will return nothing because replicasets are not included
147 if !matchResources(resources, apiRes) {
148 klog.V(5).Infof(" api (%s) doesn't match any resource pattern, skipping: %v", apiRes.Name, apiRes.Verbs)
149 continue
150 }
151 v := apiResource{
152 gv: gv,
153 r: apiRes,
154 }
155 names := apiNames(apiRes, gv)
156 klog.V(6).Infof("names: %s", strings.Join(names, ", "))
157 for _, name := range names {
158 rm.m[name] = append(rm.m[name], v)
159 }
160 rm.list = append(rm.list, v)
161 }
162 }
163 klog.V(5).Infof(" found %d apis", len(rm.m))
164 return rm, nil
165}
166
167func contains(v []string, s string) bool {
168 return slices.Contains(v, s)

Callers 1

runFunction · 0.85

Calls 4

matchGroupsFunction · 0.85
containsFunction · 0.85
matchResourcesFunction · 0.85
apiNamesFunction · 0.85

Tested by

no test coverage detected