Skip to content

Commit c4e030e

Browse files
AlexsJonesjames-milliganbeeme1mr
authored
feat: kubernetes sync (#157)
open-feature/ofep#31 <img width="1512" alt="Screenshot 2022-08-30 at 15 54 16" src="https://user-images.githubusercontent.com/1235925/187469966-d3c137bd-1f1b-4ee7-9512-848951ec780b.png"> Signed-off-by: James-Milligan <[email protected]> Signed-off-by: Alex Jones <[email protected]> Signed-off-by: Alex Jones <[email protected]> Co-authored-by: James Milligan <[email protected]> Co-authored-by: Michael Beemer <[email protected]>
1 parent c977eb7 commit c4e030e

File tree

14 files changed

+527
-28
lines changed

14 files changed

+527
-28
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ returns
252252
{ "value": true, "reason": "TARGETING_MATCH", "variant": "off" }
253253
```
254254

255+
### [Customization of sync providers](./docs/configuration.md)
256+
255257
### [Reusable targeting rules](./docs/reusable_targeting_rules.md)
256258

257259
### [Fractional Evaluation](./docs/fractional_evaluation.md)
@@ -261,3 +263,4 @@ returns
261263
<a href="https://github.com/open-feature/flagd/graphs/contributors">
262264
<img src="https://contrib.rocks/image?repo=open-feature/flagd" />
263265
</a>
266+

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var (
1313
Version string
1414
Commit string
1515
Date string
16+
Debug bool
1617
)
1718

1819
var rootCmd = &cobra.Command{
@@ -45,7 +46,7 @@ func init() {
4546
// Here you will define your flags and configuration settings.
4647
// Cobra supports persistent flags, which, if defined here,
4748
// will be global for your application.
48-
49+
rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "x", false, "verbose logging")
4950
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.agent.yaml)")
5051
}
5152

cmd/start.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
serviceProviderFlagName = "service-provider"
1616
socketPathFlagName = "socket-path"
1717
syncProviderFlagName = "sync-provider"
18+
providerArgsFlagName = "sync-provider-args"
1819
evaluatorFlagName = "evaluator"
1920
serverCertPathFlagName = "server-cert-path"
2021
serverKeyPathFlagName = "server-key-path"
@@ -40,6 +41,8 @@ func init() {
4041
flags.StringP(evaluatorFlagName, "e", "json", "Set an evaluator e.g. json")
4142
flags.StringP(serverCertPathFlagName, "c", "", "Server side tls certificate path")
4243
flags.StringP(serverKeyPathFlagName, "k", "", "Server side tls key path")
44+
flags.StringToStringP(providerArgsFlagName,
45+
"a", nil, "Sync provider arguments as key values separated by =")
4346
flags.StringSliceP(
4447
uriFlagName, "f", []string{}, "Set a sync provider uri to read data from this can be a filepath or url. "+
4548
"Using multiple providers is supported where collisions between "+
@@ -51,13 +54,12 @@ func init() {
5154
_ = viper.BindPFlag(socketPathFlagName, flags.Lookup(socketPathFlagName))
5255
_ = viper.BindPFlag(serviceProviderFlagName, flags.Lookup(serviceProviderFlagName))
5356
_ = viper.BindPFlag(syncProviderFlagName, flags.Lookup(syncProviderFlagName))
57+
_ = viper.BindPFlag(providerArgsFlagName, flags.Lookup(providerArgsFlagName))
5458
_ = viper.BindPFlag(evaluatorFlagName, flags.Lookup(evaluatorFlagName))
5559
_ = viper.BindPFlag(serverCertPathFlagName, flags.Lookup(serverCertPathFlagName))
5660
_ = viper.BindPFlag(serverKeyPathFlagName, flags.Lookup(serverKeyPathFlagName))
5761
_ = viper.BindPFlag(uriFlagName, flags.Lookup(uriFlagName))
5862
_ = viper.BindPFlag(bearerTokenFlagName, flags.Lookup(bearerTokenFlagName))
59-
60-
_ = startCmd.MarkFlagRequired("uri")
6163
}
6264

6365
// startCmd represents the start command
@@ -69,8 +71,11 @@ var startCmd = &cobra.Command{
6971
// Configure loggers -------------------------------------------------------
7072
log.SetFormatter(&log.JSONFormatter{})
7173
log.SetOutput(os.Stdout)
72-
log.SetLevel(log.DebugLevel)
73-
74+
if Debug {
75+
log.SetLevel(log.DebugLevel)
76+
} else {
77+
log.SetLevel(log.InfoLevel)
78+
}
7479
// Build Runtime -----------------------------------------------------------
7580
rt, err := runtime.FromConfig(runtime.Config{
7681
ServiceProvider: viper.GetString(serviceProviderFlagName),
@@ -80,6 +85,7 @@ var startCmd = &cobra.Command{
8085
ServiceKeyPath: viper.GetString(serverKeyPathFlagName),
8186

8287
SyncProvider: viper.GetString(syncProviderFlagName),
88+
ProviderArgs: viper.GetStringMapString(providerArgsFlagName),
8389
SyncURI: viper.GetStringSlice(uriFlagName),
8490
SyncBearerToken: viper.GetString(bearerTokenFlagName),
8591

docs/configuration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Supported flags are as follows (result of running `./flagd start --help`):
1313
-c, --server-cert-path string Server side tls certificate path
1414
-k, --server-key-path string Server side tls key path
1515
-s, --service-provider string Set a service provider e.g. http or grpc (default "http")
16+
-a, --sync-provider-args Sync provider arguments as key values separated by =
1617
-d, --socket-path string Set the 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
1718
-y, --sync-provider string Set a sync provider e.g. filepath or remote (default "filepath")
1819
-f, --uri strings Set a sync provider uri to read data from this can be a filepath or url. Using multiple providers is supported where collisions between flags with the same key, the later will be used.
@@ -22,3 +23,25 @@ Environment variable keys are uppercased, prefixed with `FLAGD_` and all `-` are
2223
`sync-provider` in environment variable form is `FLAGD_SYNC_PROVIDER`.
2324

2425
Config file expects the keys to have the exact naming as the flags.
26+
27+
28+
### Customising sync providers
29+
30+
Custom sync providers can be used to provide flag evaluation logic.
31+
32+
#### Kubernetes provider
33+
34+
The Kubernetes provider allows flagD to connect to a Kubernetes cluster and evaluate flags against a specified FeatureFlagConfiguration resource as defined within the [open-feature-operator](https://github.com/open-feature/open-feature-operator/blob/main/apis/core/v1alpha1/featureflagconfiguration_types.go) spec.
35+
36+
To use an existing FeatureFlagConfiguration custom resource, start flagD with the following command:
37+
38+
```shell
39+
flagd start --sync-provider=kubernetes --sync-provider-args=featureflagconfiguration=my-example --sync-provider-args=namespace=default
40+
```
41+
42+
An additional optional flag `refreshtime` can be applied to shorten the cache refresh when using the Kubernetes provider ( The default is 5s ). As an example:
43+
44+
```shell
45+
flagd start --sync-provider=kubernetes --sync-provider-args=featureflagconfiguration=my-example --sync-provider-args=namespace=default
46+
--sync-provider-args=refreshtime=1s
47+
```

go.mod

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ require (
1111
github.com/golang/mock v1.6.0
1212
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.1
1313
github.com/mattn/go-colorable v0.1.12
14-
github.com/open-feature/schemas v0.0.0-20220809125026-ba7ea0aa4841
14+
github.com/open-feature/open-feature-operator v0.0.10-0.20220826061622-a6421d66936a
15+
github.com/open-feature/schemas v0.0.0-20220809125333-185e3bd77775
1516
github.com/robfig/cron v1.2.0
1617
github.com/sirupsen/logrus v1.8.1
1718
github.com/soheilhy/cmux v0.1.5
@@ -24,25 +25,46 @@ require (
2425
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
2526
google.golang.org/grpc v1.48.0
2627
google.golang.org/protobuf v1.28.1
28+
k8s.io/apimachinery v0.25.0
29+
k8s.io/client-go v0.25.0
30+
sigs.k8s.io/controller-runtime v0.11.0
2731
)
2832

29-
require gopkg.in/yaml.v2 v2.4.0 // indirect
30-
3133
require (
34+
github.com/PuerkitoBio/purell v1.1.1 // indirect
35+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
3236
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 // indirect
3337
github.com/davecgh/go-spew v1.1.1 // indirect
38+
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
39+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
40+
github.com/go-logr/logr v1.2.3 // indirect
41+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
42+
github.com/go-openapi/jsonreference v0.19.5 // indirect
43+
github.com/go-openapi/swag v0.21.1 // indirect
44+
github.com/gogo/protobuf v1.3.2 // indirect
3445
github.com/golang/protobuf v1.5.2 // indirect
46+
github.com/google/gnostic v0.5.7-v3refs // indirect
47+
github.com/google/go-cmp v0.5.8 // indirect
48+
github.com/google/gofuzz v1.1.0 // indirect
3549
github.com/google/uuid v1.3.0 // indirect
3650
github.com/hashicorp/hcl v1.0.0 // indirect
51+
github.com/imdario/mergo v0.3.12 // indirect
3752
github.com/inconshreveable/mousetrap v1.0.0 // indirect
53+
github.com/josharian/intern v1.0.0 // indirect
54+
github.com/json-iterator/go v1.1.12 // indirect
3855
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
3956
github.com/magiconair/properties v1.8.6 // indirect
57+
github.com/mailru/easyjson v0.7.7 // indirect
4058
github.com/mattn/go-isatty v0.0.14 // indirect
4159
github.com/mitchellh/copystructure v1.2.0 // indirect
4260
github.com/mitchellh/mapstructure v1.5.0 // indirect
4361
github.com/mitchellh/reflectwalk v1.0.2 // indirect
62+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
63+
github.com/modern-go/reflect2 v1.0.2 // indirect
64+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4465
github.com/pelletier/go-toml v1.9.5 // indirect
4566
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
67+
github.com/pkg/errors v0.9.1 // indirect
4668
github.com/pmezard/go-difflib v1.0.0 // indirect
4769
github.com/spf13/afero v1.8.2 // indirect
4870
github.com/spf13/cast v1.5.0 // indirect
@@ -51,10 +73,23 @@ require (
5173
github.com/subosito/gotenv v1.3.0 // indirect
5274
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
5375
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
54-
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
55-
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
76+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
77+
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect
78+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
79+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
5680
golang.org/x/text v0.3.7 // indirect
81+
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
82+
google.golang.org/appengine v1.6.7 // indirect
5783
google.golang.org/genproto v0.0.0-20220728213248-dd149ef739b9 // indirect
84+
gopkg.in/inf.v0 v0.9.1 // indirect
5885
gopkg.in/ini.v1 v1.66.4 // indirect
86+
gopkg.in/yaml.v2 v2.4.0 // indirect
5987
gopkg.in/yaml.v3 v3.0.1 // indirect
88+
k8s.io/api v0.25.0 // indirect
89+
k8s.io/klog/v2 v2.70.1 // indirect
90+
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
91+
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
92+
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
93+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
94+
sigs.k8s.io/yaml v1.3.0 // indirect
6095
)

0 commit comments

Comments
 (0)