Skip to content

Commit

Permalink
feat: grpc connection options to flagd configuration options (open-fe…
Browse files Browse the repository at this point in the history
…ature#532)

## This PR

Introduce configuration options for flagd grpc connection. Configuration
options,

`providerID` - Maps to `provider_id` field of the underlying grpc
requests
`selector` -  Maps to `selector ` field of the underlying grpc requests

GRPC request fields are explained here [1]

Related to open-feature#495 495


[1] -
https://buf.build/open-feature/flagd/file/main:sync/v1/sync_service.proto

---------

Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan authored Mar 20, 2023
1 parent d22406b commit aa74951
Show file tree
Hide file tree
Showing 11 changed files with 729 additions and 60 deletions.
5 changes: 3 additions & 2 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.19

require (
buf.build/gen/go/open-feature/flagd/bufbuild/connect-go v1.5.2-20230222100723-491ee098dd92.1
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230222100723-491ee098dd92.1
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.0-20230222100723-491ee098dd92.1
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230317150644-afd1cc2ef580.1
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.1-20230317150644-afd1cc2ef580.1
github.com/bufbuild/connect-go v1.5.2
github.com/diegoholiveira/jsonlogic/v3 v3.2.7
github.com/fsnotify/fsnotify v1.6.0
Expand Down Expand Up @@ -59,6 +59,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
22 changes: 8 additions & 14 deletions core/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
buf.build/gen/go/grpc-ecosystem/grpc-gateway/grpc/go v1.3.0-20220906183531-bc28b723cd77.1/go.mod h1:9Ec7rvBnjfZvU/TnWjtcSGgiLQ4B+U3B+6SnZgVTA7A=
buf.build/gen/go/grpc-ecosystem/grpc-gateway/protocolbuffers/go v1.28.1-20220906183531-bc28b723cd77.4/go.mod h1:92ejKVTiuvnKoAtRlpJpIxKfloI935DDqhs0NCRx+KM=
buf.build/gen/go/grpc-ecosystem/grpc-gateway/protocolbuffers/go v1.29.0-20220906183531-bc28b723cd77.1/go.mod h1:KdKLHnCB+HSTqTEczw09mmFoJtNrCF7urUbAakgSGhg=
buf.build/gen/go/open-feature/flagd/bufbuild/connect-go v1.5.2-20230222100723-491ee098dd92.1 h1:OGqJyjvn5VQsctIQEwB7JP8dR1mtN/G6LOTntrKzd+Y=
buf.build/gen/go/open-feature/flagd/bufbuild/connect-go v1.5.2-20230222100723-491ee098dd92.1/go.mod h1:wgQMLfjCfOMyzfdWA8HNnD1SA5HWyIJWPpJTc7dsZgw=
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230222100723-491ee098dd92.1 h1:9rPkEFUaz3lHMAuQFlxeYevJdsG5veBv5Pf8mPkYHMU=
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230222100723-491ee098dd92.1/go.mod h1:q82HHNffH+U9jBkQk8lPNSeQhyxz8MyyKgJJ/PKJ8nI=
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.28.1-20230222100723-491ee098dd92.4/go.mod h1:+Bnrjo56uVn/aBcLWchTveR8UeCj+KSJN4fE0xSmBNc=
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.0-20230222100723-491ee098dd92.1 h1:UDYoy5IeFX07wDFQT3dk77P3BKipoEmyuOGi7Ed0VAU=
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.0-20230222100723-491ee098dd92.1/go.mod h1:FAqNA3VWo+LSqIw6HrmATwdJnW5iWGYkrRF9vCokB2E=
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230317150644-afd1cc2ef580.1 h1:ky5p/GKnbkgTqzsI06rkxsgjb0qsljgPcHdd4xGR2dI=
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.1-20230317150644-afd1cc2ef580.1 h1:CWgXoLTajc7QagTz76kplcFnjjWIkxhhDdmKYtVShOU=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down Expand Up @@ -598,8 +594,9 @@ github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -623,11 +620,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.6.0 h1:9t9b9vRUbFq3C4qKFCGkVuq/fIHji802N1nrtkh1mNc=
github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
github.com/open-feature/open-feature-operator v0.2.29 h1:Ky/SMzwEiBV5x9qOfHTj1jl/CakPZNClRtoeSPqVbNo=
github.com/open-feature/open-feature-operator v0.2.29/go.mod h1:bQncVK7hvhj5QStPwexxQ1aArPwox2Y1vWrVei/qIFg=
github.com/onsi/ginkgo/v2 v2.8.1 h1:xFTEVwOFa1D/Ty24Ws1npBWkDYEV9BqZrsDxVrVkrrU=
github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754=
github.com/open-feature/open-feature-operator v0.2.31 h1:8MCULZPHGJpGVHIOm/vx07tpjDD6fMdNvOCdSFFhIaY=
github.com/open-feature/open-feature-operator v0.2.31/go.mod h1:4kdmeO7v33TFg/IxEH8N7c3Jply571G2Db12kXo8StA=
github.com/open-feature/schemas v0.2.8 h1:oA75hJXpOd9SFgmNI2IAxWZkwzQPUDm7Jyyh3q489wM=
Expand All @@ -653,6 +647,8 @@ github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo=
github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
Expand Down Expand Up @@ -1262,8 +1258,6 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.29.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM=
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Expand Down
4 changes: 3 additions & 1 deletion core/pkg/runtime/from_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func (r *Runtime) newGRPC(config sync.SourceConfig, logger *logger.Logger) *grpc
zap.String("component", "sync"),
zap.String("sync", "grpc"),
),
CertPath: config.CertPath,
CertPath: config.CertPath,
ProviderID: config.ProviderID,
Selector: config.Selector,
}
}

Expand Down
35 changes: 35 additions & 0 deletions core/pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,41 @@ func TestSyncProviderArgParse(t *testing.T) {
},
},
},
"multiple-syncs-with-options": {
in: `[
{"uri":"file:config/samples/example_flags.json","provider":"file"},
{"uri":"https://test.com","provider":"http","bearerToken":":)"},
{"uri":"host:port","provider":"grpc"},
{"uri":"host:port","provider":"grpcs","providerID":"appA","selector":"source=database"},
{"uri":"core.openfeature.dev/namespace/my-crd","provider":"kubernetes"}
]`,
expectErr: false,
out: []sync.SourceConfig{
{
URI: "config/samples/example_flags.json",
Provider: "file",
},
{
URI: "https://test.com",
Provider: "http",
BearerToken: ":)",
},
{
URI: "host:port",
Provider: "grpc",
},
{
URI: "host:port",
Provider: "grpcs",
ProviderID: "appA",
Selector: "source=database",
},
{
URI: "namespace/my-crd",
Provider: "kubernetes",
},
},
},
"empty": {
in: `[]`,
expectErr: false,
Expand Down
5 changes: 3 additions & 2 deletions core/pkg/sync/grpc/grpc_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var once msync.Once
type Sync struct {
URI string
ProviderID string
Selector string
CertPath string
Logger *logger.Logger

Expand Down Expand Up @@ -104,7 +105,7 @@ func (g *Sync) IsReady() bool {

func (g *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
// Initialize SyncFlags client. This fails if server connection establishment fails (ex:- grpc server offline)
syncClient, err := g.client.SyncFlags(ctx, &v1.SyncFlagsRequest{ProviderId: g.ProviderID})
syncClient, err := g.client.SyncFlags(ctx, &v1.SyncFlagsRequest{ProviderId: g.ProviderID, Selector: g.Selector})
if err != nil {
return err
}
Expand Down Expand Up @@ -163,7 +164,7 @@ func (g *Sync) connectWithRetry(

g.Logger.Warn(fmt.Sprintf("connection re-establishment attempt in-progress for grpc target: %s", g.URI))

syncClient, err := g.client.SyncFlags(ctx, &v1.SyncFlagsRequest{ProviderId: g.ProviderID})
syncClient, err := g.client.SyncFlags(ctx, &v1.SyncFlagsRequest{ProviderId: g.ProviderID, Selector: g.Selector})
if err != nil {
g.Logger.Debug(fmt.Sprintf("error opening service client: %s", err.Error()))
continue
Expand Down
5 changes: 3 additions & 2 deletions core/pkg/sync/grpc/grpc_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"errors"
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"io"
"log"
"net"
"os"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"golang.org/x/sync/errgroup"

"buf.build/gen/go/open-feature/flagd/grpc/go/sync/v1/syncv1grpc"
Expand Down
2 changes: 2 additions & 0 deletions core/pkg/sync/isync.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ type SourceConfig struct {

BearerToken string `json:"bearerToken,omitempty"`
CertPath string `json:"certPath,omitempty"`
ProviderID string `json:"providerID,omitempty"`
Selector string `json:"selector,omitempty"`
}
19 changes: 13 additions & 6 deletions docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ The flag takes a string argument, which should be a JSON representation of an ar
Alternatively, these configurations should be passed to
flagd via config file, specified using the `--config` flag.

| Field | Type | Note |
|-------------|------------------------------------------------------------|----------------------------------------------------|
| uri | required `string` | |
| provider | required `string` (`file`, `kubernetes`, `http` or `grpc`) | |
| bearerToken | optional `string` | Used for http sync |
| certPath | optional `string` | Used for grpcs sync when TLS certificate is needed |
| Field | Type | Note |
|-------------|------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
| uri | required `string` | |
| provider | required `string` (`file`, `kubernetes`, `http` or `grpc`) | |
| bearerToken | optional `string` | Used for http sync |
| providerID | optional `string` | Value binds to grpc connection's providerID field. GRPC server implementations may use this to identify connecting flagd instance |
| selector | optional `string` | Value binds to grpc connection's selector field. GRPC server implementations may use this to filter flag configurations |
| certPath | optional `string` | Used for grpcs sync when TLS certificate is needed. If not provided, system certificates will be used for TLS connection |

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.
Expand All @@ -77,4 +79,9 @@ sources:
- uri: grpcs://my-flag-source:8080
provider: grpc
certPath: /certs/ca.cert
- uri: grpcs://my-flag-source:8080
provider: grpc
certPath: /certs/ca.cert
providerID: flagd-weatherapp-sidecar
selector: 'source=database,app=weatherapp'
```
2 changes: 1 addition & 1 deletion flagd/cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"strings"

"github.com/spf13/cobra/doc"
)

Expand All @@ -16,4 +17,3 @@ func GenerateDoc(path string) error {
}
return doc.GenMarkdownTreeCustom(rootCmd, path, filePrepender, linkHandler)
}

12 changes: 6 additions & 6 deletions flagd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require (

require (
buf.build/gen/go/open-feature/flagd/bufbuild/connect-go v1.5.2-20230222100723-491ee098dd92.1 // indirect
buf.build/gen/go/open-feature/flagd/grpc/go v1.2.0-20230222100723-491ee098dd92.4 // indirect
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.28.1-20230222100723-491ee098dd92.4 // indirect
buf.build/gen/go/open-feature/flagd/grpc/go v1.3.0-20230317150644-afd1cc2ef580.1 // indirect
buf.build/gen/go/open-feature/flagd/protocolbuffers/go v1.29.1-20230317150644-afd1cc2ef580.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/connect-go v1.5.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down Expand Up @@ -68,11 +68,11 @@ require (
github.com/zeebo/xxh3 v1.0.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230221151758-ace64dc21148 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit aa74951

Please sign in to comment.