Skip to content

Commit 05fb999

Browse files
authored
add support for showing kstatus (#66)
1 parent a074fae commit 05fb999

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

cmd/kubectl-tree/status.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,50 @@ import (
55

66
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
77
"k8s.io/klog"
8+
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
89
)
910

1011
type ReadyStatus string // True False Unknown or ""
1112
type Reason string
1213

13-
func extractStatus(obj unstructured.Unstructured) (ReadyStatus, Reason) {
14+
func extractStatus(obj unstructured.Unstructured) (ReadyStatus, Reason, status.Status) {
1415
jsonVal, _ := json.Marshal(obj.Object["status"])
1516
klog.V(6).Infof("status for object=%s/%s: %s", obj.GetKind(), obj.GetName(), string(jsonVal))
17+
result, err := status.Compute(&obj)
18+
if err != nil {
19+
return "", "", ""
20+
}
1621
statusF, ok := obj.Object["status"]
1722
if !ok {
18-
return "", ""
23+
return "", "", ""
1924
}
2025
statusV, ok := statusF.(map[string]interface{})
2126
if !ok {
22-
return "", ""
27+
return "", "", ""
2328
}
2429
conditionsF, ok := statusV["conditions"]
2530
if !ok {
26-
return "", ""
31+
return "", "", ""
2732
}
2833
conditionsV, ok := conditionsF.([]interface{})
2934
if !ok {
30-
return "", ""
35+
return "", "", ""
3136
}
3237

3338
for _, cond := range conditionsV {
3439
condM, ok := cond.(map[string]interface{})
3540
if !ok {
36-
return "", ""
41+
return "", "", ""
3742
}
3843
condType, ok := condM["type"].(string)
3944
if !ok {
40-
return "", ""
45+
return "", "", ""
4146
}
4247
if condType == "Ready" {
4348
condStatus, _ := condM["status"].(string)
4449
condReason, _ := condM["reason"].(string)
45-
return ReadyStatus(condStatus), Reason(condReason)
50+
return ReadyStatus(condStatus), Reason(condReason), status.Status(result.Status.String())
4651
}
4752
}
48-
return "", ""
53+
return "", "", ""
4954
}

cmd/kubectl-tree/tree.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/gosuri/uitable"
1111
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1212
"k8s.io/apimachinery/pkg/util/duration"
13+
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
1314
)
1415

1516
const (
@@ -20,22 +21,23 @@ const (
2021
)
2122

2223
var (
23-
gray = color.New(color.FgHiBlack)
24-
red = color.New(color.FgRed)
25-
green = color.New(color.FgGreen)
24+
gray = color.New(color.FgHiBlack)
25+
red = color.New(color.FgRed)
26+
yellow = color.New(color.FgYellow)
27+
green = color.New(color.FgGreen)
2628
)
2729

2830
// treeView prints object hierarchy to out stream.
2931
func treeView(out io.Writer, objs objectDirectory, obj unstructured.Unstructured) {
3032
tbl := uitable.New()
3133
tbl.Separator = " "
32-
tbl.AddRow("NAMESPACE", "NAME", "READY", "REASON", "AGE")
34+
tbl.AddRow("NAMESPACE", "NAME", "READY", "REASON", "STATUS", "AGE")
3335
treeViewInner("", tbl, objs, obj)
3436
fmt.Fprintln(color.Output, tbl)
3537
}
3638

3739
func treeViewInner(prefix string, tbl *uitable.Table, objs objectDirectory, obj unstructured.Unstructured) {
38-
ready, reason := extractStatus(obj)
40+
ready, reason, kstatus := extractStatus(obj)
3941

4042
var readyColor *color.Color
4143
switch ready {
@@ -50,6 +52,21 @@ func treeViewInner(prefix string, tbl *uitable.Table, objs objectDirectory, obj
5052
ready = "-"
5153
}
5254

55+
var statusColor *color.Color
56+
switch kstatus {
57+
case status.CurrentStatus:
58+
statusColor = green
59+
case status.InProgressStatus:
60+
statusColor = yellow
61+
case status.FailedStatus, status.TerminatingStatus:
62+
statusColor = red
63+
default:
64+
statusColor = gray
65+
}
66+
if kstatus == "" {
67+
kstatus = "-"
68+
}
69+
5370
c := obj.GetCreationTimestamp()
5471
age := duration.HumanDuration(time.Since(c.Time))
5572
if c.IsZero() {
@@ -62,6 +79,7 @@ func treeViewInner(prefix string, tbl *uitable.Table, objs objectDirectory, obj
6279
color.New(color.Bold).Sprint(obj.GetName())),
6380
readyColor.Sprint(ready),
6481
readyColor.Sprint(reason),
82+
statusColor.Sprint(kstatus),
6583
age)
6684
chs := objs.ownedBy(obj.GetUID())
6785
for i, child := range chs {

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ go 1.20
55
require (
66
github.com/fatih/color v1.15.0
77
github.com/gosuri/uitable v0.0.4
8+
github.com/pkg/errors v0.9.1
89
github.com/spf13/cobra v1.7.0
910
github.com/spf13/pflag v1.0.5
1011
k8s.io/apimachinery v0.27.4
1112
k8s.io/cli-runtime v0.27.4
1213
k8s.io/client-go v0.27.4
1314
k8s.io/klog v1.0.0
15+
sigs.k8s.io/cli-utils v0.35.0
1416
)
1517

1618
require (
@@ -31,7 +33,7 @@ require (
3133
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
3234
github.com/google/uuid v1.3.0 // indirect
3335
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
34-
github.com/imdario/mergo v0.3.6 // indirect
36+
github.com/imdario/mergo v0.3.12 // indirect
3537
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3638
github.com/josharian/intern v1.0.0 // indirect
3739
github.com/json-iterator/go v1.1.12 // indirect
@@ -45,7 +47,6 @@ require (
4547
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
4648
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4749
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
48-
github.com/pkg/errors v0.9.1 // indirect
4950
github.com/rivo/uniseg v0.2.0 // indirect
5051
github.com/xlab/treeprint v1.1.0 // indirect
5152
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
@@ -54,7 +55,7 @@ require (
5455
golang.org/x/sys v0.6.0 // indirect
5556
golang.org/x/term v0.6.0 // indirect
5657
golang.org/x/text v0.8.0 // indirect
57-
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
58+
golang.org/x/time v0.3.0 // indirect
5859
google.golang.org/appengine v1.6.7 // indirect
5960
google.golang.org/protobuf v1.28.1 // indirect
6061
gopkg.in/inf.v0 v0.9.1 // indirect

go.sum

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:Fecb
142142
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
143143
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
144144
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
145-
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
146-
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
145+
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
146+
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
147147
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
148148
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
149149
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -352,8 +352,8 @@ golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
352352
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
353353
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
354354
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
355-
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44=
356-
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
355+
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
356+
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
357357
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
358358
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
359359
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
@@ -490,6 +490,7 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
490490
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
491491
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
492492
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
493+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
493494
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
494495
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
495496
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
@@ -522,6 +523,8 @@ k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt
522523
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
523524
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
524525
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
526+
sigs.k8s.io/cli-utils v0.35.0 h1:dfSJaF1W0frW74PtjwiyoB4cwdRygbHnC7qe7HF0g/Y=
527+
sigs.k8s.io/cli-utils v0.35.0/go.mod h1:ITitykCJxP1vaj1Cew/FZEaVJ2YsTN9Q71m02jebkoE=
525528
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
526529
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
527530
sigs.k8s.io/kustomize/api v0.13.2 h1:kejWfLeJhUsTGioDoFNJET5LQe/ajzXhJGYoU+pJsiA=

0 commit comments

Comments
 (0)