Skip to content

Commit ca66d51

Browse files
committed
Map cluster-wide proxy env variable for compatibility with Vector expectations
Signed-off-by: Vitalii Parfonov <[email protected]>
1 parent 48896de commit ca66d51

File tree

5 files changed

+74
-92
lines changed

5 files changed

+74
-92
lines changed

internal/collector/collector.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func (f *Factory) NewCollectorContainer(inputs logging.InputSpecs, secretNames [
209209
{Name: "POD_IP", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{APIVersion: "v1", FieldPath: "status.podIP"}}},
210210
{Name: "POD_IPS", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{APIVersion: "v1", FieldPath: "status.podIPs"}}},
211211
}
212-
collector.Env = append(collector.Env, utils.GetProxyEnvVars()...)
212+
213+
collector.Env = append(collector.Env, utils.GetProxyEnvVars(f.CollectorSpec.Type)...)
213214

214215
collector.VolumeMounts = []v1.VolumeMount{
215216
{Name: f.ResourceNames.SecretMetrics, ReadOnly: true, MountPath: metricsVolumePath},

internal/collector/collector_test.go

+41-79
Original file line numberDiff line numberDiff line change
@@ -173,39 +173,48 @@ var _ = Describe("Factory#Daemonset", func() {
173173
Fail(fmt.Sprintf("Exp. collector to include env var: %s", name))
174174
}
175175

176-
It("should add the proxy variables to the collector", func() {
177-
_httpProxy := os.Getenv("http_proxy")
178-
_httpsProxy := os.Getenv("https_proxy")
179-
_noProxy := os.Getenv("no_proxy")
180-
cleanup := func() {
181-
_ = os.Setenv("http_proxy", _httpProxy)
182-
_ = os.Setenv("https_proxy", _httpsProxy)
183-
_ = os.Setenv("no_proxy", _noProxy)
184-
}
185-
defer cleanup()
186-
187-
httpproxy := "http://[email protected]/3128/"
188-
noproxy := ".cluster.local,localhost"
189-
_ = os.Setenv("http_proxy", httpproxy)
190-
_ = os.Setenv("https_proxy", httpproxy)
191-
_ = os.Setenv("no_proxy", noproxy)
192-
caBundle := "-----BEGIN CERTIFICATE-----\n<PEM_ENCODED_CERT>\n-----END CERTIFICATE-----\n"
193-
podSpec = *factory.NewPodSpec(&v1.ConfigMap{
194-
ObjectMeta: metav1.ObjectMeta{
195-
Namespace: "openshift-logging",
196-
Name: constants.CollectorTrustedCAName,
197-
},
198-
Data: map[string]string{
199-
constants.TrustedCABundleKey: caBundle,
200-
},
201-
}, logging.ClusterLogForwarderSpec{}, "1234", "", tls.GetClusterTLSProfileSpec(nil), nil, constants.OpenshiftNS)
202-
collector = podSpec.Containers[0]
176+
DescribeTable("should add the proxy variables to the collector",
177+
func(collectorType logging.LogCollectionType, setHttpProxy, setHttpsProxy, setNoProxy, expectedHttpProxy, expectedHttpsProxy, expectedNoProxy string) {
178+
_httpProxy := os.Getenv(setHttpProxy)
179+
_httpsProxy := os.Getenv(setHttpsProxy)
180+
_noProxy := os.Getenv(setNoProxy)
181+
cleanup := func() {
182+
_ = os.Setenv(setHttpProxy, _httpProxy)
183+
_ = os.Setenv(setHttpsProxy, _httpsProxy)
184+
_ = os.Setenv(setNoProxy, _noProxy)
185+
}
186+
defer cleanup()
187+
188+
httpproxy := "http://[email protected]/3128/"
189+
noproxy := ".cluster.local,localhost"
190+
_ = os.Setenv(setHttpProxy, httpproxy)
191+
_ = os.Setenv(setHttpsProxy, httpproxy)
192+
_ = os.Setenv(setNoProxy, noproxy)
193+
caBundle := "-----BEGIN CERTIFICATE-----\n<PEM_ENCODED_CERT>\n-----END CERTIFICATE-----\n"
194+
factory.CollectorSpec = logging.CollectionSpec{
195+
Type: collectorType,
196+
}
197+
podSpec = *factory.NewPodSpec(&v1.ConfigMap{
198+
ObjectMeta: metav1.ObjectMeta{
199+
Namespace: "openshift-logging",
200+
Name: constants.CollectorTrustedCAName,
201+
},
202+
Data: map[string]string{
203+
constants.TrustedCABundleKey: caBundle,
204+
},
205+
}, logging.ClusterLogForwarderSpec{}, "1234", "", tls.GetClusterTLSProfileSpec(nil), nil, constants.OpenshiftNS)
206+
collector = podSpec.Containers[0]
203207

204-
verifyEnvVar(collector, "http_proxy", httpproxy)
205-
verifyEnvVar(collector, "https_proxy", httpproxy)
206-
verifyEnvVar(collector, "no_proxy", "elasticsearch,"+noproxy)
207-
verifyProxyVolumesAndVolumeMounts(collector, podSpec, constants.CollectorTrustedCAName)
208-
})
208+
verifyEnvVar(collector, expectedHttpProxy, httpproxy)
209+
verifyEnvVar(collector, expectedHttpsProxy, httpproxy)
210+
verifyEnvVar(collector, expectedNoProxy, "elasticsearch,"+noproxy)
211+
verifyProxyVolumesAndVolumeMounts(collector, podSpec, constants.CollectorTrustedCAName)
212+
},
213+
Entry("Fluentd expect environment variable name in lowercase", logging.LogCollectionTypeFluentd, "http_proxy", "https_proxy", "no_proxy", "http_proxy", "https_proxy", "no_proxy"),
214+
Entry("Fluentd expects existing uppercase environment variable values to be lowercased", logging.LogCollectionTypeFluentd, "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "no_proxy"),
215+
Entry("Vector expect environment variable in uppercase", logging.LogCollectionTypeVector, "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"),
216+
Entry("Vector expects existing lowercase environment variable values to be uppercased", logging.LogCollectionTypeVector, "http_proxy", "https_proxy", "no_proxy", "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"),
217+
)
209218
})
210219

211220
Context("and using custom named ClusterLogForwarder", func() {
@@ -546,53 +555,6 @@ var _ = Describe("Factory#Deployment", func() {
546555

547556
})
548557

549-
Context("and the proxy config exists", func() {
550-
551-
var verifyEnvVar = func(container v1.Container, name, value string) {
552-
for _, elem := range container.Env {
553-
if elem.Name == name {
554-
Expect(elem.Value).To(Equal(value), "Exp. collector to have env var %s: %s:", name, value)
555-
return
556-
}
557-
}
558-
Fail(fmt.Sprintf("Exp. collector to include env var: %s", name))
559-
}
560-
561-
It("should add the proxy variables to the collector", func() {
562-
_httpProxy := os.Getenv("http_proxy")
563-
_httpsProxy := os.Getenv("https_proxy")
564-
_noProxy := os.Getenv("no_proxy")
565-
cleanup := func() {
566-
_ = os.Setenv("http_proxy", _httpProxy)
567-
_ = os.Setenv("https_proxy", _httpsProxy)
568-
_ = os.Setenv("no_proxy", _noProxy)
569-
}
570-
defer cleanup()
571-
572-
httpproxy := "http://[email protected]/3128/"
573-
noproxy := ".cluster.local,localhost"
574-
_ = os.Setenv("http_proxy", httpproxy)
575-
_ = os.Setenv("https_proxy", httpproxy)
576-
_ = os.Setenv("no_proxy", noproxy)
577-
caBundle := "-----BEGIN CERTIFICATE-----\n<PEM_ENCODED_CERT>\n-----END CERTIFICATE-----\n"
578-
podSpec = *factory.NewPodSpec(&v1.ConfigMap{
579-
ObjectMeta: metav1.ObjectMeta{
580-
Namespace: "openshift-logging",
581-
Name: constants.CollectorTrustedCAName,
582-
},
583-
Data: map[string]string{
584-
constants.TrustedCABundleKey: caBundle,
585-
},
586-
}, logging.ClusterLogForwarderSpec{}, "1234", "", tls.GetClusterTLSProfileSpec(nil), nil, constants.OpenshiftNS)
587-
collector = podSpec.Containers[0]
588-
589-
verifyEnvVar(collector, "http_proxy", httpproxy)
590-
verifyEnvVar(collector, "https_proxy", httpproxy)
591-
verifyEnvVar(collector, "no_proxy", "elasticsearch,"+noproxy)
592-
verifyProxyVolumesAndVolumeMounts(collector, podSpec, constants.CollectorTrustedCAName)
593-
})
594-
})
595-
596558
Context("and using custom named ClusterLogForwarder", func() {
597559

598560
It("should have custom named podSpec resources based on CLF name", func() {

internal/utils/utils.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func EnvVarResourceFieldSelectorEqual(resource1, resource2 v1.ResourceFieldSelec
341341
resource1.Divisor.Cmp(resource2.Divisor) == 0
342342
}
343343

344-
func GetProxyEnvVars() []v1.EnvVar {
344+
func GetProxyEnvVars(collectionType logging.LogCollectionType) []v1.EnvVar {
345345
envVars := []v1.EnvVar{}
346346
for _, envvar := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy"} {
347347
if value := os.Getenv(envvar); value != "" {
@@ -350,8 +350,14 @@ func GetProxyEnvVars() []v1.EnvVar {
350350
value = strings.Join(constants.ExtraNoProxyList, ",") + "," + value
351351
}
352352
}
353+
switch collectionType {
354+
case logging.LogCollectionTypeVector:
355+
envvar = strings.ToUpper(envvar)
356+
case logging.LogCollectionTypeFluentd:
357+
envvar = strings.ToLower(envvar)
358+
}
353359
envVars = append(envVars, v1.EnvVar{
354-
Name: strings.ToLower(envvar),
360+
Name: envvar,
355361
Value: value,
356362
})
357363
}

internal/utils/utils_test.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package utils
22

33
import (
4+
logging "github.com/openshift/cluster-logging-operator/api/logging/v1"
45
"os"
6+
"strings"
57
"testing"
68

79
v1 "k8s.io/api/core/v1"
@@ -145,22 +147,35 @@ var _ = Describe("GetProxyEnvVars", func() {
145147
var (
146148
envvars = map[string]string{}
147149
)
148-
BeforeEach(func() {
150+
AfterEach(func() {
151+
for k, v := range envvars {
152+
Expect(os.Setenv(k, v)).To(Succeed())
153+
}
154+
})
155+
It("should retrieve the proxy settings from the operators ENV variables for Fluentd should be in lowercase", func() {
149156
for _, envvar := range []string{"https_proxy", "http_proxy", "no_proxy"} {
150157
envvars[envvar] = os.Getenv(envvar)
151158
Expect(os.Setenv(envvar, envvar)).To(Succeed())
152159
}
153-
})
154-
AfterEach(func() {
155-
for k, v := range envvars {
156-
Expect(os.Setenv(k, v)).To(Succeed())
160+
envvars := GetProxyEnvVars(logging.LogCollectionTypeFluentd)
161+
Expect(envvars).To(HaveLen(3)) //proxy,noproxy vars
162+
for _, envvar := range envvars {
163+
if envvar.Name == "no_proxy" {
164+
Expect(envvar.Value).To(Equal("elasticsearch,"+envvar.Name), "Exp. the value to be set to the name for the test with elasticsearch prepended")
165+
} else {
166+
Expect(envvar.Name).To(Equal(envvar.Value), "Exp. the value to be set to the name for the test")
167+
}
157168
}
158169
})
159-
It("should retrieve the proxy settings from the operators ENV variables", func() {
160-
envvars := GetProxyEnvVars()
170+
It("should retrieve the proxy settings from the operators ENV variables for Vector should be in uppercase", func() {
171+
for _, envvar := range []string{"https_proxy", "http_proxy", "no_proxy"} {
172+
envvars[envvar] = os.Getenv(envvar)
173+
Expect(os.Setenv(envvar, strings.ToUpper(envvar))).To(Succeed())
174+
}
175+
envvars := GetProxyEnvVars(logging.LogCollectionTypeVector)
161176
Expect(envvars).To(HaveLen(3)) //proxy,noproxy vars
162177
for _, envvar := range envvars {
163-
if envvar.Name == "NO_PROXY" || envvar.Name == "no_proxy" {
178+
if envvar.Name == "NO_PROXY" {
164179
Expect(envvar.Value).To(Equal("elasticsearch,"+envvar.Name), "Exp. the value to be set to the name for the test with elasticsearch prepended")
165180
} else {
166181
Expect(envvar.Name).To(Equal(envvar.Value), "Exp. the value to be set to the name for the test")

test/functional/misc/vector_api_cli_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build !fluentd
2-
31
package misc
42

53
import (

0 commit comments

Comments
 (0)