Skip to content

Commit 3839b8c

Browse files
fix(relayproxy): configure kafka addresses with env variables (#3303)
* fix(relayproxy): configure kafka addresses with env variables Signed-off-by: Thomas Poignant <[email protected]> * adding test + fixing lint Signed-off-by: Thomas Poignant <[email protected]> --------- Signed-off-by: Thomas Poignant <[email protected]>
1 parent db526f5 commit 3839b8c

File tree

3 files changed

+163
-18
lines changed

3 files changed

+163
-18
lines changed

cmd/relayproxy/config/config.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,35 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
9797
}
9898

9999
// Map environment variables
100-
_ = k.Load(env.ProviderWithValue("", ".", func(s string, v string) (string, interface{}) {
100+
_ = k.Load(mapEnvVariablesProvider(log), nil)
101+
_ = k.Set("version", version)
102+
103+
proxyConf := &Config{}
104+
errUnmarshal := k.Unmarshal("", &proxyConf)
105+
if errUnmarshal != nil {
106+
return nil, errUnmarshal
107+
}
108+
109+
if proxyConf.Exporters != nil {
110+
for i := range *proxyConf.Exporters {
111+
(*proxyConf.Exporters)[i].Kafka.Addresses = stringToArray(
112+
(*proxyConf.Exporters)[i].Kafka.Addresses,
113+
)
114+
}
115+
}
116+
117+
if proxyConf.Debug {
118+
log.Warn(
119+
"Option Debug that you are using in your configuration file is deprecated" +
120+
"and will be removed in future versions." +
121+
"Please use logLevel: debug to continue to run the relay-proxy with debug logs.")
122+
}
123+
124+
return proxyConf, nil
125+
}
126+
127+
func mapEnvVariablesProvider(log *zap.Logger) koanf.Provider {
128+
return env.ProviderWithValue("", ".", func(s string, v string) (string, interface{}) {
101129
if strings.HasPrefix(s, "RETRIEVERS") ||
102130
strings.HasPrefix(s, "NOTIFIERS") ||
103131
strings.HasPrefix(s, "EXPORTERS") {
@@ -115,6 +143,10 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
115143
return s, v
116144
}
117145

146+
if strings.HasPrefix(s, "EXPORTER_KAFKA_ADDRESSES") {
147+
return "exporter.kafka.addresses", strings.Split(v, ",")
148+
}
149+
118150
if strings.HasPrefix(s, "AUTHORIZEDKEYS_EVALUATION") {
119151
return "authorizedKeys.evaluation", strings.Split(v, ",")
120152
}
@@ -128,24 +160,14 @@ func New(flagSet *pflag.FlagSet, log *zap.Logger, version string) (*Config, erro
128160
}
129161

130162
return strings.ReplaceAll(strings.ToLower(s), "_", "."), v
131-
}), nil)
132-
133-
_ = k.Set("version", version)
134-
135-
proxyConf := &Config{}
136-
errUnmarshal := k.Unmarshal("", &proxyConf)
137-
if errUnmarshal != nil {
138-
return nil, errUnmarshal
139-
}
163+
})
164+
}
140165

141-
if proxyConf.Debug {
142-
log.Warn(
143-
"Option Debug that you are using in your configuration file is deprecated" +
144-
"and will be removed in future versions." +
145-
"Please use logLevel: debug to continue to run the relay-proxy with debug logs.")
166+
func stringToArray(item []string) []string {
167+
if len(item) > 0 {
168+
return strings.Split(item[0], ",")
146169
}
147-
148-
return proxyConf, nil
170+
return item
149171
}
150172

151173
func parseOtelResourceAttributes(attributes string, log *zap.Logger) {

cmd/relayproxy/config/config_test.go

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
13+
"github.com/thomaspoignant/go-feature-flag/exporter/kafkaexporter"
1314
"go.uber.org/zap"
1415
"go.uber.org/zap/zapcore"
1516
)
@@ -902,7 +903,6 @@ func TestMergeConfig_FromOSEnv(t *testing.T) {
902903
},
903904
{
904905
HTTPHeaders: map[string][]string{
905-
906906
"authorization": {
907907
"test1",
908908
},
@@ -939,6 +939,106 @@ func TestMergeConfig_FromOSEnv(t *testing.T) {
939939
"RETRIEVERS_2_HEADERS_X-GOFF-CUSTOM": "custom",
940940
},
941941
},
942+
{
943+
name: "Change kafka exporter",
944+
fileLocation: "../testdata/config/validate-array-env-file.yaml",
945+
want: &config.Config{
946+
ListenPort: 1031,
947+
PollingInterval: 1000,
948+
FileFormat: "yaml",
949+
Host: "localhost",
950+
Retrievers: &[]config.RetrieverConf{
951+
{
952+
Kind: "http",
953+
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
954+
},
955+
{
956+
Kind: "file",
957+
Path: "examples/retriever_file/flags.goff.yaml",
958+
HTTPHeaders: map[string][]string{
959+
"token": {
960+
"11213123",
961+
},
962+
},
963+
},
964+
},
965+
Exporter: &config.ExporterConf{
966+
Kind: "kafka",
967+
Kafka: kafkaexporter.Settings{
968+
Addresses: []string{"localhost:19092", "localhost:19093"},
969+
},
970+
},
971+
AuthorizedKeys: config.APIKeys{
972+
Admin: []string{
973+
"apikey3",
974+
},
975+
Evaluation: []string{
976+
"apikey1",
977+
"apikey2",
978+
},
979+
},
980+
StartWithRetrieverError: false,
981+
Version: "1.X.X",
982+
EnableSwagger: true,
983+
LogLevel: "info",
984+
},
985+
wantErr: assert.NoError,
986+
envVars: map[string]string{
987+
"EXPORTER_KAFKA_ADDRESSES": "localhost:19092,localhost:19093",
988+
"EXPORTER_KIND": "kafka",
989+
},
990+
},
991+
{
992+
name: "Change kafka exporters",
993+
fileLocation: "../testdata/config/valid-env-exporters-kafka.yaml",
994+
want: &config.Config{
995+
ListenPort: 1031,
996+
PollingInterval: 1000,
997+
FileFormat: "yaml",
998+
Host: "localhost",
999+
Retrievers: &[]config.RetrieverConf{
1000+
{
1001+
Kind: "http",
1002+
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
1003+
},
1004+
{
1005+
Kind: "file",
1006+
Path: "examples/retriever_file/flags.goff.yaml",
1007+
HTTPHeaders: map[string][]string{
1008+
"token": {
1009+
"11213123",
1010+
},
1011+
},
1012+
},
1013+
},
1014+
Exporters: &[]config.ExporterConf{
1015+
{
1016+
Kind: "kafka",
1017+
Kafka: kafkaexporter.Settings{
1018+
Addresses: []string{"localhost:19092", "localhost:19093"},
1019+
Topic: "svc-goff.evaluation",
1020+
},
1021+
},
1022+
},
1023+
AuthorizedKeys: config.APIKeys{
1024+
Admin: []string{
1025+
"apikey3",
1026+
},
1027+
Evaluation: []string{
1028+
"apikey1",
1029+
"apikey2",
1030+
},
1031+
},
1032+
StartWithRetrieverError: false,
1033+
Version: "1.X.X",
1034+
EnableSwagger: true,
1035+
LogLevel: "info",
1036+
},
1037+
wantErr: assert.NoError,
1038+
envVars: map[string]string{
1039+
"EXPORTERS_0_KAFKA_ADDRESSES": "localhost:19092,localhost:19093",
1040+
},
1041+
},
9421042
{
9431043
name: "Valid YAML with OTel config",
9441044
fileLocation: "../testdata/config/valid-otel.yaml",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
listen: 1031
2+
pollingInterval: 1000
3+
startWithRetrieverError: false
4+
retrievers:
5+
- kind: http
6+
url: https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml
7+
- kind: file
8+
path: examples/retriever_file/flags.goff.yaml
9+
headers:
10+
token: 11213123
11+
exporters:
12+
- kind: kafka
13+
kafka:
14+
topic: svc-goff.evaluation
15+
addresses: [ kafka:9092 ]
16+
enableSwagger: true
17+
authorizedKeys:
18+
evaluation:
19+
- apikey1 # owner: userID1
20+
- apikey2 # owner: userID2
21+
admin:
22+
- apikey3
23+
logLevel: info

0 commit comments

Comments
 (0)