This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dryrun_target.go
300 lines (253 loc) · 7.21 KB
/
dryrun_target.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package fi
import (
"fmt"
"bytes"
"github.com/golang/glog"
"io"
"k8s.io/kops/upup/pkg/fi/utils"
"reflect"
"strings"
)
// DryRunTarget is a special Target that does not execute anything, but instead tracks all changes.
// By running against a DryRunTarget, a list of changes that would be made can be easily collected,
// without any special support from the Tasks.
type DryRunTarget struct {
changes []*render
deletions []Deletion
// The destination to which the final report will be printed on Finish()
out io.Writer
}
type render struct {
a Task
aIsNil bool
e Task
changes Task
}
var _ Target = &DryRunTarget{}
func NewDryRunTarget(out io.Writer) *DryRunTarget {
t := &DryRunTarget{}
t.out = out
return t
}
func (t *DryRunTarget) Render(a, e, changes Task) error {
valA := reflect.ValueOf(a)
aIsNil := valA.IsNil()
t.changes = append(t.changes, &render{
a: a,
aIsNil: aIsNil,
e: e,
changes: changes,
})
return nil
}
func (t *DryRunTarget) Delete(deletion Deletion) error {
t.deletions = append(t.deletions, deletion)
return nil
}
func IdForTask(taskMap map[string]Task, t Task) string {
for k, v := range taskMap {
if v == t {
return k
}
}
glog.Fatalf("unknown task: %v", t)
return "?"
}
func (t *DryRunTarget) PrintReport(taskMap map[string]Task, out io.Writer) error {
b := &bytes.Buffer{}
if len(t.changes) != 0 {
var creates []*render
var updates []*render
for _, r := range t.changes {
if r.aIsNil {
creates = append(creates, r)
} else {
updates = append(updates, r)
}
}
if len(creates) != 0 {
fmt.Fprintf(b, "Will create resources:\n")
for _, r := range creates {
taskName := getTaskName(r.changes)
fmt.Fprintf(b, " %-20s\t%s\n", taskName, IdForTask(taskMap, r.e))
changes := reflect.ValueOf(r.changes)
if changes.Kind() == reflect.Ptr && !changes.IsNil() {
changes = changes.Elem()
}
if changes.Kind() == reflect.Struct {
for i := 0; i < changes.NumField(); i++ {
field := changes.Field(i)
fieldName := changes.Type().Field(i).Name
fieldValue := ValueAsString(field)
// The field name is already printed above, no need to repeat it.
// Additionally, ignore any output that is not informative
if fieldName != "Name" && fieldValue != "<nil>" && fieldValue != "id:<nil>" && fieldValue != "<resource>" {
fmt.Fprintf(b, " \t%-20s\t%s\n", fieldName, fieldValue)
}
}
}
fmt.Fprintf(b, "\n")
}
}
if len(updates) != 0 {
fmt.Fprintf(b, "Will modify resources:\n")
// We can't use our reflection helpers here - we want corresponding values from a,e,c
for _, r := range updates {
var changeList []string
valC := reflect.ValueOf(r.changes)
valA := reflect.ValueOf(r.a)
valE := reflect.ValueOf(r.e)
if valC.Kind() == reflect.Ptr && !valC.IsNil() {
valC = valC.Elem()
}
if valA.Kind() == reflect.Ptr && !valA.IsNil() {
valA = valA.Elem()
}
if valE.Kind() == reflect.Ptr && !valE.IsNil() {
valE = valE.Elem()
}
if valC.Kind() == reflect.Struct {
for i := 0; i < valC.NumField(); i++ {
fieldValC := valC.Field(i)
changed := true
switch fieldValC.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map:
changed = !fieldValC.IsNil()
case reflect.String:
changed = fieldValC.Interface().(string) != ""
}
if !changed {
continue
}
if fieldValC.Kind() == reflect.String && fieldValC.Interface().(string) == "" {
// No change
continue
}
fieldValE := valE.Field(i)
description := ""
ignored := false
if fieldValE.CanInterface() {
fieldValA := valA.Field(i)
switch fieldValE.Interface().(type) {
//case SimpleUnit:
// ignored = true
default:
description = fmt.Sprintf(" %v -> %v", ValueAsString(fieldValA), ValueAsString(fieldValE))
}
}
if ignored {
continue
}
changeList = append(changeList, fmt.Sprintf("%-20s\t%s", valC.Type().Field(i).Name, description))
}
} else {
return fmt.Errorf("unhandled change type: %v", valC.Type())
}
if len(changeList) == 0 {
continue
}
taskName := getTaskName(r.changes)
fmt.Fprintf(b, " %-20s\t%s\n", taskName, IdForTask(taskMap, r.e))
for _, f := range changeList {
fmt.Fprintf(b, " \t%s\n", f)
}
fmt.Fprintf(b, "\n")
}
}
}
if len(t.deletions) != 0 {
fmt.Fprintf(b, "Will delete items:\n")
for _, d := range t.deletions {
fmt.Fprintf(b, " %-20s %s\n", d.TaskName(), d.Item())
}
}
_, err := out.Write(b.Bytes())
return err
}
func getTaskName(t Task) string {
s := fmt.Sprintf("%T", t)
lastDot := strings.LastIndexByte(s, '.')
if lastDot != -1 {
s = s[lastDot+1:]
}
return s
}
// asString returns a human-readable string representation of the passed value
func ValueAsString(value reflect.Value) string {
b := &bytes.Buffer{}
walker := func(path string, field *reflect.StructField, v reflect.Value) error {
if utils.IsPrimitiveValue(v) || v.Kind() == reflect.String {
fmt.Fprintf(b, "%v", v.Interface())
return utils.SkipReflection
}
switch v.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map:
if v.IsNil() {
fmt.Fprintf(b, "<nil>")
return utils.SkipReflection
}
}
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return nil // descend into value
case reflect.Slice:
len := v.Len()
fmt.Fprintf(b, "[")
for i := 0; i < len; i++ {
av := v.Index(i)
if i != 0 {
fmt.Fprintf(b, ", ")
}
fmt.Fprintf(b, "%s", ValueAsString(av))
}
fmt.Fprintf(b, "]")
return utils.SkipReflection
case reflect.Map:
keys := v.MapKeys()
fmt.Fprintf(b, "{")
for i, key := range keys {
mv := v.MapIndex(key)
if i != 0 {
fmt.Fprintf(b, ", ")
}
fmt.Fprintf(b, "%s: %s", ValueAsString(key), ValueAsString(mv))
}
fmt.Fprintf(b, "}")
return utils.SkipReflection
case reflect.Struct:
intf := v.Addr().Interface()
if _, ok := intf.(Resource); ok {
fmt.Fprintf(b, "<resource>")
} else if _, ok := intf.(*ResourceHolder); ok {
fmt.Fprintf(b, "<resource>")
} else if compareWithID, ok := intf.(CompareWithID); ok {
id := compareWithID.CompareWithID()
if id == nil {
fmt.Fprintf(b, "id:<nil>")
} else {
fmt.Fprintf(b, "id:%s", *id)
}
} else {
glog.V(4).Infof("Unhandled kind in asString for %q: %T", path, v.Interface())
fmt.Fprint(b, DebugAsJsonString(intf))
}
return utils.SkipReflection
default:
glog.Infof("Unhandled kind in asString for %q: %T", path, v.Interface())
return fmt.Errorf("Unhandled kind for %q: %v", path, v.Kind())
}
}
err := utils.ReflectRecursive(value, walker)
if err != nil {
glog.Fatalf("unexpected error during reflective walk: %v", err)
}
return b.String()
}
// Finish is called at the end of a run, and prints a list of changes to the configured Writer
func (t *DryRunTarget) Finish(taskMap map[string]Task) error {
return t.PrintReport(taskMap, t.out)
}
// HasChanges returns true iff any changes would have been made
func (t *DryRunTarget) HasChanges() bool {
return len(t.changes)+len(t.deletions) != 0
}