Skip to content

Commit cebda36

Browse files
authored
Envconfig settings (#10694)
Signed-off-by: Jenny Shu <[email protected]>
1 parent 06ff8b9 commit cebda36

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

internal/kgateway/extensions2/plugins/istio/plugin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535

3636
type IstioSettings struct {
3737
EnableIstioIntegration bool
38-
EnableAutoMTLS bool
38+
EnableAutoMtls bool
3939
EnableIstioSidecarOnGateway bool
4040
}
4141

@@ -67,7 +67,7 @@ func NewPlugin(ctx context.Context, commoncol *common.CommonCollections) extensi
6767
// when translating upstreams. if we want we can add the gateway to the context of PerClientProcessUpstream
6868
sidecarEnabled := envutils.IsEnvTruthy(ourwellknown.IstioInjectionEnabled)
6969
istioSettings := IstioSettings{
70-
EnableAutoMTLS: commoncol.Settings.EnableAutoMTLS,
70+
EnableAutoMtls: commoncol.Settings.EnableAutoMtls,
7171
EnableIstioIntegration: commoncol.Settings.EnableIstioIntegration,
7272
EnableIstioSidecarOnGateway: sidecarEnabled,
7373
}
@@ -114,7 +114,7 @@ func (p istioPlugin) processUpstream(ctx context.Context, ir ir.PolicyIR, in ir.
114114
// 1) automtls is enabled on the settings
115115
// 2) the upstream has not disabled auto mtls
116116
// 3) the upstream has no sslConfig
117-
if st.EnableAutoMTLS && !isDisabledForUpstream(in) && !doesClusterHaveSslConfigPresent(out) {
117+
if st.EnableAutoMtls && !isDisabledForUpstream(in) && !doesClusterHaveSslConfigPresent(out) {
118118
// Istio automtls config is not applied if istio integration is disabled on the helm chart.
119119
// When istio integration is disabled via istioSds.enabled=false, there is no sds or istio-proxy sidecar present
120120
if !st.EnableIstioIntegration {

internal/kgateway/extensions2/settings/settings.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
type Settings struct {
8-
EnableIstioIntegration bool
9-
EnableAutoMTLS bool
10-
StsClusterName string
11-
StsUri string
8+
EnableIstioIntegration bool `split_words:"true"`
9+
EnableAutoMtls bool `split_words:"true"`
10+
StsClusterName string `split_words:"true"`
11+
StsUri string `split_words:"true"`
1212
}
1313

1414
// BuildSettings returns a zero-valued Settings obj if error is encountered when parsing env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package settings_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/onsi/gomega"
8+
. "github.com/onsi/gomega"
9+
10+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/settings"
11+
)
12+
13+
func TestSettings(t *testing.T) {
14+
testCases := []struct {
15+
// name of the test case
16+
name string
17+
18+
// env vars that are set at the beginning of test (and removed after test)
19+
envVars map[string]string
20+
21+
// if set, then these are the expected populated settings
22+
expectedSettings *settings.Settings
23+
24+
// if set, then an error parsing the settings is expected to occur
25+
expectedErrorStr string
26+
}{
27+
{
28+
name: "defaults to empty values",
29+
envVars: map[string]string{},
30+
expectedSettings: &settings.Settings{
31+
EnableIstioIntegration: false,
32+
EnableAutoMtls: false,
33+
StsClusterName: "",
34+
StsUri: "",
35+
},
36+
},
37+
{
38+
name: "all values set",
39+
envVars: map[string]string{
40+
"KGW_ENABLE_ISTIO_INTEGRATION": "true",
41+
"KGW_ENABLE_AUTO_MTLS": "true",
42+
"KGW_STS_CLUSTER_NAME": "my-cluster",
43+
"KGW_STS_URI": "my.sts.uri",
44+
},
45+
expectedSettings: &settings.Settings{
46+
EnableIstioIntegration: true,
47+
EnableAutoMtls: true,
48+
StsClusterName: "my-cluster",
49+
StsUri: "my.sts.uri",
50+
},
51+
},
52+
{
53+
name: "errors on invalid bool",
54+
envVars: map[string]string{
55+
"KGW_ENABLE_ISTIO_INTEGRATION": "true123",
56+
},
57+
expectedErrorStr: "invalid syntax",
58+
},
59+
{
60+
name: "ignores other env vars",
61+
envVars: map[string]string{
62+
"KGW_DOES_NOT_EXIST": "true",
63+
"ANOTHER_VAR": "abc",
64+
"KGW_ENABLE_AUTO_MTLS": "true",
65+
},
66+
expectedSettings: &settings.Settings{
67+
EnableAutoMtls: true,
68+
},
69+
},
70+
}
71+
72+
for _, tc := range testCases {
73+
t.Run(tc.name, func(t *testing.T) {
74+
g := gomega.NewWithT(t)
75+
76+
t.Cleanup(func() {
77+
for k := range tc.envVars {
78+
err := os.Unsetenv(k)
79+
g.Expect(err).NotTo(HaveOccurred())
80+
}
81+
})
82+
83+
for k, v := range tc.envVars {
84+
err := os.Setenv(k, v)
85+
g.Expect(err).NotTo(HaveOccurred())
86+
}
87+
s, err := settings.BuildSettings()
88+
if tc.expectedErrorStr != "" {
89+
g.Expect(err).To(HaveOccurred())
90+
g.Expect(err.Error()).To(gomega.ContainSubstring(tc.expectedErrorStr))
91+
} else {
92+
g.Expect(err).NotTo(HaveOccurred())
93+
g.Expect(s).To(Equal(tc.expectedSettings))
94+
}
95+
})
96+
}
97+
}

internal/kgateway/krtcollections/endpoints.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewGlooK8sEndpointInputs(
5757
k8supstreams krt.Collection[ir.Upstream],
5858
) EndpointsInputs {
5959
endpointSettings := EndpointsSettings{
60-
EnableAutoMtls: stngs.EnableAutoMTLS,
60+
EnableAutoMtls: stngs.EnableAutoMtls,
6161
}
6262

6363
// Create index on EndpointSlices by service name and endpointslice namespace

internal/kgateway/setup/ggv2setup_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ func TestScenarios(t *testing.T) {
113113

114114
os.Setenv("POD_NAMESPACE", "gwtest") // TODO: is this still needed?
115115
// set global settings env vars; current ggv2setup_tests all assume these are set to true
116-
os.Setenv("KGW_ENABLEISTIOINTEGRATION", "true")
117-
os.Setenv("KGW_ENABLEAUTOMTLS", "true")
116+
os.Setenv("KGW_ENABLE_ISTIO_INTEGRATION", "true")
117+
os.Setenv("KGW_ENABLE_AUTO_MTLS", "true")
118+
t.Cleanup(func() {
119+
os.Unsetenv("POD_NAMESPACE")
120+
os.Unsetenv("KGW_ENABLE_ISTIO_INTEGRATION")
121+
os.Unsetenv("KGW_ENABLE_AUTO_MTLS")
122+
})
118123

119124
testEnv := &envtest.Environment{
120125
CRDDirectoryPaths: []string{

0 commit comments

Comments
 (0)