-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (153 loc) · 3.46 KB
/
main.go
File metadata and controls
176 lines (153 loc) · 3.46 KB
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
package main
import (
"io"
"log"
"os"
"strings"
"flag"
"gopkg.in/yaml.v3"
)
type fixer struct {
decoder *yaml.Decoder
encoder *yaml.Encoder
fromApiGroup string
toApiGroup string
}
func newFixer(in io.Reader, out io.Writer) *fixer {
return &fixer{
decoder: yaml.NewDecoder(in),
encoder: yaml.NewEncoder(out),
}
}
func (f *fixer) run() error {
for {
var rawDocument interface{}
err := f.decoder.Decode(&rawDocument)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
apiVersion := findTextChild(rawDocument, "apiVersion")
kind := findTextChild(rawDocument, "kind")
if apiVersion == "apiextensions.k8s.io/v1beta1" && kind == "CustomResourceDefinition" {
if err = f.convertCrd(rawDocument); err != nil {
return err
}
} else if apiVersion == "rbac.authorization.k8s.io/v1" && (kind == "ClusterRole" || kind == "Role") {
if err = f.convertRole(rawDocument); err != nil {
return err
}
} else if strings.HasPrefix(apiVersion, f.fromApiGroup+"/") {
suffix := apiVersion[len(f.fromApiGroup+"/"):]
rawDocument.(map[string]interface{})["apiVersion"] = f.toApiGroup + "/" + suffix
}
if err = f.encoder.Encode(&rawDocument); err != nil {
return err
}
}
}
func (f *fixer) convertCrd(rawDocument interface{}) error {
metadata := findMapChild(rawDocument, "metadata")
if metadata != nil {
name := findTextChild(metadata, "name")
suffix := "." + f.fromApiGroup
if strings.HasSuffix(name, suffix) {
metadata["name"] = name[0:len(name)-len(suffix)] + "." + f.toApiGroup
}
}
spec := findMapChild(rawDocument, "spec")
if spec != nil {
if findTextChild(spec, "group") == f.fromApiGroup {
spec["group"] = f.toApiGroup
}
}
return nil
}
func (f *fixer) convertRole(rawDocument interface{}) error {
rules := findSequenceChild(rawDocument, "rules")
if rules == nil {
return nil
}
for _, rule := range rules {
apiGroups := findSequenceChild(rule, "apiGroups")
if apiGroups != nil {
for index, apiGroup := range apiGroups {
s, ok := apiGroup.(string)
if ok && s == f.fromApiGroup {
apiGroups[index] = f.toApiGroup
}
}
}
}
return nil
}
func findChild(node interface{}, name string) interface{} {
o, ok := node.(map[string]interface{})
if !ok {
return nil
}
for key, value := range o {
if key == name {
return value
}
}
return nil
}
func findTextChild(node interface{}, name string) string {
child := findChild(node, name)
if child == nil {
return ""
}
s, ok := child.(string)
if !ok {
return ""
}
return s
}
func findSequenceChild(node interface{}, name string) []interface{} {
child := findChild(node, name)
if child == nil {
return nil
}
a, ok := child.([]interface{})
if !ok {
return nil
}
return a
}
func findMapChild(node interface{}, name string) map[string]interface{} {
child := findChild(node, name)
if child == nil {
return nil
}
m, ok := child.(map[string]interface{})
if !ok {
return nil
}
return m
}
func main() {
var in io.Reader
var out io.Writer
var err error
in = os.Stdin
out = os.Stdout
fromApiGroup := flag.String("from", "", "The API Group value to change from")
toApiGroup := flag.String("to", "", "The API Group value to change to")
flag.Parse()
if *fromApiGroup == "" {
log.Fatal("Must supply -from option")
}
if *toApiGroup == "" {
log.Fatal("Must supply -to option")
}
f := newFixer(in, out)
f.fromApiGroup = *fromApiGroup
f.toApiGroup = *toApiGroup
err = f.run()
if err != nil {
log.Fatal(err)
}
}