diff --git a/tests/e2e/testutils.go b/tests/e2e/testutils.go index 357e41992978..7dae5a799ffb 100644 --- a/tests/e2e/testutils.go +++ b/tests/e2e/testutils.go @@ -314,22 +314,29 @@ func FetchNodeExternalIP(nodename string) (string, error) { } func GenKubeConfigFile(serverName string) (string, error) { - cmd := fmt.Sprintf("vagrant ssh %s -c \"sudo cat /etc/rancher/k3s/k3s.yaml\"", serverName) - kubeConfig, err := RunCommand(cmd) + kubeConfigFile := fmt.Sprintf("kubeconfig-%s", serverName) + cmd := fmt.Sprintf("vagrant scp %s:/etc/rancher/k3s/k3s.yaml ./%s", serverName, kubeConfigFile) + _, err := RunCommand(cmd) + if err != nil { + return "", err + } + + kubeConfig, err := os.ReadFile(kubeConfigFile) if err != nil { return "", err } + re := regexp.MustCompile(`(?m)==> vagrant:.*\n`) - kubeConfig = re.ReplaceAllString(kubeConfig, "") + modifiedKubeConfig := re.ReplaceAllString(string(kubeConfig), "") nodeIP, err := FetchNodeExternalIP(serverName) if err != nil { return "", err } - kubeConfig = strings.Replace(kubeConfig, "127.0.0.1", nodeIP, 1) - kubeConfigFile := fmt.Sprintf("kubeconfig-%s", serverName) - if err := os.WriteFile(kubeConfigFile, []byte(kubeConfig), 0644); err != nil { + modifiedKubeConfig = strings.Replace(modifiedKubeConfig, "127.0.0.1", nodeIP, 1) + if err := os.WriteFile(kubeConfigFile, []byte(modifiedKubeConfig), 0644); err != nil { return "", err } + if err := os.Setenv("E2E_KUBECONFIG", kubeConfigFile); err != nil { return "", err }