Skip to content

Commit 27f57aa

Browse files
authored
Add function to facilitate proxy-aware operators (#71)
* Add function to facilitate proxy-aware operators Signed-off-by: austin <[email protected]> * linter Signed-off-by: austin <[email protected]> * mor linter Signed-off-by: austin <[email protected]> * license Signed-off-by: austin <[email protected]> * review and cleanup Signed-off-by: austin <[email protected]> * more PR suggestions Signed-off-by: austin <[email protected]>
1 parent ecd9a4a commit 27f57aa

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

proxy/doc.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*
16+
Package proxy implements helper functions to facilitate making operators proxy-aware.
17+
18+
This package assumes that proxy environment variables `HTTPS_PROXY`,
19+
`HTTP_PROXY`, and/or `NO_PROXY` are set in the Operator environment, typically
20+
via the Operator deployment.
21+
22+
Proxy-aware operators can use the ReadProxyVarsFromEnv to retrieve these values
23+
as a slice of corev1 EnvVars. Each of the proxy variables are duplicated in
24+
upper and lower case to support applications that use either. In their
25+
reconcile functions, Operator authors are then responsible for setting these
26+
variables in the Container Envs that must use the proxy, For example:
27+
28+
// Pods with Kubernetes < 1.22
29+
for _, cSpec := range (myPod.Spec.Containers) {
30+
cSpec.Env = append(defaultEnv(), ReadProxyVarsFromEnv())
31+
}
32+
*/
33+
package proxy

proxy/proxy.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package proxy
16+
17+
import (
18+
"os"
19+
"strings"
20+
21+
corev1 "k8s.io/api/core/v1"
22+
)
23+
24+
// ProxyEnvNames are standard environment variables for proxies
25+
var ProxyEnvNames = []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}
26+
27+
// ReadProxyVarsFromEnv retrieves the standard proxy-related environment
28+
// variables from the running environment and returns a slice of corev1 EnvVar
29+
// containing upper and lower case versions of those variables.
30+
func ReadProxyVarsFromEnv() []corev1.EnvVar {
31+
envVars := []corev1.EnvVar{}
32+
for _, s := range ProxyEnvNames {
33+
value, isSet := os.LookupEnv(s)
34+
if isSet {
35+
envVars = append(envVars, corev1.EnvVar{
36+
Name: s,
37+
Value: value,
38+
}, corev1.EnvVar{
39+
Name: strings.ToLower(s),
40+
Value: value,
41+
})
42+
}
43+
}
44+
return envVars
45+
}

proxy/proxy_suite_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package proxy_test
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestProxy(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Proxy Suite")
27+
}

proxy/proxy_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package proxy
16+
17+
import (
18+
"errors"
19+
"os"
20+
"strings"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
)
26+
27+
func checkValueFromEnvObj(name string, envVars []corev1.EnvVar) (string, error) {
28+
for i := range envVars {
29+
if envVars[i].Name == name {
30+
return envVars[i].Value, nil
31+
}
32+
}
33+
return "", errors.New("empty name")
34+
35+
}
36+
37+
var _ = Describe("Retrieving", func() {
38+
Describe("proxy environment variables", func() {
39+
It("returns a slice of environment variables that were set", func() {
40+
os.Setenv("HTTPS_PROXY", "https_proxy_test")
41+
os.Setenv("HTTP_PROXY", "http_proxy_test")
42+
os.Setenv("NO_PROXY", "no_proxy_test")
43+
envVars := ReadProxyVarsFromEnv()
44+
Expect(len(envVars)).To(Equal(6))
45+
})
46+
It("does not return unset variables", func() {
47+
envVars := ReadProxyVarsFromEnv()
48+
Expect(len(envVars)).To(Equal(0))
49+
})
50+
51+
It("creates upper and lower case environment variables with the same value", func() {
52+
os.Setenv("HTTPS_PROXY", "https_proxy_test")
53+
os.Setenv("HTTP_PROXY", "http_proxy_test")
54+
os.Setenv("NO_PROXY", "no_proxy_test")
55+
envVars := ReadProxyVarsFromEnv()
56+
57+
for _, envName := range ProxyEnvNames {
58+
upperValue, err := checkValueFromEnvObj(envName, envVars)
59+
Expect(err).To(BeNil())
60+
lowerValue, err := checkValueFromEnvObj(strings.ToLower(envName), envVars)
61+
Expect(err).To(BeNil())
62+
Expect(upperValue).To(Equal(lowerValue))
63+
}
64+
})
65+
AfterEach(func() {
66+
_ = os.Unsetenv("HTTPS_PROXY")
67+
_ = os.Unsetenv("HTTP_PROXY")
68+
_ = os.Unsetenv("NO_PROXY")
69+
})
70+
})
71+
})

0 commit comments

Comments
 (0)