k3s/tests/e2e/rootless/rootless_utils.go
Ian Cardoso df5e983fc8
add e2e startup test for rootless k3s (#8383)
* add test for rootless k3s

Signed-off-by: Ian Cardoso <osodracnai@gmail.com>

* fix comments

Signed-off-by: Ian Cardoso <osodracnai@gmail.com>

* Cleanup rootless e2e test, simplify logic

Signed-off-by: Derek Nola <derek.nola@suse.com>

---------

Signed-off-by: Ian Cardoso <osodracnai@gmail.com>
Signed-off-by: Derek Nola <derek.nola@suse.com>
Co-authored-by: Derek Nola <derek.nola@suse.com>
2024-01-09 10:39:54 -08:00

44 lines
1.2 KiB
Go

package rootless
import (
"fmt"
"os"
"strings"
"github.com/k3s-io/k3s/tests/e2e"
)
// RunCmdOnRootlesNode executes a command from within the given node as user vagrant
func RunCmdOnRootlesNode(cmd string, nodename string) (string, error) {
injectEnv := ""
if _, ok := os.LookupEnv("E2E_GOCOVER"); ok && strings.HasPrefix(cmd, "k3s") {
injectEnv = "GOCOVERDIR=/tmp/k3scov "
}
runcmd := "vagrant ssh " + nodename + " -c \"" + injectEnv + cmd + "\""
out, err := e2e.RunCommand(runcmd)
if err != nil {
return out, fmt.Errorf("failed to run command: %s on node %s: %s, %v", cmd, nodename, out, err)
}
return out, nil
}
func GenRootlessKubeConfigFile(serverName string) (string, error) {
kubeConfig, err := RunCmdOnRootlesNode("cat /home/vagrant/.kube/k3s.yaml", serverName)
if err != nil {
return "", err
}
nodeIP, err := e2e.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 {
return "", err
}
if err := os.Setenv("E2E_KUBECONFIG", kubeConfigFile); err != nil {
return "", err
}
return kubeConfigFile, nil
}