-
Notifications
You must be signed in to change notification settings - Fork 88
/
root.go
201 lines (168 loc) · 7.27 KB
/
root.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
package cli
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
extensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
extensionsscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
"k8s.io/client-go/kubernetes/scheme"
)
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "schemagen",
Short: "Generate openapischemas for the kinds in this project",
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
return generateSchemas(v)
},
}
cobra.OnInitialize(initConfig)
cmd.Flags().String("output-dir", "./schemas", "directory to save the schemas in")
viper.BindPFlags(cmd.Flags())
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return cmd
}
func InitAndExecute() {
if err := RootCmd().Execute(); err != nil {
os.Exit(1)
}
}
func initConfig() {
viper.SetEnvPrefix("TROUBLESHOOT")
viper.AutomaticEnv()
}
func generateSchemas(v *viper.Viper) error {
// we generate schemas from the config/crds in the root of this project
// those crds can be created from controller-gen or by running `make openapischema`
workdir, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "failed to get workdir")
}
airgapContent, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_airgaps.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read airgap crd")
}
if err := generateSchemaFromCRD(airgapContent, filepath.Join(workdir, v.GetString("output-dir"), "airgap-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write airgap schema")
}
applicationContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_applications.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read application crd")
}
if err := generateSchemaFromCRD(applicationContents, filepath.Join(workdir, v.GetString("output-dir"), "application-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write application schema")
}
configContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_configs.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read config crd")
}
if err := generateSchemaFromCRD(configContents, filepath.Join(workdir, v.GetString("output-dir"), "config-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write config schema")
}
configValuesContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_configvalues.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read configvalues crd")
}
if err := generateSchemaFromCRD(configValuesContents, filepath.Join(workdir, v.GetString("output-dir"), "configvalues-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write configvalues schema")
}
helmChartContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_helmcharts.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read helmchart crd")
}
if err := generateSchemaFromCRD(helmChartContents, filepath.Join(workdir, v.GetString("output-dir"), "helmchart-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write helmchart schema")
}
installationContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_installations.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read installations crd")
}
if err := generateSchemaFromCRD(installationContents, filepath.Join(workdir, v.GetString("output-dir"), "installation-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write installation schema")
}
licenseContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_licenses.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read license crd")
}
if err := generateSchemaFromCRD(licenseContents, filepath.Join(workdir, v.GetString("output-dir"), "license-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write license schema")
}
ingressConfigContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_ingressconfigs.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read ingressconfigs crd")
}
if err := generateSchemaFromCRD(ingressConfigContents, filepath.Join(workdir, v.GetString("output-dir"), "ingressconfigs-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write ingressconfigs schema")
}
identityConfigContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_identityconfigs.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read identityconfigs crd")
}
if err := generateSchemaFromCRD(identityConfigContents, filepath.Join(workdir, v.GetString("output-dir"), "identityconfigs-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write identityconfigs schema")
}
identityContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_identities.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read identity crd")
}
if err := generateSchemaFromCRD(identityContents, filepath.Join(workdir, v.GetString("output-dir"), "identity-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write identity schema")
}
lintConfigContents, err := ioutil.ReadFile(filepath.Join(workdir, "config", "crds", "kots.io_lintconfigs.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read lintconfigs crd")
}
if err := generateSchemaFromCRD(lintConfigContents, filepath.Join(workdir, v.GetString("output-dir"), "lintconfigs-kots-v1beta1.json")); err != nil {
return errors.Wrap(err, "failed to write lintconfigs schema")
}
return nil
}
func generateSchemaFromCRD(crd []byte, outfile string) error {
extensionsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(crd, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to decode crd")
}
customResourceDefinition := obj.(*extensionsv1.CustomResourceDefinition)
b, err := json.MarshalIndent(customResourceDefinition.Spec.Versions[0].Schema.OpenAPIV3Schema, "", " ")
if err != nil {
return errors.Wrap(err, "failed to marshal json")
}
_, err = os.Stat(outfile)
if err == nil {
if err := os.Remove(outfile); err != nil {
return errors.Wrap(err, "failed to remove file")
}
}
d, _ := path.Split(outfile)
_, err = os.Stat(d)
if os.IsNotExist(err) {
if err = os.MkdirAll(d, 0755); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
// whoa now
// working around the fact that controller-gen doesn't have tags to generate oneOf schemas, so this is hacky.
// going to work to add an issue there to support and if they accept, this terrible thing can go away
replacer := strings.NewReplacer(
`"type": "BoolString"`, `"oneOf": [{"type": "string"},{"type": "boolean"}]`,
`"type": "QuotedBool"`, `"oneOf": [{"type": "string"},{"type": "boolean"}]`,
)
boolStringed := replacer.Replace(string(b))
err = ioutil.WriteFile(outfile, []byte(boolStringed), 0644)
if err != nil {
return errors.Wrap(err, "failed to write file")
}
return nil
}