-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconfig.go
More file actions
235 lines (214 loc) · 5.73 KB
/
Copy pathconfig.go
File metadata and controls
235 lines (214 loc) · 5.73 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
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
package ecschedule
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"text/template"
"github.com/goccy/go-yaml"
gc "github.com/kayac/go-config"
"github.com/winebarrel/cronplan"
)
const defaultRole = "ecsEventsRole"
const (
jsonnetExt = ".jsonnet"
jsonExt = ".json"
)
// BaseConfig baseconfig
type BaseConfig struct {
Region string `yaml:"region" json:"region"`
Cluster string `yaml:"cluster" json:"cluster"`
AccountID string `yaml:"-" json:"-"`
TrackingID string `yaml:"trackingId,omitempty" json:"trackingId,omitempty"`
}
// Config config
type Config struct {
Role string `yaml:"role,omitempty" json:"role,omitempty"`
*BaseConfig `yaml:",inline" json:",inline"`
Rules []*Rule `yaml:"rules" json:"rules"`
Plugins []*Plugin `yaml:"plugins,omitempty" json:"plugins,omitempty"`
templateFuncs []template.FuncMap
dir string
}
// GetRuleByName gets rule by name
func (c *Config) GetRuleByName(name string) *Rule {
for _, r := range c.Rules {
if r.Name == name {
return r
}
}
return nil
}
func (c *Config) setupPlugins(ctx context.Context) error {
for _, p := range c.Plugins {
if err := p.setup(ctx, c); err != nil {
return err
}
}
return nil
}
func (c *Config) cronValidate() error {
// XXX: I'd like to use multiple errors here and format the error messages at the very end.
var errMsgs []string
for _, r := range c.Rules {
err := validateCronExpression(r.ScheduleExpression)
if err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("\trule %q: %s", r.Name, err))
}
}
if len(errMsgs) > 0 {
return fmt.Errorf("schedule expression validation errors:\n%s", strings.Join(errMsgs, "\n"))
}
return nil
}
// validateUniqueRuleNames rejects configurations containing multiple rules
// with the same name. GetRuleByName resolves names to the first match, so
// duplicates silently shadow each other and, worse, hand the same *Rule to
// multiple parallel workers (a data race).
func (c *Config) validateUniqueRuleNames() error {
seen := map[string]bool{}
reported := map[string]bool{}
var dups []string
for _, r := range c.Rules {
if seen[r.Name] && !reported[r.Name] {
dups = append(dups, r.Name)
reported[r.Name] = true
}
seen[r.Name] = true
}
if len(dups) > 0 {
return fmt.Errorf(
"duplicate rule name(s) in configuration: %s (rule names must be unique; regenerate a clean config with `ecschedule dump -region <region> -cluster <cluster>`)",
strings.Join(dups, ", "))
}
return nil
}
func validateCronExpression(exp string) error {
if strings.HasPrefix(exp, "rate(") && strings.HasSuffix(exp, ")") {
return nil
}
strippedExp := strings.TrimSuffix(strings.TrimPrefix(exp, "cron("), ")")
// 6 means `len("cron(") + len("(")`
if len(strippedExp)+6 != len(exp) {
return fmt.Errorf("invalid expression: %q", exp)
}
if strippedExp != strings.TrimSpace(strippedExp) {
return fmt.Errorf(
"trailing or leading spaces are not allowed inside parentheses: %q", exp)
}
_, err := cronplan.Parse(strippedExp)
if err != nil {
return err
}
return nil
}
type loadConfigOptions struct {
extStr map[string]string
extCode map[string]string
}
// LoadConfigOption configures LoadConfig
type LoadConfigOption func(*loadConfigOptions)
// WithExtStr binds Jsonnet std.extVar string variables
func WithExtStr(vars map[string]string) LoadConfigOption {
return func(o *loadConfigOptions) {
if o.extStr == nil {
o.extStr = map[string]string{}
}
for k, v := range vars {
o.extStr[k] = v
}
}
}
// WithExtCode binds Jsonnet std.extVar code variables
func WithExtCode(vars map[string]string) LoadConfigOption {
return func(o *loadConfigOptions) {
if o.extCode == nil {
o.extCode = map[string]string{}
}
for k, v := range vars {
o.extCode[k] = v
}
}
}
// LoadConfig loads config
func LoadConfig(ctx context.Context, r io.Reader, accountID string, confPath string, opts ...LoadConfigOption) (*Config, error) {
var o loadConfigOptions
for _, opt := range opts {
opt(&o)
}
c := Config{}
bs, ext, err := readConfigFile(r, confPath, &o)
if err != nil {
return nil, err
}
bs, err = envReplacer(bs)
if err != nil {
return nil, err
}
if err := unmarshalConfig(bs, &c, ext); err != nil {
return nil, err
}
if err := c.cronValidate(); err != nil {
return nil, err
}
c.AccountID = accountID
if c.TrackingID == "" {
c.TrackingID = c.Cluster
}
if err := c.setupPlugins(ctx); err != nil {
return nil, err
}
c.dir = filepath.Dir(confPath)
loader := gc.New()
for _, f := range c.templateFuncs {
loader.Funcs(f)
}
// recover tfstate variable
bs = tfstateRecover(bs)
// recover ssm variable
bs = ssmRecover(bs)
bs, err = loader.ReadWithEnvBytes(bs)
if err != nil {
return nil, err
}
if err := unmarshalConfig(bs, &c, ext); err != nil {
return nil, err
}
for _, r := range c.Rules {
r.mergeBaseConfig(c.BaseConfig, c.Role)
}
if err := c.validateUniqueRuleNames(); err != nil {
return nil, err
}
return &c, nil
}
// unmarshalConfig unmarshal json or yaml file
func unmarshalConfig(bs []byte, c *Config, ext string) error {
if ext == jsonExt {
return json.Unmarshal(bs, c)
}
// as a YAML file if the file type cannot be determined from the extension (e.g. .ecschedule, ecschedule.cfg)
return yaml.Unmarshal(bs, c)
}
func readConfigFile(r io.Reader, confPath string, o *loadConfigOptions) ([]byte, string, error) {
ext := filepath.Ext(confPath)
if ext == jsonnetExt {
vm := newJsonnetVM()
for k, v := range o.extStr {
vm.ExtVar(k, v)
}
for k, v := range o.extCode {
vm.ExtCode(k, v)
}
bs, err := vm.EvaluateFile(confPath)
if err != nil {
return nil, ext, fmt.Errorf("failed to evaluate jsonnet file: %w", err)
}
return []byte(bs), jsonExt, err
}
bs, err := ioutil.ReadAll(r)
return bs, ext, err
}