Skip to content

Commit c0a5f38

Browse files
committed
add api overrides
Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent cd13996 commit c0a5f38

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

cmd/kubectl-tree/apioverrides.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "strings"
4+
5+
// overrideType hardcodes lookup overrides for certain service types
6+
func overrideType(kind string, v *resourceMap) (apiResource, bool) {
7+
kind = strings.ToLower(kind)
8+
9+
switch kind {
10+
case "svc", "service", "services": // Knative also registers "Service", prefer v1.Service
11+
out := v.lookup("service.v1.")
12+
if len(out) != 0 {
13+
return out[0], true
14+
}
15+
16+
case "deploy", "deployment", "deployments": // most clusters will have Deployment in apps/v1 and extensions/v1beta1, extensions/v1/beta2
17+
out := v.lookup("deployment.v1.apps")
18+
if len(out) != 0 {
19+
return out[0], true
20+
}
21+
out = v.lookup("deployment.v1beta1.extensions")
22+
if len(out) != 0 {
23+
return out[0], true
24+
}
25+
}
26+
return apiResource{}, false
27+
}

cmd/kubectl-tree/apis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type resourceNameLookup map[string][]apiResource
2929
type resourceMap struct {
3030
list []apiResource
3131
m resourceNameLookup
32-
} // names to apis binding
32+
}
3333

3434
func (rm *resourceMap) lookup(s string) []apiResource {
3535
return rm.m[strings.ToLower(s)]

cmd/kubectl-tree/rootcmd.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,32 @@ func run(_ *cobra.Command, args []string) error {
6767

6868
kind, name := args[0], args[1]
6969
klog.V(3).Infof("parsed kind=%v name=%v", kind, name)
70-
apiRes := apis.lookup(kind)
71-
klog.V(5).Infof("kind matches=%v", apiRes)
72-
if len(apiRes) == 0 {
73-
return fmt.Errorf("could not find api kind %q", kind)
74-
} else if len(apiRes) > 1 {
75-
names := make([]string, 0, len(apiRes))
76-
for _, a := range apiRes {
77-
names = append(names, fullAPIName(a))
70+
71+
var api apiResource
72+
if k, ok := overrideType(kind, apis); ok {
73+
klog.V(2).Infof("kind=%s override found: %s", k.GroupVersionResource())
74+
api = k
75+
} else {
76+
apiResults := apis.lookup(kind)
77+
klog.V(5).Infof("kind matches=%v", apiResults)
78+
if len(apiResults) == 0 {
79+
return fmt.Errorf("could not find api kind %q", kind)
80+
} else if len(apiResults) > 1 {
81+
names := make([]string, 0, len(apiResults))
82+
for _, a := range apiResults {
83+
names = append(names, fullAPIName(a))
84+
}
85+
return fmt.Errorf("ambiguous kind %q. use one of these as the KIND disambiguate: [%s]", kind,
86+
strings.Join(names, ", "))
7887
}
79-
return fmt.Errorf("ambiguous kind %q. use one of these as the KIND disambiguate: [%s]", kind,
80-
strings.Join(names, ", "))
88+
api = apiResults[0]
8189
}
8290

8391
ns := *cf.Namespace
8492
if ns == "" {
8593
ns = "default" // TODO(ahmetb): how to get current-namespace from kubeconfig?
8694
}
87-
obj, err := dyn.Resource(apiRes[0].GroupVersionResource()).Namespace(ns).Get(name, metav1.GetOptions{})
95+
obj, err := dyn.Resource(api.GroupVersionResource()).Namespace(ns).Get(name, metav1.GetOptions{})
8896
if err != nil {
8997
return fmt.Errorf("failed to get %s/%s: %w", kind, name, err)
9098
}
@@ -100,7 +108,7 @@ func run(_ *cobra.Command, args []string) error {
100108

101109
objs := newObjectDirectory(apiObjects)
102110
if len(objs.ownership[obj.GetUID()]) == 0 {
103-
fmt.Println("No resources are owned by the specified object through ownerReferences.")
111+
fmt.Println("No resources are owned by this object through ownerReferences.")
104112
return nil
105113
}
106114
treeView(os.Stderr, objs, *obj)

cmd/kubectl-tree/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5-
5+
66
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
77
"k8s.io/klog"
88
)

0 commit comments

Comments
 (0)