forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
experimental.go
80 lines (66 loc) · 1.64 KB
/
experimental.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
// Copyright (c) 2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package experimental
import (
"context"
"fmt"
"regexp"
)
const (
nameRegStr = "^[a-z][a-z0-9_]*$"
)
// Feature to be experimental
type Feature struct {
Name string
Description string
// the expected release version to move out from experimental
ExpRelease string
}
type contextKey struct{}
var (
supportedFeatures = make(map[string]Feature)
expContextKey = contextKey{}
)
// Register register a new experimental feature
func Register(feature Feature) error {
if err := validateFeature(feature); err != nil {
return err
}
if _, ok := supportedFeatures[feature.Name]; ok {
return fmt.Errorf("Feature %q had been registered before", feature.Name)
}
supportedFeatures[feature.Name] = feature
return nil
}
// Get returns Feature with requested name
func Get(name string) *Feature {
if f, ok := supportedFeatures[name]; ok {
return &f
}
return nil
}
func validateFeature(feature Feature) error {
if len(feature.Name) == 0 ||
len(feature.Description) == 0 ||
len(feature.ExpRelease) == 0 {
return fmt.Errorf("experimental feature must have valid name, description and expected release")
}
reg := regexp.MustCompile(nameRegStr)
if !reg.MatchString(feature.Name) {
return fmt.Errorf("feature name must in the format %q", nameRegStr)
}
return nil
}
func ContextWithExp(ctx context.Context, names []string) context.Context {
return context.WithValue(ctx, expContextKey, names)
}
func ExpFromContext(ctx context.Context) []string {
value := ctx.Value(expContextKey)
if value == nil {
return nil
}
names := value.([]string)
return names
}