-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
158 lines (137 loc) · 3.15 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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/cybozu-go/cke"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
)
func main() {
err := subMain()
if err != nil {
fmt.Println("Error:", err)
os.Exit(2)
}
}
func subMain() error {
files, err := filepath.Glob("*.yml")
if err != nil {
return err
}
images := make(map[string]string)
for _, img := range cke.AllImages() {
id := strings.SplitN(path.Base(img), ":", 2)[0]
images[id] = img
}
var allResources []cke.ResourceDefinition
for _, fname := range files {
res, err := loadResources(fname, images)
if err != nil {
return err
}
allResources = append(allResources, res...)
}
cke.SortResources(allResources)
return renderResources(allResources)
}
func loadResources(fname string, images map[string]string) ([]cke.ResourceDefinition, error) {
tmpl, err := template.New(filepath.Base(fname)).ParseFiles(fname)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, images)
if err != nil {
return nil, err
}
y := utilyaml.NewYAMLReader(bufio.NewReader(buf))
var res []cke.ResourceDefinition
for {
data, err := y.Read()
if err == io.EOF {
return res, nil
}
if err != nil {
return nil, err
}
key, err := cke.ParseResource(data)
if err != nil {
return nil, err
}
parts := strings.Split(key, "/")
kind := parts[0]
var namespace, name string
if len(parts) == 2 {
name = parts[1]
} else {
namespace = parts[1]
name = parts[2]
}
obj := struct {
Metadata struct {
Annotations struct {
Revision int64 `json:"cke.cybozu.com/revision,string"`
Image string `json:"cke.cybozu.com/image"`
} `json:"annotations"`
} `json:"metadata"`
}{}
if err := yaml.Unmarshal(data, &obj); err != nil {
return nil, err
}
rev := obj.Metadata.Annotations.Revision
if rev == 0 {
return nil, errors.New("static resources must declare cke.cybozu.com/revision in annotations")
}
res = append(res, cke.ResourceDefinition{
Key: key,
Kind: kind,
Namespace: namespace,
Name: name,
Revision: rev,
Image: obj.Metadata.Annotations.Image,
Definition: data,
})
}
}
var tmpl = template.Must(template.New("").Parse(`// Code generated by compile_resources. DO NOT EDIT.
//go:generate go run ../pkg/compile_resources
package static
import (
"github.com/cybozu-go/cke"
)
// Resources is the Kubernetes resource definitions embedded in CKE.
var Resources = []cke.ResourceDefinition{
{{ range . -}}
{
Key: {{ printf "%q" .Key }},
Kind: "{{ .Kind }}",
Namespace: {{ printf "%q" .Namespace }},
Name: {{ printf "%q" .Name }},
Revision: {{ .Revision }},
Image: {{ printf "%q" .Image }},
Definition: []byte({{ printf "%q" .Definition }}),
},
{{ end -}}
}
`))
func renderResources(res []cke.ResourceDefinition) error {
f, err := os.OpenFile("resources.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
err = tmpl.Execute(f, res)
if err != nil {
return err
}
return exec.Command("goimports", "-w", f.Name()).Run()
}