-
Notifications
You must be signed in to change notification settings - Fork 186
/
importer.go
392 lines (328 loc) · 9.31 KB
/
importer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package ext
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/blang/semver/v4"
errs "go.flipt.io/flipt/errors"
"go.flipt.io/flipt/rpc/flipt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Creator interface {
GetNamespace(context.Context, *flipt.GetNamespaceRequest) (*flipt.Namespace, error)
CreateNamespace(context.Context, *flipt.CreateNamespaceRequest) (*flipt.Namespace, error)
CreateFlag(context.Context, *flipt.CreateFlagRequest) (*flipt.Flag, error)
CreateVariant(context.Context, *flipt.CreateVariantRequest) (*flipt.Variant, error)
CreateSegment(context.Context, *flipt.CreateSegmentRequest) (*flipt.Segment, error)
CreateConstraint(context.Context, *flipt.CreateConstraintRequest) (*flipt.Constraint, error)
CreateRule(context.Context, *flipt.CreateRuleRequest) (*flipt.Rule, error)
CreateDistribution(context.Context, *flipt.CreateDistributionRequest) (*flipt.Distribution, error)
CreateRollout(context.Context, *flipt.CreateRolloutRequest) (*flipt.Rollout, error)
}
type Importer struct {
creator Creator
}
type ImportOpt func(*Importer)
func NewImporter(store Creator, opts ...ImportOpt) *Importer {
i := &Importer{
creator: store,
}
for _, opt := range opts {
opt(i)
}
return i
}
func (i *Importer) Import(ctx context.Context, enc Encoding, r io.Reader) (err error) {
var (
dec = enc.NewDecoder(r)
version semver.Version
)
idx := 0
for {
var doc = new(Document)
if err := dec.Decode(doc); err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("unmarshalling document: %w", err)
}
// Only support parsing vesrion at the top of each import file.
if idx == 0 {
version = latestVersion
if doc.Version != "" {
version, err = semver.ParseTolerant(doc.Version)
if err != nil {
return fmt.Errorf("parsing document version: %w", err)
}
var found bool
for _, sv := range supportedVersions {
if found = sv.EQ(version); found {
break
}
}
if !found {
return fmt.Errorf("unsupported version: %s", doc.Version)
}
}
}
var namespace = doc.Namespace
if namespace != "" && namespace != flipt.DefaultNamespace {
_, err := i.creator.GetNamespace(ctx, &flipt.GetNamespaceRequest{
Key: namespace,
})
if err != nil {
if status.Code(err) != codes.NotFound && !errs.AsMatch[errs.ErrNotFound](err) {
return err
}
_, err = i.creator.CreateNamespace(ctx, &flipt.CreateNamespaceRequest{
Key: namespace,
Name: namespace,
})
if err != nil {
return err
}
}
}
var (
// map flagKey => *flag
createdFlags = make(map[string]*flipt.Flag)
// map segmentKey => *segment
createdSegments = make(map[string]*flipt.Segment)
// map flagKey:variantKey => *variant
createdVariants = make(map[string]*flipt.Variant)
)
// create flags/variants
for _, f := range doc.Flags {
if f == nil {
continue
}
req := &flipt.CreateFlagRequest{
Key: f.Key,
Name: f.Name,
Description: f.Description,
Enabled: f.Enabled,
NamespaceKey: namespace,
}
// support explicitly setting flag type from 1.1
if f.Type != "" {
if err := ensureFieldSupported("flag.type", semver.Version{
Major: 1,
Minor: 1,
}, version); err != nil {
return err
}
req.Type = flipt.FlagType(flipt.FlagType_value[f.Type])
}
flag, err := i.creator.CreateFlag(ctx, req)
if err != nil {
return fmt.Errorf("creating flag: %w", err)
}
for _, v := range f.Variants {
if v == nil {
continue
}
var out []byte
if v.Attachment != nil {
converted := convert(v.Attachment)
out, err = json.Marshal(converted)
if err != nil {
return fmt.Errorf("marshalling attachment: %w", err)
}
}
variant, err := i.creator.CreateVariant(ctx, &flipt.CreateVariantRequest{
FlagKey: f.Key,
Key: v.Key,
Name: v.Name,
Description: v.Description,
Attachment: string(out),
NamespaceKey: namespace,
})
if err != nil {
return fmt.Errorf("creating variant: %w", err)
}
createdVariants[fmt.Sprintf("%s:%s", flag.Key, variant.Key)] = variant
}
createdFlags[flag.Key] = flag
}
// create segments/constraints
for _, s := range doc.Segments {
if s == nil {
continue
}
segment, err := i.creator.CreateSegment(ctx, &flipt.CreateSegmentRequest{
Key: s.Key,
Name: s.Name,
Description: s.Description,
MatchType: flipt.MatchType(flipt.MatchType_value[s.MatchType]),
NamespaceKey: namespace,
})
if err != nil {
return fmt.Errorf("creating segment: %w", err)
}
for _, c := range s.Constraints {
if c == nil {
continue
}
_, err := i.creator.CreateConstraint(ctx, &flipt.CreateConstraintRequest{
SegmentKey: s.Key,
Type: flipt.ComparisonType(flipt.ComparisonType_value[c.Type]),
Property: c.Property,
Operator: c.Operator,
Value: c.Value,
NamespaceKey: namespace,
})
if err != nil {
return fmt.Errorf("creating constraint: %w", err)
}
}
createdSegments[segment.Key] = segment
}
// create rules/distributions
for _, f := range doc.Flags {
if f == nil {
continue
}
// loop through rules
for idx, r := range f.Rules {
if r == nil {
continue
}
// support implicit rank from version >=1.1
rank := int32(r.Rank)
if rank == 0 && version.GE(semver.Version{Major: 1, Minor: 1}) {
rank = int32(idx) + 1
}
fcr := &flipt.CreateRuleRequest{
FlagKey: f.Key,
Rank: rank,
NamespaceKey: namespace,
}
switch s := r.Segment.IsSegment.(type) {
case SegmentKey:
fcr.SegmentKey = string(s)
case *Segments:
fcr.SegmentKeys = s.Keys
fcr.SegmentOperator = flipt.SegmentOperator(flipt.SegmentOperator_value[s.SegmentOperator])
}
rule, err := i.creator.CreateRule(ctx, fcr)
if err != nil {
return fmt.Errorf("creating rule: %w", err)
}
for _, d := range r.Distributions {
if d == nil {
continue
}
variant, found := createdVariants[fmt.Sprintf("%s:%s", f.Key, d.VariantKey)]
if !found {
return fmt.Errorf("finding variant: %s; flag: %s", d.VariantKey, f.Key)
}
_, err := i.creator.CreateDistribution(ctx, &flipt.CreateDistributionRequest{
FlagKey: f.Key,
RuleId: rule.Id,
VariantId: variant.Id,
Rollout: d.Rollout,
NamespaceKey: namespace,
})
if err != nil {
return fmt.Errorf("creating distribution: %w", err)
}
}
}
// support explicitly setting flag type from 1.1
if len(f.Rollouts) > 0 {
if err := ensureFieldSupported("flag.rollouts", semver.Version{
Major: 1,
Minor: 1,
}, version); err != nil {
return err
}
for idx, r := range f.Rollouts {
if r.Segment != nil && r.Threshold != nil {
return fmt.Errorf(`rollout "%s/%s/%d" cannot have both segment and percentage rule`,
namespace,
f.Key,
idx,
)
}
req := &flipt.CreateRolloutRequest{
NamespaceKey: namespace,
FlagKey: f.Key,
Description: r.Description,
Rank: int32(idx + 1),
}
if r.Segment != nil {
frs := &flipt.RolloutSegment{
Value: r.Segment.Value,
SegmentKey: r.Segment.Key,
}
if len(r.Segment.Keys) > 0 && r.Segment.Key != "" {
return fmt.Errorf("rollout %s/%s/%d cannot have both segment.keys and segment.key",
namespace,
f.Key,
idx,
)
}
// support explicitly setting only "keys" on rules from 1.2
if len(r.Segment.Keys) > 0 {
if err := ensureFieldSupported("flag.rollouts[*].segment.keys", semver.Version{
Major: 1,
Minor: 2,
}, version); err != nil {
return err
}
frs.SegmentKeys = r.Segment.Keys
}
frs.SegmentOperator = flipt.SegmentOperator(flipt.SegmentOperator_value[r.Segment.Operator])
req.Rule = &flipt.CreateRolloutRequest_Segment{
Segment: frs,
}
} else if r.Threshold != nil {
req.Rule = &flipt.CreateRolloutRequest_Threshold{
Threshold: &flipt.RolloutThreshold{
Percentage: r.Threshold.Percentage,
Value: r.Threshold.Value,
},
}
}
if _, err := i.creator.CreateRollout(ctx, req); err != nil {
return fmt.Errorf("creating rollout: %w", err)
}
}
}
}
idx += 1
}
return nil
}
// convert converts each encountered map[interface{}]interface{} to a map[string]interface{} value.
// This is necessary because the json library does not support map[interface{}]interface{} values which nested
// maps get unmarshalled into from the yaml library.
func convert(i interface{}) interface{} {
switch x := i.(type) {
case map[interface{}]interface{}:
m := map[string]interface{}{}
for k, v := range x {
if sk, ok := k.(string); ok {
m[sk] = convert(v)
}
}
return m
case []interface{}:
for i, v := range x {
x[i] = convert(v)
}
}
return i
}
func ensureFieldSupported(field string, expected, have semver.Version) error {
if have.LT(expected) {
return fmt.Errorf("%s is supported in version >=%s, found %s",
field,
versionString(expected),
versionString(have))
}
return nil
}