Skip to content

Commit ffbc494

Browse files
committed
kubeadm: add unit test for GetProxyEnvVars
1 parent fa75c8c commit ffbc494

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

cmd/kubeadm/app/phases/addons/proxy/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func createKubeProxyAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset
259259
}
260260
// Propagate the http/https proxy host environment variables to the container
261261
env := &kubeproxyDaemonSet.Spec.Template.Spec.Containers[0].Env
262-
*env = append(*env, kubeadmutil.MergeKubeadmEnvVars(kubeadmutil.GetProxyEnvVars())...)
262+
*env = append(*env, kubeadmutil.MergeKubeadmEnvVars(kubeadmutil.GetProxyEnvVars(nil))...)
263263

264264
// Create the DaemonSet for kube-proxy or update it in case it already exists
265265
return []byte(""), apiclient.CreateOrUpdateDaemonSet(client, kubeproxyDaemonSet)

cmd/kubeadm/app/phases/controlplane/manifests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func GetStaticPodSpecs(cfg *kubeadmapi.ClusterConfiguration, endpoint *kubeadmap
5252
// Get the required hostpath mounts
5353
mounts := getHostPathVolumesForTheControlPlane(cfg)
5454
if proxyEnvs == nil {
55-
proxyEnvs = kubeadmutil.GetProxyEnvVars()
55+
proxyEnvs = kubeadmutil.GetProxyEnvVars(nil)
5656
}
5757
componentHealthCheckTimeout := kubeadmapi.GetActiveTimeouts().ControlPlaneComponentHealthCheck
5858

cmd/kubeadm/app/util/env.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ import (
2525
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
2626
)
2727

28-
// GetProxyEnvVars builds a list of environment variables in order to use the right proxy
29-
func GetProxyEnvVars() []kubeadmapi.EnvVar {
28+
// GetProxyEnvVars builds a list of environment variables in order to use the right proxy.
29+
// Passing nil for environment will make the function use the OS environment.
30+
func GetProxyEnvVars(environment []string) []kubeadmapi.EnvVar {
3031
envs := []kubeadmapi.EnvVar{}
31-
for _, env := range os.Environ() {
32+
if environment == nil {
33+
environment = os.Environ()
34+
}
35+
for _, env := range environment {
3236
pos := strings.Index(env, "=")
3337
if pos == -1 {
34-
// malformed environment variable, skip it.
38+
// Malformed environment variable, skip it.
3539
continue
3640
}
3741
name := env[:pos]

cmd/kubeadm/app/util/env_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"reflect"
2021
"testing"
2122

2223
"github.com/stretchr/testify/assert"
@@ -88,3 +89,51 @@ func TestMergeKubeadmEnvVars(t *testing.T) {
8889
})
8990
}
9091
}
92+
93+
func TestGetProxyEnvVars(t *testing.T) {
94+
var tests = []struct {
95+
name string
96+
environment []string
97+
expected []kubeadmapi.EnvVar
98+
}{
99+
{
100+
name: "environment with lowercase proxy vars",
101+
environment: []string{
102+
"http_proxy=p1",
103+
"foo1=bar1",
104+
"https_proxy=p2",
105+
"foo2=bar2",
106+
"no_proxy=p3",
107+
},
108+
expected: []kubeadmapi.EnvVar{
109+
{EnvVar: v1.EnvVar{Name: "http_proxy", Value: "p1"}},
110+
{EnvVar: v1.EnvVar{Name: "https_proxy", Value: "p2"}},
111+
{EnvVar: v1.EnvVar{Name: "no_proxy", Value: "p3"}},
112+
},
113+
},
114+
{
115+
name: "environment with uppercase proxy vars",
116+
environment: []string{
117+
"HTTP_PROXY=p1",
118+
"foo1=bar1",
119+
"HTTPS_PROXY=p2",
120+
"foo2=bar2",
121+
"NO_PROXY=p3",
122+
},
123+
expected: []kubeadmapi.EnvVar{
124+
{EnvVar: v1.EnvVar{Name: "HTTP_PROXY", Value: "p1"}},
125+
{EnvVar: v1.EnvVar{Name: "HTTPS_PROXY", Value: "p2"}},
126+
{EnvVar: v1.EnvVar{Name: "NO_PROXY", Value: "p3"}},
127+
},
128+
},
129+
}
130+
131+
for _, test := range tests {
132+
t.Run(test.name, func(t *testing.T) {
133+
envs := GetProxyEnvVars(test.environment)
134+
if !reflect.DeepEqual(envs, test.expected) {
135+
t.Errorf("expected env: %v, got: %v", test.expected, envs)
136+
}
137+
})
138+
}
139+
}

0 commit comments

Comments
 (0)