Skip to content

Commit

Permalink
feat: introduce per-sync configurations (#448)
Browse files Browse the repository at this point in the history
<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

- introduces the `SyncProviderConfig` object and associated start flags
to pass configuration to specific sync providers

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

#405

### Notes
<!-- any additional notes for this PR -->

### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be
noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
<!-- if applicable, add testing instructions under this section -->

---------

Signed-off-by: James Milligan <james@omnant.co.uk>
Signed-off-by: James Milligan <75740990+james-milligan@users.noreply.github.com>
Co-authored-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
james-milligan and skyerus authored Mar 9, 2023
1 parent 6a039ce commit 1d80039
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 70 deletions.
41 changes: 36 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/open-feature/flagd/pkg/logger"
"github.com/open-feature/flagd/pkg/runtime"
"github.com/open-feature/flagd/pkg/sync"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
Expand All @@ -24,6 +25,7 @@ const (
serverCertPathFlagName = "server-cert-path"
serverKeyPathFlagName = "server-key-path"
socketPathFlagName = "socket-path"
sourcesFlagName = "sources"
syncProviderFlagName = "sync-provider"
uriFlagName = "uri"
)
Expand All @@ -44,7 +46,7 @@ func init() {
flags.StringP(serverCertPathFlagName, "c", "", "Server side tls certificate path")
flags.StringP(serverKeyPathFlagName, "k", "", "Server side tls key path")
flags.StringToStringP(providerArgsFlagName,
"a", nil, "Sync provider arguments as key values separated by =")
"a", nil, "DEPRECATED: Sync provider arguments as key values separated by =")
flags.StringSliceP(
uriFlagName, "f", []string{}, "Set a sync provider uri to read data from, this can be a filepath,"+
"url (http and grpc) or FeatureFlagConfiguration. When flag keys are duplicated across multiple providers the "+
Expand All @@ -53,11 +55,16 @@ func init() {
"Please note that if you are using filepath, flagd only supports files with `.yaml/.yml/.json` extension.",
)
flags.StringP(
bearerTokenFlagName, "b", "", "Set a bearer token to use for remote sync")
bearerTokenFlagName, "b", "", "DEPRECATED: Superseded by --sources.")
flags.StringSliceP(corsFlagName, "C", []string{}, "CORS allowed origins, * will allow all origins")
flags.StringP(
syncProviderFlagName, "y", "", "DEPRECATED: Set a sync provider e.g. filepath or remote",
)
flags.StringP(
sourcesFlagName, "s", "", "JSON representation of an array of SourceConfig objects. This object contains "+
"2 required fields, uri (string) and provider (string). Documentation for this object: "+
"https://github.com/open-feature/flagd/blob/main/docs/configuration/configuration.md#sync-provider-customisation",
)
flags.StringP(logFormatFlagName, "z", "console", "Set the logging format, e.g. console or json ")

_ = viper.BindPFlag(bearerTokenFlagName, flags.Lookup(bearerTokenFlagName))
Expand All @@ -71,6 +78,7 @@ func init() {
_ = viper.BindPFlag(serverKeyPathFlagName, flags.Lookup(serverKeyPathFlagName))
_ = viper.BindPFlag(socketPathFlagName, flags.Lookup(socketPathFlagName))
_ = viper.BindPFlag(syncProviderFlagName, flags.Lookup(syncProviderFlagName))
_ = viper.BindPFlag(sourcesFlagName, flags.Lookup(sourcesFlagName))
_ = viper.BindPFlag(uriFlagName, flags.Lookup(uriFlagName))
}

Expand Down Expand Up @@ -106,17 +114,40 @@ var startCmd = &cobra.Command{
rtLogger.Warn("DEPRECATED: The --evaluator flag has been deprecated. " +
"Docs: https://github.com/open-feature/flagd/blob/main/docs/configuration/configuration.md")
}

if viper.GetString(providerArgsFlagName) != "" {
rtLogger.Warn("DEPRECATED: The --sync-provider-args flag has been deprecated. " +
"Docs: https://github.com/open-feature/flagd/blob/main/docs/configuration/configuration.md")
}

syncProviders, err := runtime.SyncProvidersFromURIs(viper.GetStringSlice(uriFlagName))
if err != nil {
log.Fatal(err)
}

syncProvidersFromConfig := []sync.SourceConfig{}
if cfgFile == "" && viper.GetString(sourcesFlagName) != "" {
syncProvidersFromConfig, err = runtime.SyncProviderArgParse(viper.GetString(sourcesFlagName))
if err != nil {
log.Fatal(err)
}
} else {
err = viper.UnmarshalKey(sourcesFlagName, &syncProvidersFromConfig)
if err != nil {
log.Fatal(err)
}
}
syncProviders = append(syncProviders, syncProvidersFromConfig...)

// Build Runtime -----------------------------------------------------------
rt, err := runtime.FromConfig(logger, runtime.Config{
CORS: viper.GetStringSlice(corsFlagName),
MetricsPort: viper.GetInt32(metricsPortFlagName),
ProviderArgs: viper.GetStringMapString(providerArgsFlagName),
ServiceCertPath: viper.GetString(serverCertPathFlagName),
ServiceKeyPath: viper.GetString(serverKeyPathFlagName),
ServicePort: viper.GetInt32(portFlagName),
ServiceSocketPath: viper.GetString(socketPathFlagName),
SyncBearerToken: viper.GetString(bearerTokenFlagName),
SyncURI: viper.GetStringSlice(uriFlagName),
SyncProviders: syncProviders,
})
if err != nil {
rtLogger.Fatal(err.Error())
Expand Down
39 changes: 36 additions & 3 deletions docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Environment variable keys are uppercased, prefixed with `FLAGD_` and all `-` are

Config file expects the keys to have the exact naming as the flags.

### URI patterns
### <a name="uri-patterns"></a> URI patterns

Any URI passed to flagd via the `--uri` flag must follow one of the 4 following patterns to ensure that it is passed to the correct implementation:

Expand All @@ -22,7 +22,6 @@ Any URI passed to flagd via the `--uri` flag must follow one of the 4 following
| Grpc | `grpc://flag-source-url` | `grpc://my-flags-server` |



### Customising sync providers

Custom sync providers can be used to provide flag evaluation logic.
Expand All @@ -35,4 +34,38 @@ To use an existing FeatureFlagConfiguration custom resource, start flagD with th

```shell
flagd start --uri core.openfeature.dev/default/my_example
```
```

### Source Configuration

While a URI may be passed to flagd via the `--uri` flag, some implementations may require further configurations. In these cases the `--sources` flag should be used.
The flag takes a string argument, which should be a JSON representation of an array of `SourceConfig` objects. Alternatively, these configurations should be passed to
flagd via config file, specified using the `--config` flag.

| Field | Type |
|------------|------------------------------------|
| uri | required `string` | |
| provider | required `string` (`file`, `kubernetes`, `http` or `grpc`) |
| bearerToken | optional `string` |

The `uri` field values do not need to follow the [URI patterns](#uri-patterns), the provider type is instead derived from the provider field. If the prefix is supplied, it will be removed on startup without error.

Example start command using a filepath sync provider and the equivalent config file definition:
```sh
./flagd start --sources='[{"uri":"config/samples/example_flags.json","provider":"file"},{"uri":"http://my-flag-source.json","provider":"http","bearerToken":"bearer-dji34ld2l"}]{"uri":"default/my-flag-config","provider":"kubernetes"},{"uri":"grpc://my-flag-source:8080","provider":"grpc"}'
```

```yaml
sources:
- uri: config/samples/example_flags.json
provider: file
- uri: http://my-flag-source.json
provider: http
bearerToken: bearer-dji34ld2l
- uri: default/my-flag-config
provider: kubernetes
- uri: http://my-flag-source.json
provider: kubernetes
- uri: grpc://my-flag-source:8080
provider: grpc
```
5 changes: 3 additions & 2 deletions docs/configuration/flagd_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ flagd start [flags]
### Options

```
-b, --bearer-token string Set a bearer token to use for remote sync
-b, --bearer-token string DEPRECATED: Superseded by --sources.
-C, --cors-origin strings CORS allowed origins, * will allow all origins
-e, --evaluator string DEPRECATED: Set an evaluator e.g. json, yaml/yml.Please note that yaml/yml and json evaluations work the same (yaml/yml files are converted to json internally) (default "json")
-h, --help help for start
Expand All @@ -19,8 +19,9 @@ flagd start [flags]
-c, --server-cert-path string Server side tls certificate path
-k, --server-key-path string Server side tls key path
-d, --socket-path string Flagd socket path. With grpc the service will become available on this address. With http(s) the grpc-gateway proxy will use this address internally.
-s, --sources string JSON representation of an array of SourceConfig objects. This object contains 2 required fields, uri (string) and provider (string). Documentation for this object: https://github.com/open-feature/flagd/blob/main/docs/configuration/configuration.md#sync-provider-customisation
-y, --sync-provider string DEPRECATED: Set a sync provider e.g. filepath or remote
-a, --sync-provider-args stringToString Sync provider arguments as key values separated by = (default [])
-a, --sync-provider-args stringToString DEPRECATED: Sync provider arguments as key values separated by = (default [])
-f, --uri .yaml/.yml/.json Set a sync provider uri to read data from, this can be a filepath,url (http and grpc) or FeatureFlagConfiguration. When flag keys are duplicated across multiple providers the merge priority follows the index of the flag arguments, as such flags from the uri at index 0 take the lowest precedence, with duplicated keys being overwritten by those from the uri at index 1. Please note that if you are using filepath, flagd only supports files with .yaml/.yml/.json extension.
```

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
120 changes: 92 additions & 28 deletions pkg/runtime/from_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package runtime

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
Expand All @@ -20,6 +22,13 @@ import (
"go.uber.org/zap"
)

const (
syncProviderFile = "file"
syncProviderGrpc = "grpc"
syncProviderKubernetes = "kubernetes"
syncProviderHTTP = "http"
)

var (
regCrd *regexp.Regexp
regURL *regexp.Regexp
Expand All @@ -36,7 +45,11 @@ func init() {

func FromConfig(logger *logger.Logger, config Config) (*Runtime, error) {
s := store.NewFlags()
s.FlagSources = config.SyncURI
sources := []string{}
for _, sync := range config.SyncProviders {
sources = append(sources, sync.URI)
}
s.FlagSources = sources
rt := Runtime{
config: config,
Logger: logger.WithFields(zap.String("component", "runtime")),
Expand Down Expand Up @@ -67,67 +80,66 @@ func (r *Runtime) setService(logger *logger.Logger) {

func (r *Runtime) setSyncImplFromConfig(logger *logger.Logger) error {
rtLogger := logger.WithFields(zap.String("component", "runtime"))
r.SyncImpl = make([]sync.ISync, 0, len(r.config.SyncURI))
for _, uri := range r.config.SyncURI {
switch uriB := []byte(uri); {
case regFile.Match(uriB):
r.SyncImpl = make([]sync.ISync, 0, len(r.config.SyncProviders))
for _, syncProvider := range r.config.SyncProviders {
switch syncProvider.Provider {
case syncProviderFile:
r.SyncImpl = append(
r.SyncImpl,
r.newFile(uri, logger),
r.newFile(syncProvider, logger),
)
rtLogger.Debug(fmt.Sprintf("using filepath sync-provider for: %q", uri))
case regCrd.Match(uriB):
k, err := r.newK8s(uri, logger)
rtLogger.Debug(fmt.Sprintf("using filepath sync-provider for: %q", syncProvider.URI))
case syncProviderKubernetes:
k, err := r.newK8s(syncProvider.URI, logger)
if err != nil {
return err
}
r.SyncImpl = append(
r.SyncImpl,
k,
)
rtLogger.Debug(fmt.Sprintf("using kubernetes sync-provider for: %s", uri))
case regURL.Match(uriB):
rtLogger.Debug(fmt.Sprintf("using kubernetes sync-provider for: %s", syncProvider.URI))
case syncProviderHTTP:
r.SyncImpl = append(
r.SyncImpl,
r.newHTTP(uri, logger),
r.newHTTP(syncProvider, logger),
)
rtLogger.Debug(fmt.Sprintf("using remote sync-provider for: %q", uri))
case regGRPC.Match(uriB):
rtLogger.Debug(fmt.Sprintf("using remote sync-provider for: %s", syncProvider.URI))
case syncProviderGrpc:
r.SyncImpl = append(
r.SyncImpl,
r.newGRPC(uri, logger),
r.newGRPC(syncProvider, logger),
)
default:
return fmt.Errorf("invalid sync uri argument: %s, must start with 'file:', 'http(s)://', 'grpc://',"+
" or 'core.openfeature.dev'", uri)
" or 'core.openfeature.dev'", syncProvider.URI)
}
}
return nil
}

func (r *Runtime) newGRPC(uri string, logger *logger.Logger) *grpc.Sync {
func (r *Runtime) newGRPC(config sync.SourceConfig, logger *logger.Logger) *grpc.Sync {
return &grpc.Sync{
Target: grpc.URLToGRPCTarget(uri),
Target: grpc.URLToGRPCTarget(config.URI),
Logger: logger.WithFields(
zap.String("component", "sync"),
zap.String("sync", "grpc"),
),
}
}

func (r *Runtime) newHTTP(uri string, logger *logger.Logger) *httpSync.Sync {
func (r *Runtime) newHTTP(config sync.SourceConfig, logger *logger.Logger) *httpSync.Sync {
return &httpSync.Sync{
URI: uri,
BearerToken: r.config.SyncBearerToken,
URI: config.URI,
Client: &http.Client{
Timeout: time.Second * 10,
},
Logger: logger.WithFields(
zap.String("component", "sync"),
zap.String("sync", "remote"),
),
ProviderArgs: r.config.ProviderArgs,
Cron: cron.New(),
BearerToken: config.BearerToken,
Cron: cron.New(),
}
}

Expand All @@ -142,20 +154,72 @@ func (r *Runtime) newK8s(uri string, logger *logger.Logger) (*kubernetes.Sync, e
zap.String("sync", "kubernetes"),
),
regCrd.ReplaceAllString(uri, ""),
r.config.ProviderArgs,
reader,
dynamic,
), nil
}

func (r *Runtime) newFile(uri string, logger *logger.Logger) *file.Sync {
func (r *Runtime) newFile(config sync.SourceConfig, logger *logger.Logger) *file.Sync {
return &file.Sync{
URI: regFile.ReplaceAllString(uri, ""),
URI: config.URI,
Logger: logger.WithFields(
zap.String("component", "sync"),
zap.String("sync", "filepath"),
),
ProviderArgs: r.config.ProviderArgs,
Mux: &msync.RWMutex{},
Mux: &msync.RWMutex{},
}
}

func SyncProviderArgParse(syncProviders string) ([]sync.SourceConfig, error) {
syncProvidersParsed := []sync.SourceConfig{}
if err := json.Unmarshal([]byte(syncProviders), &syncProvidersParsed); err != nil {
return syncProvidersParsed, fmt.Errorf("unable to parse sync providers: %w", err)
}
for i, sp := range syncProvidersParsed {
if sp.URI == "" {
return syncProvidersParsed, errors.New("sync provider argument parse: uri is a required field")
}
if sp.Provider == "" {
return syncProvidersParsed, errors.New("sync provider argument parse: provider is a required field")
}
switch uriB := []byte(sp.URI); {
case regFile.Match(uriB):
syncProvidersParsed[i].URI = regFile.ReplaceAllString(syncProvidersParsed[i].URI, "")
case regCrd.Match(uriB):
syncProvidersParsed[i].URI = regCrd.ReplaceAllString(syncProvidersParsed[i].URI, "")
}
}
return syncProvidersParsed, nil
}

func SyncProvidersFromURIs(uris []string) ([]sync.SourceConfig, error) {
syncProvidersParsed := []sync.SourceConfig{}
for _, uri := range uris {
switch uriB := []byte(uri); {
case regFile.Match(uriB):
syncProvidersParsed = append(syncProvidersParsed, sync.SourceConfig{
URI: regFile.ReplaceAllString(uri, ""),
Provider: syncProviderFile,
})
case regCrd.Match(uriB):
syncProvidersParsed = append(syncProvidersParsed, sync.SourceConfig{
URI: regCrd.ReplaceAllString(uri, ""),
Provider: syncProviderKubernetes,
})
case regURL.Match(uriB):
syncProvidersParsed = append(syncProvidersParsed, sync.SourceConfig{
URI: uri,
Provider: syncProviderHTTP,
})
case regGRPC.Match(uriB):
syncProvidersParsed = append(syncProvidersParsed, sync.SourceConfig{
URI: uri,
Provider: syncProviderGrpc,
})
default:
return syncProvidersParsed, fmt.Errorf("invalid sync uri argument: %s, must start with 'file:', "+
"'http(s)://', 'grpc://', or 'core.openfeature.dev'", uri)
}
}
return syncProvidersParsed, nil
}
8 changes: 2 additions & 6 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ type Config struct {
ServiceCertPath string
ServiceKeyPath string

ProviderArgs sync.ProviderArgs
SyncURI []string
RemoteSyncType string
SyncBearerToken string

CORS []string
SyncProviders []sync.SourceConfig
CORS []string
}

func (r *Runtime) Start() error {
Expand Down
Loading

0 comments on commit 1d80039

Please sign in to comment.