Skip to content

Commit

Permalink
feat(flagd): Support supplying providerID for in-process resolver as …
Browse files Browse the repository at this point in the history
…an option (#626)

Signed-off-by: Maks Osowski <maks@google.com>
  • Loading branch information
cupofcat authored Feb 18, 2025
1 parent 210e397 commit 2d35083
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
9 changes: 6 additions & 3 deletions providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Configuration can be provided as constructor options or as environment variables
| WithLRUCache<br/>WithBasicInMemoryCache<br/>WithoutCache | FLAGD_CACHE | string (lru, mem, disabled) | lru | rpc |
| WithEventStreamConnectionMaxAttempts | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
| WithOfflineFilePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | string | "" | in-process |
| WithProviderID | FLAGD_SOURCE_PROVIDER_ID | string | "" | in-process |
| WithSelector | FLAGD_SOURCE_SELECTOR | string | "" | in-process |

### Overriding behavior

Expand Down Expand Up @@ -158,9 +160,10 @@ For general information on events, see the [official documentation](https://open

The flagd provider currently support following flag evaluation metadata,

| Field | Type | Value |
|---------|--------|---------------------------------------------------|
| `scope` | string | "selector" set for the associated source in flagd |
| Field | Type | Value |
|--------------|--------|-----------------------------------------------------|
| `scope` | string | "selector" set for the associated source in flagd |
| `providerID` | string | "providerID" set for the associated source in flagd |

## Logging

Expand Down
6 changes: 6 additions & 0 deletions providers/flagd/pkg/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
flagdMaxCacheSizeEnvironmentVariableName = "FLAGD_MAX_CACHE_SIZE"
flagdMaxEventStreamRetriesEnvironmentVariableName = "FLAGD_MAX_EVENT_STREAM_RETRIES"
flagdResolverEnvironmentVariableName = "FLAGD_RESOLVER"
flagdSourceProviderIDEnvironmentVariableName = "FLAGD_SOURCE_PROVIDER_ID"
flagdSourceSelectorEnvironmentVariableName = "FLAGD_SOURCE_SELECTOR"
flagdOfflinePathEnvironmentVariableName = "FLAGD_OFFLINE_FLAG_SOURCE_PATH"
flagdTargetUriEnvironmentVariableName = "FLAGD_TARGET_URI"
Expand All @@ -51,6 +52,7 @@ type providerConfiguration struct {
Port uint16
TargetUri string
Resolver ResolverType
ProviderID string
Selector string
SocketPath string
TLSEnabled bool
Expand Down Expand Up @@ -161,6 +163,10 @@ func (cfg *providerConfiguration) updateFromEnvVar() {
cfg.OfflineFlagSourcePath = offlinePath
}

if providerID := os.Getenv(flagdSourceProviderIDEnvironmentVariableName); providerID != "" {
cfg.ProviderID = providerID
}

if selector := os.Getenv(flagdSourceSelectorEnvironmentVariableName); selector != "" {
cfg.Selector = selector
}
Expand Down
8 changes: 8 additions & 0 deletions providers/flagd/pkg/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func NewProvider(opts ...ProviderOption) *Provider {
service = process.NewInProcessService(process.Configuration{
Host: provider.providerConfiguration.Host,
Port: provider.providerConfiguration.Port,
ProviderID: provider.providerConfiguration.ProviderID,
Selector: provider.providerConfiguration.Selector,
TargetUri: provider.providerConfiguration.TargetUri,
TLSEnabled: provider.providerConfiguration.TLSEnabled,
Expand Down Expand Up @@ -327,6 +328,13 @@ func WithSelector(selector string) ProviderOption {
}
}

// WithProviderID sets the providerID to be used for InProcess flag sync calls
func WithProviderID(providerID string) ProviderOption {
return func(p *Provider) {
p.providerConfiguration.ProviderID = providerID
}
}

// FromEnv sets the provider configuration from environment variables (if set)
func FromEnv() ProviderOption {
return func(p *Provider) {
Expand Down
28 changes: 28 additions & 0 deletions providers/flagd/pkg/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestNewProvider(t *testing.T) {
expectOtelIntercept bool
expectSocketPath string
expectTlsEnabled bool
expectProviderID string
expectSelector string
expectCustomSyncProvider sync.ISync
expectCustomSyncProviderUri string
options []ProviderOption
Expand Down Expand Up @@ -171,6 +173,22 @@ func TestNewProvider(t *testing.T) {
WithCustomSyncProvider(customSyncProvider),
},
},
{
name: "with selector and providerID with in-process resolver",
expectedResolver: inProcess,
expectHost: defaultHost,
expectPort: defaultInProcessPort,
expectCacheType: defaultCache,
expectCacheSize: defaultMaxCacheSize,
expectMaxRetries: defaultMaxEventStreamRetries,
expectProviderID: "testProvider",
expectSelector: "flags=test",
options: []ProviderOption{
WithInProcessResolver(),
WithSelector("flags=test"),
WithProviderID("testProvider"),
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -229,6 +247,16 @@ func TestNewProvider(t *testing.T) {
test.expectTargetUri, config.TargetUri)
}

if config.Selector != test.expectSelector {
t.Errorf("incorrect configuration Selector, expected %v, got %v",
test.expectSelector, config.Selector)
}

if config.ProviderID != test.expectProviderID {
t.Errorf("incorrect configuration ProviderID, expected %v, got %v",
test.expectProviderID, config.ProviderID)
}

if config.CustomSyncProvider != test.expectCustomSyncProvider {
t.Errorf("incorrect configuration CustomSyncProvider, expected %v, got %v",
test.expectCustomSyncProvider, config.CustomSyncProvider)
Expand Down
5 changes: 5 additions & 0 deletions providers/flagd/pkg/service/in_process/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Configuration struct {
Host any
Port any
TargetUri string
ProviderID string
Selector string
TLSEnabled bool
OfflineFlagSource string
Expand All @@ -54,6 +55,9 @@ func NewInProcessService(cfg Configuration) *InProcess {
svcMetadata = make(model.Metadata, 1)
svcMetadata["scope"] = cfg.Selector
}
if cfg.ProviderID != "" {
svcMetadata["providerID"] = cfg.ProviderID
}

flagStore := store.NewFlags()
flagStore.FlagSources = append(flagStore.FlagSources, uri)
Expand Down Expand Up @@ -301,6 +305,7 @@ func makeSyncProvider(cfg Configuration, log *logger.Logger) (sync.ISync, string
CredentialBuilder: &credentials.CredentialBuilder{},
Logger: log,
Secure: cfg.TLSEnabled,
ProviderID: cfg.ProviderID,
Selector: cfg.Selector,
URI: uri,
}, uri
Expand Down

0 comments on commit 2d35083

Please sign in to comment.