-
Notifications
You must be signed in to change notification settings - Fork 104
/
inspect_tree_view.go
213 lines (175 loc) · 5.24 KB
/
inspect_tree_view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package tools
import (
"fmt"
"io"
"strings"
"github.com/cppforlife/color"
"github.com/cppforlife/go-cli-ui/ui"
uitable "github.com/cppforlife/go-cli-ui/ui/table"
ctlcap "github.com/k14s/kapp/pkg/kapp/clusterapply"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
)
type InspectTreeView struct {
Source string
Resources []ctlres.Resource
Sort bool
}
func (v InspectTreeView) Print(ui ui.UI) {
groupHeader := uitable.NewHeader("Group")
groupHeader.Hidden = true
versionHeader := uitable.NewHeader("Version")
versionHeader.Hidden = true
reconcileStateHeader := uitable.NewHeader("Reconcile state")
reconcileStateHeader.Title = "Rs"
reconcileInfoHeader := uitable.NewHeader("Reconcile info")
reconcileInfoHeader.Title = "Ri"
table := uitable.Table{
Title: fmt.Sprintf("Resources in %s", v.Source),
Content: "resources",
Header: []uitable.Header{
groupHeader,
uitable.NewHeader("Namespace"),
uitable.NewHeader("Name"),
uitable.NewHeader("Kind"),
versionHeader,
uitable.NewHeader("Owner"),
reconcileStateHeader,
reconcileInfoHeader,
uitable.NewHeader("Age"),
},
FillFirstColumn: true,
SortBy: []uitable.ColumnSort{
{Column: 0, Asc: true},
{Column: 5, Asc: true},
{Column: 1, Asc: true},
},
Notes: []string{"Rs: Reconcile state", "Ri: Reconcile information"},
}
rsByUID := map[string]ctlres.Resource{}
for _, resource := range v.Resources {
rsByUID[resource.UID()] = resource
}
for _, resource := range v.Resources {
prefix := ""
assocSortingVal := newAssocSortingValue(resource, rsByUID)
if assocSortingVal.Depth() > 0 {
prefix = " L" + strings.Repeat("..", assocSortingVal.Depth()-1) + " "
}
row := []uitable.Value{
uitable.NewValueString(assocSortingVal.Value()),
cmdcore.NewValueNamespace(resource.Namespace()),
ValueColored{
// TODO better composability
S: prefix + resource.Name(),
Func: func(str string, opts ...interface{}) string {
result := fmt.Sprintf(str, opts...)
return strings.Replace(result, prefix, color.New(color.Faint).Sprintf("%s", prefix), 1)
},
},
uitable.NewValueString(resource.Kind()),
uitable.NewValueString(resource.APIVersion()),
NewValueResourceOwner(resource),
}
if resource.IsProvisioned() {
syncVal := ctlcap.NewValueResourceConverged(resource)
row = append(row,
syncVal.StateVal,
syncVal.ReasonVal,
cmdcore.NewValueAge(resource.CreatedAt()),
)
} else {
row = append(row,
uitable.NewValueString(""),
uitable.NewValueString(""),
uitable.NewValueString(""),
)
}
table.Rows = append(table.Rows, row)
}
ui.PrintTable(table)
}
type assocSortingValue struct {
resource ctlres.Resource
rsByUID map[string]ctlres.Resource
values []string
}
func newAssocSortingValue(resource ctlres.Resource, rsByUID map[string]ctlres.Resource) *assocSortingValue {
return &assocSortingValue{resource, rsByUID, nil}
}
func (a *assocSortingValue) Value() string {
return strings.Join(a.Values(), "@")
}
func (a *assocSortingValue) Values() []string {
if len(a.values) == 0 {
a.values = a.calculateValue()
}
return a.values
}
func (a *assocSortingValue) calculateValue() []string {
// TODO currently below prefers label based approach
// which potentially misses case when some things are
// labeled and some things are not but both are owner-ref-ed
lblVal := a.labelAssocStr()
if len(lblVal) > 0 {
return []string{lblVal, a.uidOwnersStr()}
}
return []string{a.uidOwnersStr()}
}
func (a *assocSortingValue) labelAssocStr() string {
lblVal := a.resource.Labels()[ctlres.NewAssociationLabel(a.resource).Key()]
if len(lblVal) > 0 {
if a.resource.Transient() {
lblVal = "lbl-" + lblVal + "-2/child" // child
} else {
lblVal = "lbl-" + lblVal + "-1" // parent
}
}
return lblVal
}
func (a *assocSortingValue) uidOwnersStr() string {
identifiers := []string{a.resIdentifier(a.resource)}
nextRes := &a.resource
for nextRes != nil {
res := *nextRes
nextRes = nil
for _, ref := range res.OwnerRefs() {
foundRes, found := a.rsByUID[string(ref.UID)]
if found {
// only nest into first object that we find
identifiers = append([]string{a.resIdentifier(foundRes)}, identifiers...)
nextRes = &foundRes
break
}
}
}
return "ref-" + strings.Join(identifiers, "/")
}
func (a *assocSortingValue) resIdentifier(resource ctlres.Resource) string {
return fmt.Sprintf("%s$%s$%s$%s", resource.Namespace(), resource.APIGroup(), resource.Kind(), resource.Name())
}
func (a *assocSortingValue) Depth() int {
var depth int
for _, val := range a.Values() {
depth = a.max(depth, strings.Count(val, "/"))
}
return depth
}
func (*assocSortingValue) max(a, b int) int {
if a > b {
return a
}
return b
}
type ValueColored struct {
S string
Func func(string, ...interface{}) string
}
func (t ValueColored) String() string { return t.S }
func (t ValueColored) Value() uitable.Value { return t }
func (t ValueColored) Compare(other uitable.Value) int { panic("Never called") }
func (t ValueColored) Fprintf(w io.Writer, pattern string, rest ...interface{}) (int, error) {
return fmt.Fprintf(w, "%s", t.Func(pattern, rest...))
}