kubeadm: add unit test for GetProxyEnvVars

This commit is contained in:
Lubomir I. Ivanov 2024-08-16 17:36:33 +03:00
parent fa75c8c749
commit ffbc494a4a
4 changed files with 59 additions and 6 deletions

View File

@ -259,7 +259,7 @@ func createKubeProxyAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset
}
// Propagate the http/https proxy host environment variables to the container
env := &kubeproxyDaemonSet.Spec.Template.Spec.Containers[0].Env
*env = append(*env, kubeadmutil.MergeKubeadmEnvVars(kubeadmutil.GetProxyEnvVars())...)
*env = append(*env, kubeadmutil.MergeKubeadmEnvVars(kubeadmutil.GetProxyEnvVars(nil))...)
// Create the DaemonSet for kube-proxy or update it in case it already exists
return []byte(""), apiclient.CreateOrUpdateDaemonSet(client, kubeproxyDaemonSet)

View File

@ -52,7 +52,7 @@ func GetStaticPodSpecs(cfg *kubeadmapi.ClusterConfiguration, endpoint *kubeadmap
// Get the required hostpath mounts
mounts := getHostPathVolumesForTheControlPlane(cfg)
if proxyEnvs == nil {
proxyEnvs = kubeadmutil.GetProxyEnvVars()
proxyEnvs = kubeadmutil.GetProxyEnvVars(nil)
}
componentHealthCheckTimeout := kubeadmapi.GetActiveTimeouts().ControlPlaneComponentHealthCheck

View File

@ -25,13 +25,17 @@ import (
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
)
// GetProxyEnvVars builds a list of environment variables in order to use the right proxy
func GetProxyEnvVars() []kubeadmapi.EnvVar {
// GetProxyEnvVars builds a list of environment variables in order to use the right proxy.
// Passing nil for environment will make the function use the OS environment.
func GetProxyEnvVars(environment []string) []kubeadmapi.EnvVar {
envs := []kubeadmapi.EnvVar{}
for _, env := range os.Environ() {
if environment == nil {
environment = os.Environ()
}
for _, env := range environment {
pos := strings.Index(env, "=")
if pos == -1 {
// malformed environment variable, skip it.
// Malformed environment variable, skip it.
continue
}
name := env[:pos]

View File

@ -17,6 +17,7 @@ limitations under the License.
package util
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
@ -88,3 +89,51 @@ func TestMergeKubeadmEnvVars(t *testing.T) {
})
}
}
func TestGetProxyEnvVars(t *testing.T) {
var tests = []struct {
name string
environment []string
expected []kubeadmapi.EnvVar
}{
{
name: "environment with lowercase proxy vars",
environment: []string{
"http_proxy=p1",
"foo1=bar1",
"https_proxy=p2",
"foo2=bar2",
"no_proxy=p3",
},
expected: []kubeadmapi.EnvVar{
{EnvVar: v1.EnvVar{Name: "http_proxy", Value: "p1"}},
{EnvVar: v1.EnvVar{Name: "https_proxy", Value: "p2"}},
{EnvVar: v1.EnvVar{Name: "no_proxy", Value: "p3"}},
},
},
{
name: "environment with uppercase proxy vars",
environment: []string{
"HTTP_PROXY=p1",
"foo1=bar1",
"HTTPS_PROXY=p2",
"foo2=bar2",
"NO_PROXY=p3",
},
expected: []kubeadmapi.EnvVar{
{EnvVar: v1.EnvVar{Name: "HTTP_PROXY", Value: "p1"}},
{EnvVar: v1.EnvVar{Name: "HTTPS_PROXY", Value: "p2"}},
{EnvVar: v1.EnvVar{Name: "NO_PROXY", Value: "p3"}},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
envs := GetProxyEnvVars(test.environment)
if !reflect.DeepEqual(envs, test.expected) {
t.Errorf("expected env: %v, got: %v", test.expected, envs)
}
})
}
}