Repository navigation
ci(flags): validate runtime flag definitions before merge #4484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c37ea50
ci(flags): validate runtime definitions against flagd schema
devantler 989402b
chore: incorporate merged Go validator migration
devantler d963635
chore: incorporate merged runner preparation
devantler ea71ae1
Merge remote-tracking branch 'origin/main' into codex/platform-flag-s…
devantler 42e040d
fix(ci): match Kubernetes feature flag YAML decoding
devantler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # FeatureFlag definitions | ||
|
|
||
| The guard uses Kubernetes-compatible YAML 1.1 decoding. Quote variant names such | ||
| as `"on"` and `"off"`; an unquoted boolean default is rejected. Duplicate keys | ||
| are rejected before validation. Symlinks to YAML, directories or unresolved | ||
| targets fail coverage; ordinary non-manifest file symlinks are ignored. | ||
|
|
||
| ```bash | ||
| go run ./scripts/validate-feature-flags [YAML-ROOT] | ||
| go test ./scripts/validate-feature-flags | ||
| ``` | ||
|
|
||
| CI runs the guard on PRs and merge groups without a path filter. It inspects all | ||
| YAML documents and list members under `k8s`, validates `spec.flagSpec`, and | ||
| reports how many FeatureFlag resources were examined. No flags is an explicit | ||
| empty-coverage result. Unreadable inputs and invalid definitions fail. | ||
|
|
||
| The schemas are unmodified copies from the official | ||
| [`open-feature/flagd-schemas` release json/json-schema-v0.2.15](https://github.com/open-feature/flagd-schemas/tree/1daf5ff56b48d582187d59e35d48c6e191c23839/json), | ||
| commit `1daf5ff56b48d582187d59e35d48c6e191c23839`: | ||
|
|
||
| | File | SHA-256 | | ||
| | --- | --- | | ||
| | flags.json | a9b065cc3e140d10a5e139a3f2bbd2f24d4fe8a728ce824a5f2a1231ed60680b | | ||
| | targeting.json | 4194aeab5611f80ce8650a10bed5b18483754587bfb2d7ef093e75a0609a2e75 | | ||
|
|
||
| Both are embedded, and the compiler refuses every unregistered schema URI. | ||
| Validation makes no network request. A schema update replaces both upstream | ||
| files and refreshes these checksums in a reviewed change. | ||
|
|
||
| The flagd schema permits a null code-defined default for general flagd inputs; | ||
| the OpenFeature operator's `v1beta1` CR requires a string `defaultVariant`. | ||
| The guard also checks that it names a defined variant. It does not attempt to | ||
| prove targeting outcomes, SDK integration, or live flag consumption. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "embed" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/santhosh-tekuri/jsonschema/v6" | ||
| "go.yaml.in/yaml/v2" | ||
| kubernetesyaml "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| //go:embed schema/*.json | ||
| var schemas embed.FS | ||
|
|
||
| const schemaURL = "https://flagd.dev/schema/v0/flags.json" | ||
|
|
||
| type offlineLoader struct{} | ||
|
|
||
| func (offlineLoader) Load(url string) (any, error) { | ||
| return nil, fmt.Errorf("unregistered schema %q; network loading is disabled", url) | ||
| } | ||
|
|
||
| func compileSchema() (*jsonschema.Schema, error) { | ||
| compiler := jsonschema.NewCompiler() | ||
| compiler.UseLoader(offlineLoader{}) | ||
| for _, name := range []string{"flags.json", "targeting.json"} { | ||
| data, err := schemas.ReadFile("schema/" + name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| document, err := jsonschema.UnmarshalJSON(bytes.NewReader(data)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := compiler.AddResource("https://flagd.dev/schema/v0/"+name, document); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| return compiler.Compile(schemaURL) | ||
| } | ||
|
|
||
| func inspect(document map[string]any, schema *jsonschema.Schema, count *int) error { | ||
| kind, _ := document["kind"].(string) | ||
| api, _ := document["apiVersion"].(string) | ||
| if kind == "List" || kind == "FeatureFlagList" { | ||
| if kind == "FeatureFlagList" && api != "core.openfeature.dev/v1beta1" { | ||
| return fmt.Errorf("unsupported FeatureFlagList API %q", api) | ||
| } | ||
| items, ok := document["items"].([]any) | ||
| if !ok { | ||
| return fmt.Errorf("%s must contain an items list", kind) | ||
| } | ||
| for _, item := range items { | ||
| child, ok := item.(map[string]any) | ||
| if !ok { | ||
| return errors.New("resource list item must be an object") | ||
| } | ||
| if kind == "FeatureFlagList" { | ||
| if child["kind"] == nil { | ||
| child["kind"] = "FeatureFlag" | ||
| } | ||
| if child["apiVersion"] == nil { | ||
| child["apiVersion"] = api | ||
| } | ||
| if child["kind"] != "FeatureFlag" { | ||
| return errors.New("FeatureFlagList contains a different resource kind") | ||
| } | ||
| } | ||
| if err := inspect(child, schema, count); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| if kind != "FeatureFlag" { | ||
| return nil | ||
| } | ||
| if api != "core.openfeature.dev/v1beta1" { | ||
| return fmt.Errorf("unsupported FeatureFlag API %q", api) | ||
| } | ||
| spec, _ := document["spec"].(map[string]any) | ||
| definition, ok := spec["flagSpec"].(map[string]any) | ||
| if !ok { | ||
| return errors.New("FeatureFlag spec.flagSpec must be an object") | ||
| } | ||
| data, err := json.Marshal(definition) | ||
| if err != nil { | ||
| return fmt.Errorf("flag definition is not JSON-compatible: %w", err) | ||
| } | ||
| value, err := jsonschema.UnmarshalJSON(bytes.NewReader(data)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := schema.Validate(value); err != nil { | ||
| return fmt.Errorf("flagd schema: %w", err) | ||
| } | ||
| flags, _ := definition["flags"].(map[string]any) | ||
| keys := make([]string, 0, len(flags)) | ||
| for key := range flags { | ||
| keys = append(keys, key) | ||
| } | ||
| // Stable diagnostics when several flags have invalid defaults. | ||
| sort.Strings(keys) | ||
| for _, key := range keys { | ||
| flag, _ := flags[key].(map[string]any) | ||
| variant, ok := flag["defaultVariant"].(string) | ||
| if !ok { | ||
| return fmt.Errorf("flag %q requires a string defaultVariant in the operator CR", key) | ||
| } | ||
| variants, _ := flag["variants"].(map[string]any) | ||
| if _, exists := variants[variant]; !exists { | ||
| return fmt.Errorf("flag %q defaultVariant does not name a variant", key) | ||
| } | ||
| } | ||
| (*count)++ | ||
| return nil | ||
| } | ||
|
|
||
| func validate(root string, output io.Writer) error { | ||
| schema, err := compileSchema() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| count := 0 | ||
| err = filepath.WalkDir(root, func(path string, entry os.DirEntry, walkErr error) error { | ||
| if walkErr != nil { | ||
| return walkErr | ||
| } | ||
| if entry.Type()&os.ModeSymlink != 0 { | ||
| info, statErr := os.Stat(path) | ||
| ext := filepath.Ext(path) | ||
| if statErr != nil || info.IsDir() || ext == ".yaml" || ext == ".yml" { | ||
| return fmt.Errorf("refusing symlink that may hide manifests: %s", path) | ||
| } | ||
| return nil | ||
| } | ||
| if entry.IsDir() || (filepath.Ext(path) != ".yaml" && filepath.Ext(path) != ".yml") { | ||
| return nil | ||
| } | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| decoder := yaml.NewDecoder(bytes.NewReader(data)) | ||
| decoder.SetStrict(true) | ||
| for { | ||
| var value any | ||
| err := decoder.Decode(&value) | ||
| if errors.Is(err, io.EOF) { | ||
| return nil | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("%s: %w", path, err) | ||
| } | ||
| // Standalone JSON6902 patch arrays and scalar overlay inputs are not CRs. | ||
| if _, mapping := value.(map[any]any); !mapping { | ||
| continue | ||
| } | ||
| // Use the Kubernetes YAML 1.1 resolver before JSON conversion. Re-encoding | ||
| // a yaml.v3 node would turn plain on/off into quoted strings instead. | ||
| documentYAML, err := yaml.Marshal(value) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: %w", path, err) | ||
| } | ||
| documentJSON, err := kubernetesyaml.YAMLToJSONStrict(documentYAML) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: %w", path, err) | ||
| } | ||
| var document map[string]any | ||
| if err := json.Unmarshal(documentJSON, &document); err != nil { | ||
| return fmt.Errorf("%s: %w", path, err) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if err := inspect(document, schema, &count); err != nil { | ||
| return fmt.Errorf("%s: %w", path, err) | ||
| } | ||
| } | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if count == 0 { | ||
| _, err = fmt.Fprintln(output, "No FeatureFlag resources found; flag coverage is empty.") | ||
| } else { | ||
| _, err = fmt.Fprintf(output, "Validated %d FeatureFlag resources against flagd schema 0.2.15.\n", count) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func main() { | ||
| root := "k8s" | ||
| if len(os.Args) > 2 { | ||
| fmt.Fprintln(os.Stderr, "usage: validate-feature-flags [ROOT]") | ||
| os.Exit(2) | ||
| } | ||
| if len(os.Args) == 2 { | ||
| root = os.Args[1] | ||
| } | ||
| if strings.TrimSpace(root) == "" { | ||
| fmt.Fprintln(os.Stderr, "ROOT must not be empty") | ||
| os.Exit(2) | ||
| } | ||
| if err := validate(root, os.Stdout); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.