Skip to content
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

feat: introduce per-sync configurations #448

Merged
merged 36 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d79dafe
wip
james-milligan Feb 28, 2023
ee8d823
docs
james-milligan Feb 28, 2023
35a84b5
docs
james-milligan Feb 28, 2023
e88e47b
linting
james-milligan Feb 28, 2023
484814f
docs
james-milligan Feb 28, 2023
cd3ad8c
Merge branch 'main' into sync-provider-2
james-milligan Feb 28, 2023
f8aef13
bug fix
james-milligan Feb 28, 2023
36c1223
Merge branch 'sync-provider-2' of https://github.com/james-milligan/f…
james-milligan Feb 28, 2023
7ee052d
Merge branch 'main' into sync-provider-2
james-milligan Feb 28, 2023
71f09e7
test coverage
james-milligan Mar 1, 2023
eb21c91
Merge branch 'sync-provider-2' of https://github.com/james-milligan/f…
james-milligan Mar 1, 2023
2d23994
Merge branch 'main' into sync-provider-2
james-milligan Mar 1, 2023
a0c844e
Apply suggestions from code review
james-milligan Mar 1, 2023
c4dee0e
name update
james-milligan Mar 1, 2023
5ecdb19
Merge branch 'main' into sync-provider-2
james-milligan Mar 1, 2023
c5864ac
merge conflicts
james-milligan Mar 2, 2023
73ce052
conflict fix
james-milligan Mar 2, 2023
5c3942e
conflict fix
james-milligan Mar 2, 2023
de6b6ba
bug fix
james-milligan Mar 2, 2023
2c6980d
Merge branch 'sync-provider-2' of https://github.com/james-milligan/f…
james-milligan Mar 2, 2023
86d392d
doc fix
james-milligan Mar 2, 2023
3e7c41a
deprecation warning
james-milligan Mar 2, 2023
1f7f7ae
removed config object from syncs
james-milligan Mar 7, 2023
a522f56
rename sync providers to sources
james-milligan Mar 7, 2023
5019af8
reintroduce deprecation of --sync-provider
james-milligan Mar 7, 2023
33644c3
rename func
james-milligan Mar 7, 2023
d0d77f1
conflict fix
james-milligan Mar 7, 2023
000509d
cleanup
james-milligan Mar 8, 2023
aa54685
linting
james-milligan Mar 8, 2023
46b1ed4
Merge http://github.com/open-feature/flagd into sync-provider-2
james-milligan Mar 8, 2023
cfbef90
remove source
james-milligan Mar 8, 2023
12b8f87
deprecate bearer token
james-milligan Mar 8, 2023
31e1126
flag doc fix
james-milligan Mar 8, 2023
1eee15c
merge
james-milligan Mar 8, 2023
4ad480c
conflict resolutions
james-milligan Mar 8, 2023
2dd6912
Merge branch 'main' into sync-provider-2
james-milligan Mar 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
james-milligan marked this conversation as resolved.
Show resolved Hide resolved
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. " +
james-milligan marked this conversation as resolved.
Show resolved Hide resolved
"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))
james-milligan marked this conversation as resolved.
Show resolved Hide resolved
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