|
| 1 | +// Package features implements Kubernetes-style feature gates for the |
| 2 | +// snapshot agent: experimental capabilities are explicit per-agent opt-ins |
| 3 | +// selected with --feature-gates=Name=bool,... (or the FEATURE_GATES env |
| 4 | +// var), with alpha gates off by default. |
| 5 | +package features |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "sort" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// Feature is the name of a feature gate. |
| 15 | +type Feature string |
| 16 | + |
| 17 | +// DirectMemoryBackend gates the direct_memory backend. It is driven by |
| 18 | +// GPU-CR, which is itself experimental and carries deployment requirements |
| 19 | +// and operational caveats. |
| 20 | +const DirectMemoryBackend Feature = "DirectMemoryBackend" |
| 21 | + |
| 22 | +// defaults registers every known gate and its default state. Alpha gates |
| 23 | +// default to false; flipping a default here is the promotion path |
| 24 | +// (alpha → beta → GA), as in Kubernetes. Adding a gate is one const plus |
| 25 | +// one entry here. |
| 26 | +var defaults = map[Feature]bool{ |
| 27 | + DirectMemoryBackend: false, |
| 28 | +} |
| 29 | + |
| 30 | +// Gates holds the explicitly configured gate values. The zero value (nil) |
| 31 | +// is valid and yields every gate's default — callers that don't care pass |
| 32 | +// nil. |
| 33 | +type Gates map[Feature]bool |
| 34 | + |
| 35 | +// Parse parses a comma-separated list of Name=bool pairs, tolerating |
| 36 | +// whitespace around names, values, and separators. An empty spec yields |
| 37 | +// the defaults. Unknown gate names and non-boolean values are errors so |
| 38 | +// that a typo never runs silently ignored. A gate repeated within the |
| 39 | +// spec takes its last value, matching Kubernetes --feature-gates |
| 40 | +// behavior. |
| 41 | +func Parse(spec string) (Gates, error) { |
| 42 | + gates := Gates{} |
| 43 | + if strings.TrimSpace(spec) == "" { |
| 44 | + return gates, nil |
| 45 | + } |
| 46 | + for _, pair := range strings.Split(spec, ",") { |
| 47 | + pair = strings.TrimSpace(pair) |
| 48 | + if pair == "" { |
| 49 | + continue |
| 50 | + } |
| 51 | + name, value, found := strings.Cut(pair, "=") |
| 52 | + if !found { |
| 53 | + return nil, fmt.Errorf("invalid feature gate %q: must be Name=bool", pair) |
| 54 | + } |
| 55 | + feature := Feature(strings.TrimSpace(name)) |
| 56 | + if _, known := defaults[feature]; !known { |
| 57 | + return nil, fmt.Errorf("unknown feature gate %q; known gates: %s", strings.TrimSpace(name), knownGates()) |
| 58 | + } |
| 59 | + enabled, err := strconv.ParseBool(strings.TrimSpace(value)) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("invalid value %q for feature gate %q: must be a boolean", strings.TrimSpace(value), feature) |
| 62 | + } |
| 63 | + gates[feature] = enabled |
| 64 | + } |
| 65 | + return gates, nil |
| 66 | +} |
| 67 | + |
| 68 | +// Enabled reports whether the gate is on: the explicitly configured value |
| 69 | +// if set, else the registry default. |
| 70 | +func (g Gates) Enabled(f Feature) bool { |
| 71 | + if enabled, ok := g[f]; ok { |
| 72 | + return enabled |
| 73 | + } |
| 74 | + return defaults[f] |
| 75 | +} |
| 76 | + |
| 77 | +// String returns every known gate with its effective value as a sorted, |
| 78 | +// comma-separated Name=bool list, for startup logging. |
| 79 | +func (g Gates) String() string { |
| 80 | + pairs := make([]string, 0, len(defaults)) |
| 81 | + for feature := range defaults { |
| 82 | + pairs = append(pairs, fmt.Sprintf("%s=%t", feature, g.Enabled(feature))) |
| 83 | + } |
| 84 | + sort.Strings(pairs) |
| 85 | + return strings.Join(pairs, ",") |
| 86 | +} |
| 87 | + |
| 88 | +func knownGates() string { |
| 89 | + names := make([]string, 0, len(defaults)) |
| 90 | + for feature := range defaults { |
| 91 | + names = append(names, string(feature)) |
| 92 | + } |
| 93 | + sort.Strings(names) |
| 94 | + return strings.Join(names, ", ") |
| 95 | +} |
0 commit comments