Skip to content

Commit cd13996

Browse files
committed
add AGE column
Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent c453552 commit cd13996

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

cmd/kubectl-tree/apis.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
44
"fmt"
5+
"strings"
6+
"time"
7+
58
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
69
"k8s.io/apimachinery/pkg/runtime/schema"
710
"k8s.io/client-go/discovery"
811
"k8s.io/klog"
9-
"strings"
10-
"time"
1112
)
1213

1314
type apiResource struct {

cmd/kubectl-tree/query.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
44
"fmt"
5+
"sync"
6+
"time"
7+
58
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
69
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
710
"k8s.io/client-go/dynamic"
811
"k8s.io/klog"
9-
"sync"
10-
"time"
1112
)
1213

1314
// getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).

cmd/kubectl-tree/relationship.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package main
22

33
import (
4+
"sort"
5+
46
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
57
"k8s.io/apimachinery/pkg/types"
6-
"sort"
78
)
89

910
// objectDirectory stores objects and owner relationships between them.

cmd/kubectl-tree/rootcmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ package main
1818
import (
1919
"flag"
2020
"fmt"
21+
"os"
22+
"strings"
23+
2124
"github.com/spf13/cobra"
2225
"github.com/spf13/pflag"
2326
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
"k8s.io/cli-runtime/pkg/genericclioptions"
2528
"k8s.io/client-go/dynamic"
26-
"k8s.io/klog"
27-
"os"
28-
"strings"
29-
3029
_ "k8s.io/client-go/plugin/pkg/client/auth" // combined authprovider import
30+
"k8s.io/klog"
3131
)
3232

3333
var cf *genericclioptions.ConfigFlags

cmd/kubectl-tree/status.go

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

33
import (
44
"encoding/json"
5+
56
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
67
"k8s.io/klog"
78
)

cmd/kubectl-tree/tree.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package main
22

33
import (
44
"fmt"
5+
"io"
6+
"strings"
7+
"time"
8+
59
"github.com/fatih/color"
610
"github.com/gosuri/uitable"
7-
"io"
811
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9-
"strings"
12+
"k8s.io/apimachinery/pkg/util/duration"
1013
)
1114

1215
const (
@@ -26,7 +29,7 @@ var (
2629
func treeView(out io.Writer, objs objectDirectory, obj unstructured.Unstructured) {
2730
tbl := uitable.New()
2831
tbl.Separator = " "
29-
tbl.AddRow("NAMESPACE", "NAME", "READY", "REASON")
32+
tbl.AddRow("NAMESPACE", "NAME", "READY", "REASON", "AGE")
3033
treeViewInner("", tbl, objs, obj)
3134
fmt.Fprintln(out, tbl)
3235
}
@@ -47,12 +50,19 @@ func treeViewInner(prefix string, tbl *uitable.Table, objs objectDirectory, obj
4750
ready = "-"
4851
}
4952

53+
c := obj.GetCreationTimestamp()
54+
age := duration.HumanDuration(time.Since(c.Time))
55+
if c.IsZero() {
56+
age = "<unknown>"
57+
}
58+
5059
tbl.AddRow(obj.GetNamespace(), fmt.Sprintf("%s%s/%s",
5160
gray.Sprint(printPrefix(prefix)),
5261
obj.GetKind(),
5362
color.New(color.Bold).Sprint(obj.GetName())),
5463
readyColor.Sprint(ready),
55-
readyColor.Sprint(reason))
64+
readyColor.Sprint(reason),
65+
age)
5666
chs := objs.ownedBy(obj.GetUID())
5767
for i, child := range chs {
5868
var p string

0 commit comments

Comments
 (0)