Skip to content

Commit

Permalink
use git submodules instead of copying schemas
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Apr 26, 2024
1 parent 93129bf commit cd52a36
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 778 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "apis/flagd-schemas"]
path = apis/flagd-schemas
url = https://github.com/open-feature/flagd-schemas.git
36 changes: 26 additions & 10 deletions apis/core/v1beta1/featureflag_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1beta1
import (
"encoding/json"
"fmt"
"os"

_ "embed"

Expand All @@ -29,15 +30,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

//go:embed schema/targeting.json
var TargetingSchema string

//go:embed schema/flags.json
var FlagsScheme string

// log is for logging in this package.
var featureflaglog = logf.Log.WithName("featureflag-resource")

const SchemaPath = "./../../flagd-schemas/json/"

func (ff *FeatureFlag) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(ff).
Expand Down Expand Up @@ -84,11 +81,10 @@ func validateFeatureFlagFlags(flags Flags) error {
}

documentLoader := gojsonschema.NewStringLoader(string(b))
schemaLoader := gojsonschema.NewSchemaLoader()
schemaLoader.AddSchemas(gojsonschema.NewStringLoader(TargetingSchema))
compiledSchema, err := schemaLoader.Compile(gojsonschema.NewStringLoader(FlagsScheme))

compiledSchema, err := initSchemas()
if err != nil {
return err
return fmt.Errorf("unable to initialize Schema: %s", err.Error())
}

result, err := compiledSchema.Validate(documentLoader)
Expand All @@ -104,3 +100,23 @@ func validateFeatureFlagFlags(flags Flags) error {
}
return err
}

func initSchemas() (*gojsonschema.Schema, error) {
targetingSchema, err := os.ReadFile(SchemaPath + "targeting.json")
if err != nil {
return nil, err
}
flagsScheme, err := os.ReadFile(SchemaPath + "flags.json")
if err != nil {
return nil, err
}

schemaLoader := gojsonschema.NewSchemaLoader()
schemaLoader.AddSchemas(gojsonschema.NewStringLoader(string(targetingSchema)))
compiledSchema, err := schemaLoader.Compile(gojsonschema.NewStringLoader(string(flagsScheme)))
if err != nil {
return nil, err
}

return compiledSchema, nil
}
185 changes: 0 additions & 185 deletions apis/core/v1beta1/schema/flags.json

This file was deleted.

Loading

0 comments on commit cd52a36

Please sign in to comment.