-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.go
244 lines (228 loc) · 5.43 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"sort"
"strings"
"unicode"
"gopkg.in/yaml.v2"
)
func main() {
err := run(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func run(args []string) error {
if len(args) == 0 {
return mutateYaml(os.Stdin, os.Stdout)
}
for _, arg := range args {
f, err := os.Open(arg) // #nosec G304 comes from user input
if err != nil {
return err
}
defer func() {
_ = f.Close()
}() // #nosec G307 we cannot handle this error in other way then just simply ignore it
var buf bytes.Buffer
err = mutateYaml(f, &buf)
if err != nil {
return err
}
err = ioutil.WriteFile(arg, buf.Bytes(), 0644) // #nosec mimics original file permissions from kubebuilder
if err != nil {
return err
}
}
return nil
}
func mutateYaml(r io.Reader, w io.Writer) error {
var yamlData yaml.MapSlice
err := yaml.NewDecoder(r).Decode(&yamlData)
if err != nil {
return err
}
removeValueType(&yamlData, false)
versions := getVersions(yamlData)
setVersion(&yamlData, versions[len(versions)-1], 0)
reorderVersions(&yamlData, versions, false)
return yaml.NewEncoder(w).Encode(yamlData)
}
func removeValueType(v interface{}, inValue bool) (remove bool) {
d, set := ptrSetter(v)
switch d := d.(type) {
case []interface{}:
newSlice := make([]interface{}, 0, len(d))
for ix := range d {
item := d[ix]
shouldRemove := removeValueType(&item, inValue)
if !shouldRemove {
newSlice = append(newSlice, item)
}
}
set(newSlice)
return false
case yaml.MapSlice:
newSlice := make(yaml.MapSlice, 0, len(d))
for ix := range d {
item := d[ix]
shouldRemove := removeValueType(&item, inValue)
if !shouldRemove {
newSlice = append(newSlice, item)
}
}
set(newSlice)
return false
case yaml.MapItem:
if inValue && d.Key == "type" && d.Value == "object" {
return true
}
b := removeValueType(&d.Value, inValue || d.Key == "value")
set(d)
return b
default:
return false
}
}
func ptrSetter(v interface{}) (value interface{}, setPtr func(interface{})) {
val := reflect.ValueOf(v).Elem()
return val.Interface(), func(newValue interface{}) {
val.Set(reflect.ValueOf(newValue))
}
}
func setVersion(v interface{}, version string, depth int) {
const maxDepth = 3
if depth > maxDepth {
return
}
depth++
d, set := ptrSetter(v)
switch d := d.(type) {
case []interface{}:
newSlice := make([]interface{}, 0, len(d))
for ix := range d {
item := d[ix]
setVersion(&item, version, depth)
newSlice = append(newSlice, item)
}
set(newSlice)
case yaml.MapSlice:
newSlice := make(yaml.MapSlice, 0, len(d))
for ix := range d {
item := d[ix]
setVersion(&item, version, depth)
newSlice = append(newSlice, item)
}
set(newSlice)
case yaml.MapItem:
setVersion(&d.Value, version, depth)
if d.Key == "version" {
d.Value = version
}
set(d)
}
}
func getVersions(v interface{}) []string {
versions := recursiveGetVersions(v, false)
// sort versions lowest to highest. i.e. v1alpha1, v1beta1, v1beta2, v1
sort.Slice(versions, func(a, b int) bool {
strA := versions[a]
strB := versions[b]
// artificially sort 'v1' as greater than 'v1alpha1'. this works because '~' > 'a-zA-Z'
if isDigits(strings.TrimPrefix(strA, "v")) {
strA += "~"
}
if isDigits(strings.TrimPrefix(strB, "v")) {
strB += "~"
}
return strA < strB
})
return versions
}
func recursiveGetVersions(v interface{}, foundVersions bool) (versions []string) {
switch d := v.(type) {
case []interface{}:
for ix := range d {
item := d[ix]
versions = append(versions, recursiveGetVersions(item, foundVersions)...)
}
case yaml.MapSlice:
for ix := range d {
item := d[ix]
versions = append(versions, recursiveGetVersions(item, foundVersions)...)
}
case yaml.MapItem:
if d.Key == "versions" {
return recursiveGetVersions(d.Value, true)
}
if !foundVersions {
return append(versions, recursiveGetVersions(d.Value, foundVersions)...)
}
if d.Key != "name" {
return
}
return append(versions, d.Value.(string))
}
return
}
func isDigits(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
// reorderVersions sorts the CRD versions from newest to oldest ('versions' is sorted oldest to newest so range over it backwards)
func reorderVersions(v interface{}, versions []string, foundVersions bool) {
d, set := ptrSetter(v)
switch d := d.(type) {
case []interface{}:
if foundVersions {
versionMap := make(map[string]interface{})
for ix := range d {
item := d[ix]
for _, kv := range item.(yaml.MapSlice) {
if kv.Key == "name" {
versionMap[kv.Value.(string)] = &item
break
}
}
}
newSlice := make([]interface{}, 0, len(d))
for ix := range versions {
fromEnd := versions[len(versions)-1-ix]
newSlice = append(newSlice, versionMap[fromEnd])
}
set(newSlice)
return
}
newSlice := make([]interface{}, 0, len(d))
for ix := range d {
item := d[ix]
reorderVersions(&item, versions, foundVersions)
newSlice = append(newSlice, item)
}
set(newSlice)
case yaml.MapSlice:
newSlice := make(yaml.MapSlice, 0, len(d))
for ix := range d {
item := d[ix]
reorderVersions(&item, versions, foundVersions)
newSlice = append(newSlice, item)
}
set(newSlice)
case yaml.MapItem:
if d.Key == "versions" {
foundVersions = true
}
reorderVersions(&d.Value, versions, foundVersions)
set(d)
}
}