This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
/
generate.go
293 lines (271 loc) · 7.9 KB
/
generate.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2019 eBay Inc.
// Primary authors: Simon Fell, Diego Ongaro,
// Raymond Kroeker, and Sathish Kandasamy.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Command generate writes out Kubernetes configuration for portions of the Akutan
// cluster. The diskview, persistent volume and persistent volume claims
// require an amount of boilerplate copy and paste based upon the number of
// partitions, replicas and keyspaces.
//
// Template Dir: cluster/k8s
// Output Dir: cluster/k8s/generated
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/ebay/akutan/config"
"github.com/ebay/akutan/tools/gen-local/gen"
)
const (
logServerCount = 1
templateRoot = "cluster/k8s"
outputRoot = "cluster/k8s/generated"
)
var options struct {
cfgFile string
akutanImagesPath string
akutanImagesTag string
partitions int
replicas int
}
func main() {
cluster := &cluster{}
flag.StringVar(&options.cfgFile, "cfg", "cluster/k8s/config.json",
"Akutan config file to use as template")
flag.StringVar(&options.akutanImagesPath, "akutan-images-path", "",
"Prefix to the core Docker images. If empty, uses locally built images.")
flag.StringVar(&options.akutanImagesTag, "akutan-images-tag", "latest",
"Tag to use for core Docker images")
flag.StringVar(&cluster.Images.Jaeger, "jaeger-image",
"jaegertracing/all-in-one:1.9",
"Path to Jaeger all-in-one Docker image")
flag.StringVar(&cluster.Images.LogService, "logservice-image",
"akutan-kafka:latest",
"Path to Log service Docker image")
flag.BoolVar(&cluster.UsePersistentVolumes, "pv", false,
"Use persistent volume claims and persistent volumes for Log service and Disk Views")
flag.IntVar(&options.partitions, "partitions", 2,
"Number of partitions of each DiskView hash-space")
flag.IntVar(&options.replicas, "replicas", 1,
"Number of replicas of each DiskView partition")
flag.Parse()
cluster.Images.API = joinImagePath(options.akutanImagesPath, "akutan-api", options.akutanImagesTag)
cluster.Images.DiskView = joinImagePath(options.akutanImagesPath, "akutan-diskview", options.akutanImagesTag)
cluster.Images.TxView = joinImagePath(options.akutanImagesPath, "akutan-txview", options.akutanImagesTag)
baseCfg, err := config.Load(options.cfgFile)
if err != nil {
panic(fmt.Sprintf("Unable to load base configuration: %v", err))
}
numViews := 1 + options.partitions*options.replicas*2
spec := gen.Spec{
BaseCfg: baseCfg,
API: gen.APISpec{
Replicas: 1,
},
Views: gen.ViewsSpec{
GRPCPorts: repeat(":9980", numViews),
HTTPPorts: repeat(":9981", numViews),
},
TxTimeout: gen.TxTimeoutSpec{
Replicas: 1,
},
HashPO: gen.HashPOSpec{
Partitions: options.partitions,
Replicas: options.replicas,
},
HashSP: gen.HashSPSpec{
Partitions: options.partitions,
Replicas: options.replicas,
},
}
processes, err := spec.Generate()
if err != nil {
panic(fmt.Sprintf("Unable to generate local cluster configuration: %v", err))
}
// Fill in template variables.
for _, proc := range processes.Filter(gen.DiskViewProcess) {
cluster.DiskViews = append(cluster.DiskViews, diskView{
Process: proc,
Volume: volume{
Name: proc.Name,
Claim: proc.Name,
},
})
}
cluster.APICfg = processes.Filter(gen.APIProcess)[0].Cfg
cluster.TxViewCfg = processes.Filter(gen.TxTimerProcess)[0].Cfg
cluster.LogServers = makeLogServers()
// Make sure the directory outputRoot exists and has no children.
err = os.RemoveAll(outputRoot)
if err != nil {
panic(fmt.Sprintf("Failed to remove directory %v: %v", outputRoot, err))
}
err = os.MkdirAll(outputRoot, 0755)
if err != nil {
panic(fmt.Sprintf("Failed to create directory %v: %v", outputRoot, err))
}
// Generate ${outputRoot}/%.yaml from ${templateRoot}/%.tpl.yaml.
files, err := ioutil.ReadDir(templateRoot)
if err != nil {
panic(fmt.Sprintf("Failed to list template directory '%v': %v", templateRoot, err))
}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".tpl.yaml") {
err := execTemplate(
filepath.Join(templateRoot, file.Name()),
filepath.Join(outputRoot, strings.TrimSuffix(file.Name(), ".tpl.yaml")+".yaml"),
cluster)
if err != nil {
panic(fmt.Sprintf("Failed to execute template %v: %v", file.Name(), err))
}
}
}
}
// repeat returns a slice of length 'n' with each element equal to 'x'.
func repeat(x string, n int) []string {
slice := make([]string, n)
for i := range slice {
slice[i] = x
}
return slice
}
func makeLogServers() []logServer {
logServers := make([]logServer, logServerCount)
for ls := 0; ls < logServerCount; ls++ {
logServers[ls] = logServer{
Number: ls + 1,
Count: logServerCount,
}
logServers[ls].Volume = volume{
Name: logServers[ls].Name(),
Claim: logServers[ls].Name(),
}
}
return logServers
}
// execTemplate reads the file inputName, evaluates it, and writes the result to
// outputName.
func execTemplate(inputName, outputName string, data interface{}) error {
input, err := ioutil.ReadFile(inputName)
if err != nil {
return err
}
tmpl := template.New(filepath.Base(inputName))
tmpl.Funcs(map[string]interface{}{
"join": strings.Join,
"json": func(indent string, v interface{}) (string, error) {
data, err := json.MarshalIndent(v, indent, " ")
if err != nil {
return "", fmt.Errorf("json.Marshal failed in %s: %s", inputName, err)
}
return string(data), nil
},
})
tmpl, err = tmpl.Parse(string(input))
if err != nil {
return err
}
outFile, err := os.Create(outputName)
if err != nil {
return err
}
w := bufio.NewWriter(outFile)
fmt.Fprintf(w, "# This file was automatically generated on %s from %s.\n",
time.Now().UTC().Format("2006-01-02 15:04:05 MST"),
inputName)
err = tmpl.Execute(w, data)
if err != nil {
outFile.Close()
return err
}
err = w.Flush()
if err != nil {
return err
}
return outFile.Close()
}
// joinImagePath returns a fully-qualified path to a Docker image. base should
// contain no slashes or colons. path is the prefix leading up to base. path and
// tag are optional.
func joinImagePath(path, base, tag string) string {
var b strings.Builder
if path != "" {
b.WriteString(path)
if !strings.HasSuffix(path, "/") {
b.WriteString("/")
}
}
b.WriteString(base)
if tag != "" {
b.WriteString(":")
b.WriteString(tag)
}
return b.String()
}
// cluster is the object handed to the Kubernetes YAML templates.
type cluster struct {
// Fully-qualified paths to Docker images, optionally including tags.
Images struct {
API string
DiskView string
Jaeger string
LogService string
TxView string
}
DiskViews []diskView
// The current templates only handle one API server an one Tx Timer View.
APICfg *config.Akutan
TxViewCfg *config.Akutan
LogServers []logServer
UsePersistentVolumes bool
}
func (c *cluster) Volumes() []volume {
if !c.UsePersistentVolumes {
return nil
}
volumes := make([]volume, len(c.LogServers)+len(c.DiskViews))
i := 0
for _, ls := range c.LogServers {
volumes[i] = ls.Volume
i++
}
for _, dv := range c.DiskViews {
volumes[i] = dv.Volume
i++
}
return volumes
}
type diskView struct {
*gen.Process
Volume volume
}
type logServer struct {
Number int
Count int
Volume volume
}
func (ls logServer) Name() string {
return fmt.Sprintf("log-store-%d", ls.Number)
}
type volume struct {
Name string
Claim string
}