-
Notifications
You must be signed in to change notification settings - Fork 104
/
changes_view.go
276 lines (227 loc) · 7.51 KB
/
changes_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package clusterapply
import (
"fmt"
"strings"
"github.com/cppforlife/go-cli-ui/ui"
uitable "github.com/cppforlife/go-cli-ui/ui/table"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/mitchellh/go-wordwrap"
)
type ChangeView interface {
Resource() ctlres.Resource
ExistingResource() ctlres.Resource
ApplyOp() ClusterChangeApplyOp
ApplyStrategyOp() (ClusterChangeApplyStrategyOp, error)
WaitOp() ClusterChangeWaitOp
ConfigurableTextDiff() *ctldiff.ConfigurableTextDiff
}
type ChangesView struct {
ChangeViews []ChangeView
Sort bool
countsView *ChangesCountsView
}
func (v *ChangesView) Print(ui ui.UI) {
versionHeader := uitable.NewHeader("Version")
versionHeader.Hidden = true
reconcileStateHeader := uitable.NewHeader("Reconcile state")
reconcileStateHeader.Title = "Rs"
reconcileInfoHeader := uitable.NewHeader("Reconcile info")
reconcileInfoHeader.Title = "Ri"
opStrategyHeader := uitable.NewHeader("Op strategy")
opStrategyHeader.Title = "Op st."
table := uitable.Table{
Title: "Changes",
// TODO do not show total number of "changes" as it may
// be confusing that some changes are only waits
// Content: "changes",
Header: []uitable.Header{
uitable.NewHeader("Namespace"),
uitable.NewHeader("Name"),
uitable.NewHeader("Kind"),
versionHeader,
uitable.NewHeader("Age"),
uitable.NewHeader("Op"),
opStrategyHeader,
uitable.NewHeader("Wait to"),
reconcileStateHeader,
reconcileInfoHeader,
},
}
if v.Sort {
table.SortBy = []uitable.ColumnSort{
{Column: 0, Asc: true},
{Column: 1, Asc: true},
{Column: 2, Asc: true},
{Column: 3, Asc: true},
}
} else {
// Otherwise it might look very awkward
table.FillFirstColumn = true
}
for _, view := range v.ChangeViews {
resource := view.Resource()
v.countsView.Add(view.ApplyOp(), view.WaitOp())
row := []uitable.Value{
cmdcore.NewValueNamespace(resource.Namespace()),
uitable.NewValueString(resource.Name()),
uitable.NewValueString(resource.Kind()),
uitable.NewValueString(resource.APIVersion()),
}
if resource.IsProvisioned() {
row = append(row,
cmdcore.NewValueAge(resource.CreatedAt()),
)
} else {
row = append(row,
uitable.NewValueString(""),
)
}
row = append(row,
v.applyOpCode(view.ApplyOp()),
v.applyStrategyOpCode(view),
v.waitOpCode(view.WaitOp()),
)
if view.ExistingResource() != nil {
syncVal := NewValueResourceConverged(view.ExistingResource())
row = append(row, syncVal.StateVal, syncVal.ReasonVal)
} else {
row = append(row,
uitable.NewValueString(""),
uitable.NewValueString(""),
)
}
table.Rows = append(table.Rows, row)
}
table.Notes = append(table.Notes, v.countsView.Strings(true)...)
ui.PrintTable(table)
}
func (v *ChangesView) Summary() string { return v.countsView.String() }
var (
applyOpCodeUI = map[ClusterChangeApplyOp]string{
ClusterChangeApplyOpAdd: "create",
ClusterChangeApplyOpDelete: "delete",
ClusterChangeApplyOpUpdate: "update",
ClusterChangeApplyOpNoop: "noop",
ClusterChangeApplyOpExists: "exists",
}
applyStrategyCodeUI = map[ClusterChangeApplyOp]map[ClusterChangeApplyStrategyOp]string{
ClusterChangeApplyOpAdd: {
createStrategyPlainAnnValue: "",
createStrategyFallbackOnUpdateAnnValue: "fallback on update",
},
ClusterChangeApplyOpUpdate: {
updateStrategyPlainAnnValue: "",
updateStrategyFallbackOnReplaceAnnValue: "fallback on replace",
updateStrategyAlwaysReplaceAnnValue: "always replace",
updateStrategySkipAnnValue: "skip",
},
ClusterChangeApplyOpDelete: {
deleteStrategyPlainAnnValue: "",
deleteStrategyOrphanAnnValue: "orphan",
},
ClusterChangeApplyOpNoop: {
noopStrategyOp: "",
},
ClusterChangeApplyOpExists: {
noopStrategyOp: "",
},
}
waitOpCodeUI = map[ClusterChangeWaitOp]string{
ClusterChangeWaitOpOK: "reconcile",
ClusterChangeWaitOpDelete: "delete",
ClusterChangeWaitOpNoop: "noop",
}
)
func (v *ChangesView) applyOpCode(op ClusterChangeApplyOp) uitable.Value {
switch op {
case ClusterChangeApplyOpAdd:
return uitable.ValueFmt{V: uitable.NewValueString(applyOpCodeUI[op]), Error: false}
case ClusterChangeApplyOpDelete:
return uitable.ValueFmt{V: uitable.NewValueString(applyOpCodeUI[op]), Error: true}
case ClusterChangeApplyOpUpdate:
return uitable.ValueFmt{V: uitable.NewValueString(applyOpCodeUI[op]), Error: false}
case ClusterChangeApplyOpExists:
return uitable.ValueFmt{V: uitable.NewValueString(applyOpCodeUI[op]), Error: false}
case ClusterChangeApplyOpNoop:
return uitable.NewValueString("")
default:
return uitable.NewValueString("???")
}
}
func (v *ChangesView) applyStrategyOpCode(view ChangeView) uitable.Value {
strategyOp, err := view.ApplyStrategyOp()
if err == nil {
if codeUIs, found := applyStrategyCodeUI[view.ApplyOp()]; found {
if codeUI, found := codeUIs[strategyOp]; found {
return uitable.NewValueString(codeUI)
}
}
}
return uitable.NewValueString("???")
}
func (v *ChangesView) waitOpCode(op ClusterChangeWaitOp) uitable.Value {
switch op {
case ClusterChangeWaitOpOK:
return uitable.NewValueString(waitOpCodeUI[op]) // TODO highlight for apply op noop?
case ClusterChangeWaitOpDelete:
return uitable.NewValueString(waitOpCodeUI[op])
case ClusterChangeWaitOpNoop:
return uitable.NewValueString("")
default:
return uitable.NewValueString("???")
}
}
type ChangesCountsView struct {
applyOps map[ClusterChangeApplyOp]int
waitOps map[ClusterChangeWaitOp]int
}
func NewChangesCountsView() *ChangesCountsView {
return &ChangesCountsView{map[ClusterChangeApplyOp]int{}, map[ClusterChangeWaitOp]int{}}
}
func (v *ChangesCountsView) Add(applyOp ClusterChangeApplyOp, waitOp ClusterChangeWaitOp) {
v.applyOps[applyOp]++
v.waitOps[waitOp]++
}
func (v *ChangesCountsView) Strings(table bool) []string {
applyOpsStats := []string{}
visibleApplyOps := []ClusterChangeApplyOp{
ClusterChangeApplyOpAdd, ClusterChangeApplyOpDelete, ClusterChangeApplyOpUpdate, ClusterChangeApplyOpNoop, ClusterChangeApplyOpExists}
for _, op := range visibleApplyOps {
applyOpsStats = append(applyOpsStats, fmt.Sprintf("%d %s", v.applyOps[op], applyOpCodeUI[op]))
}
waitsOpStats := []string{}
visibleWaitOps := []ClusterChangeWaitOp{
ClusterChangeWaitOpOK, ClusterChangeWaitOpDelete, ClusterChangeWaitOpNoop}
for _, op := range visibleWaitOps {
waitsOpStats = append(waitsOpStats, fmt.Sprintf("%d %s", v.waitOps[op], waitOpCodeUI[op]))
}
padding := ""
if table {
padding = " "
}
return []string{
"Op: " + padding + strings.Join(applyOpsStats, ", "),
"Wait to: " + strings.Join(waitsOpStats, ", "),
}
}
func (v *ChangesCountsView) String() string {
return strings.Join(v.Strings(false), " / ")
}
type ValueResourceConverged struct {
StateVal uitable.Value
ReasonVal uitable.Value
}
func NewValueResourceConverged(resource ctlres.Resource) ValueResourceConverged {
// TODO how to retrieve waiting rules
convergedResFactory := NewConvergedResourceFactory(nil, ConvergedResourceFactoryOpts{})
// TODO state vs err vs output
state, _, err := convergedResFactory.New(resource, nil).IsDoneApplying()
stateUI := NewDoneApplyStateUI(state, err)
stateVal := uitable.ValueFmt{V: uitable.NewValueString(stateUI.State), Error: stateUI.Error}
reasonVal := uitable.NewValueString(wordwrap.WrapString(stateUI.Message, 35))
return ValueResourceConverged{stateVal, reasonVal}
}