forked from jsonnet-libs/k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (178 loc) · 4.96 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/go-clix/cli"
"github.com/google/go-jsonnet/formatter"
"github.com/jsonnet-libs/k8s/pkg/model"
"github.com/jsonnet-libs/k8s/pkg/render"
"github.com/jsonnet-libs/k8s/pkg/swagger"
"gopkg.in/yaml.v2"
)
// Target defines an API subset to generate
type Target struct {
Output string `yaml:"output"`
Crds []string `yaml:"crds"`
Openapi string `yaml:"openapi"`
Prefix string `yaml:"prefix"`
PatchDir string `yaml:"patchDir"`
ExtensionDir string `yaml:"extensionDir"`
LocalName string `yaml:"localName"`
Repository string `yaml:"repository"`
Description string `yaml:"description"`
}
// Config holds settings for this generator
type Config struct {
Specs []Target `yaml:"specs"`
}
func main() {
log.SetFlags(0)
cmd := &cli.Command{
Use: "k8s-gen [versions]",
Short: "k8s-gen generates the Jsonnet Kubernetes library from OpenAPI specs",
}
configFile := cmd.Flags().StringP("config", "c", "config.yml", "YAML configuration file")
output := cmd.Flags().StringP("output", "o", ".", "directory to put artifacts into")
cmd.Run = func(cmd *cli.Command, args []string) error {
config := loadConfig(*configFile)
os.Chdir(filepath.Dir(*configFile))
for _, t := range config.Specs {
if len(args) > 0 && !hasStr(args, t.Output) {
continue
}
definitions := make(swagger.Definitions)
if len(t.Crds) > 0 {
for _, url := range t.Crds {
log.Printf("Generating '%s' from '%s, %s'", t.Output, url, t.Prefix)
defs, err := swagger.Load(&swagger.CRDLoader{}, url)
if err != nil {
return err
}
for k, v := range defs {
definitions[k] = v
}
}
} else {
log.Printf("Generating '%s' from '%s, %s'", t.Output, t.Openapi, t.Prefix)
d, err := swagger.Load(&swagger.SwaggerLoader{}, t.Openapi)
if err != nil {
return err
}
definitions = d
}
groups := model.Load(&definitions, t.Prefix)
path := filepath.Join(*output, t.Output)
renderJsonnet(path, groups, t)
}
return nil
}
if err := cmd.Execute(); err != nil {
log.Fatalln(err)
}
}
func hasStr(slice []string, s string) bool {
for _, sl := range slice {
if sl == s {
return true
}
}
return false
}
func loadConfig(file string) Config {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalln(err)
}
var c Config
if err := yaml.Unmarshal(data, &c); err != nil {
log.Fatalln(err)
}
return c
}
func renderJsonnet(dir string, groups map[string]model.Group, target Target) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatalln(err)
}
// gen.libsonnet
index := render.Index(groups, target.LocalName, target.Repository, target.Output, target.Description)
indexFile := filepath.Join(dir, render.IndexFile)
if err := writeJsonnet(indexFile, index.String()); err != nil {
log.Fatalln("writing gen.libsonnet:", err)
}
// _gen/<group>/<version>/<kind>.libsonnet
gen := filepath.Join(dir, render.GenPrefix)
if err := os.MkdirAll(gen, os.ModePerm); err != nil {
log.Fatalln(err)
}
for name, group := range groups {
g := render.Group(name, group)
for fn, o := range g {
file := filepath.Join(gen, name, fn)
os.MkdirAll(filepath.Dir(file), os.ModePerm)
if err := writeJsonnet(file, o.String()); err != nil {
log.Fatalln(err)
}
}
}
var adds []string
var err error
customDirStat, err := os.Stat(target.PatchDir)
if err == nil && customDirStat.IsDir() {
// custom patches
adds, err = copyDirLibsonnet(target.PatchDir, filepath.Join(dir, render.CustomPrefix))
if err != nil {
log.Fatalln("Copying custom patches:", err)
}
}
extDirStat, err := os.Stat(target.ExtensionDir)
if err == nil && extDirStat.IsDir() {
if _, err := copyDirLibsonnet(target.ExtensionDir, filepath.Join(dir, render.ExtPrefix)); err != nil {
log.Fatalln("Copying extensions:", err)
}
}
// main.libsonnet
main := render.Main(adds)
mainFile := filepath.Join(dir, render.MainFile)
if err := writeJsonnet(mainFile, main.String()); err != nil {
log.Fatalln(err)
}
}
func writeJsonnet(to, data string) error {
s, err := formatter.Format("", data, formatter.DefaultOptions())
if err != nil {
return fmt.Errorf("%s: %s", err, data)
}
return ioutil.WriteFile(to, []byte(s), 0644)
}
func copyDirLibsonnet(dir, to string) ([]string, error) {
// custom patches
var adds []string
if _, err := os.Stat(dir); err != nil {
return nil, fmt.Errorf("%s does not exist", dir)
}
filepath.Walk(dir, func(name string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
if filepath.Ext(name) != ".libsonnet" {
return nil
}
adds = append(adds, name)
return nil
})
for _, a := range adds {
content, err := ioutil.ReadFile(a)
if err != nil {
return nil, err
}
a = filepath.Join(to, filepath.Base(a))
os.MkdirAll(filepath.Dir(a), os.ModePerm)
if err := ioutil.WriteFile(a, content, 0644); err != nil {
return nil, err
}
}
return adds, nil
}