From 5e2d63daa9340d4db40ca4ced8e7fc6168bd502a Mon Sep 17 00:00:00 2001 From: Harrison Affel Date: Wed, 18 Dec 2024 16:32:33 -0500 Subject: [PATCH] ensure newlines are added to custom windows agent environment variables --- pkg/capr/installer/installer.go | 2 +- pkg/capr/installer/installer_test.go | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/capr/installer/installer.go b/pkg/capr/installer/installer.go index 58e38772d13..a3bf48145ee 100644 --- a/pkg/capr/installer/installer.go +++ b/pkg/capr/installer/installer.go @@ -188,7 +188,7 @@ func WindowsInstallScript(ctx context.Context, token string, envVars []corev1.En if envVar.Value == "" { continue } - envVarBuf.WriteString(capr.FormatWindowsEnvVar(envVar, false)) + envVarBuf.WriteString(capr.FormatWindowsEnvVar(envVar, false) + "\n") } server := "" if settings.ServerURL.Get() != "" { diff --git a/pkg/capr/installer/installer_test.go b/pkg/capr/installer/installer_test.go index 27f195b145d..bf8366df537 100644 --- a/pkg/capr/installer/installer_test.go +++ b/pkg/capr/installer/installer_test.go @@ -3,6 +3,7 @@ package installer import ( "context" "fmt" + "github.com/rancher/rancher/pkg/capr" "testing" "github.com/rancher/rancher/pkg/settings" @@ -25,7 +26,23 @@ func TestInstaller_WindowsInstallScript(t *testing.T) { CACertEncoded := systemtemplate.CAChecksum() - script, err := WindowsInstallScript(context.TODO(), "test", []corev1.EnvVar{}, "localhost", "/var/lib/rancher/rke2") + agentVar1 := corev1.EnvVar{ + Name: "TestEnvVar1", + Value: "TestEnvVarValue", + } + + agentVar2 := corev1.EnvVar{ + Name: "TestEnvVar2", + Value: "TestEnvVarValue", + } + + formattedAgentVar1 := capr.FormatWindowsEnvVar(agentVar1, false) + formattedAgentVar2 := capr.FormatWindowsEnvVar(agentVar2, false) + + script, err := WindowsInstallScript(context.TODO(), "test", []corev1.EnvVar{ + agentVar1, + agentVar2, + }, "localhost", "/var/lib/rancher/rke2") // assert a.Nil(err) @@ -37,4 +54,9 @@ func TestInstaller_WindowsInstallScript(t *testing.T) { a.Contains(string(script), "$env:CSI_PROXY_URL") a.Contains(string(script), "$env:CSI_PROXY_VERSION") a.Contains(string(script), "$env:CSI_PROXY_KUBELET_PATH") + a.Contains(string(script), "$env:TestEnvVar1") + a.Contains(string(script), "$env:TestEnvVar2") + + // ensure agent env vars are not on the same line + a.NotContains(string(script), formattedAgentVar1+formattedAgentVar2) }