2022-03-15 17:29:56 +00:00
|
|
|
package integration
|
2021-07-26 16:59:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-02-08 19:24:34 +00:00
|
|
|
"bytes"
|
2021-08-26 23:26:19 +00:00
|
|
|
"encoding/json"
|
2022-02-08 19:24:34 +00:00
|
|
|
"fmt"
|
2021-07-26 16:59:33 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"strings"
|
2022-01-06 16:05:56 +00:00
|
|
|
"syscall"
|
2021-08-10 23:22:12 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/flock"
|
2022-03-01 01:12:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-08-10 23:22:12 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-06 16:05:56 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
2021-07-26 16:59:33 +00:00
|
|
|
)
|
|
|
|
|
2021-08-26 23:26:19 +00:00
|
|
|
// Compile-time variable
|
|
|
|
var existingServer = "False"
|
|
|
|
|
2022-02-08 19:24:34 +00:00
|
|
|
const lockFile = "/tmp/k3s-test.lock"
|
2021-10-28 19:35:28 +00:00
|
|
|
|
|
|
|
type K3sServer struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
scanner *bufio.Scanner
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func findK3sExecutable() string {
|
2021-08-26 23:26:19 +00:00
|
|
|
// if running on an existing cluster, it maybe installed via k3s.service
|
|
|
|
// or run manually from dist/artifacts/k3s
|
|
|
|
if IsExistingServer() {
|
|
|
|
k3sBin, err := exec.LookPath("k3s")
|
|
|
|
if err == nil {
|
|
|
|
return k3sBin
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
k3sBin := "dist/artifacts/k3s"
|
2021-10-28 19:35:28 +00:00
|
|
|
i := 0
|
|
|
|
for ; i < 20; i++ {
|
2021-07-26 16:59:33 +00:00
|
|
|
_, err := os.Stat(k3sBin)
|
|
|
|
if err != nil {
|
|
|
|
k3sBin = "../" + k3sBin
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-10-28 19:35:28 +00:00
|
|
|
if i == 20 {
|
|
|
|
logrus.Fatal("Unable to find k3s executable")
|
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
return k3sBin
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRoot return true if the user is root (UID 0)
|
|
|
|
func IsRoot() bool {
|
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return currentUser.Uid == "0"
|
|
|
|
}
|
|
|
|
|
2021-08-26 23:26:19 +00:00
|
|
|
func IsExistingServer() bool {
|
|
|
|
return existingServer == "True"
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
// K3sCmd launches the provided K3s command via exec. Command blocks until finished.
|
2022-01-06 16:05:56 +00:00
|
|
|
// Command output from both Stderr and Stdout is provided via string. Input can
|
|
|
|
// be a single string with space separated args, or multiple string args
|
2021-07-26 16:59:33 +00:00
|
|
|
// cmdEx1, err := K3sCmd("etcd-snapshot", "ls")
|
2022-01-06 16:05:56 +00:00
|
|
|
// cmdEx2, err := K3sCmd("kubectl get pods -A")
|
2021-07-26 16:59:33 +00:00
|
|
|
// cmdEx2, err := K3sCmd("kubectl", "get", "pods", "-A")
|
2022-01-06 16:05:56 +00:00
|
|
|
func K3sCmd(inputArgs ...string) (string, error) {
|
2021-12-22 22:48:21 +00:00
|
|
|
if !IsRoot() {
|
|
|
|
return "", errors.New("integration tests must be run as sudo/root")
|
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
k3sBin := findK3sExecutable()
|
2022-01-06 16:05:56 +00:00
|
|
|
var k3sCmd []string
|
|
|
|
for _, arg := range inputArgs {
|
|
|
|
k3sCmd = append(k3sCmd, strings.Fields(arg)...)
|
|
|
|
}
|
2021-12-22 22:48:21 +00:00
|
|
|
cmd := exec.Command(k3sBin, k3sCmd...)
|
2021-07-26 16:59:33 +00:00
|
|
|
byteOut, err := cmd.CombinedOutput()
|
|
|
|
return string(byteOut), err
|
|
|
|
}
|
|
|
|
|
2021-08-26 23:26:19 +00:00
|
|
|
func contains(source []string, target string) bool {
|
|
|
|
for _, s := range source {
|
|
|
|
if s == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerArgsPresent checks if the given arguments are found in the running k3s server
|
|
|
|
func ServerArgsPresent(neededArgs []string) bool {
|
|
|
|
currentArgs := K3sServerArgs()
|
|
|
|
for _, arg := range neededArgs {
|
|
|
|
if !contains(currentArgs, arg) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// K3sServerArgs returns the list of arguments that the k3s server launched with
|
|
|
|
func K3sServerArgs() []string {
|
|
|
|
results, err := K3sCmd("kubectl", "get", "nodes", "-o", `jsonpath='{.items[0].metadata.annotations.k3s\.io/node-args}'`)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
res := strings.ReplaceAll(results, "'", "")
|
|
|
|
var args []string
|
|
|
|
if err := json.Unmarshal([]byte(res), &args); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func FindStringInCmdAsync(scanner *bufio.Scanner, target string) bool {
|
|
|
|
for scanner.Scan() {
|
|
|
|
if strings.Contains(scanner.Text(), target) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-25 16:42:21 +00:00
|
|
|
func K3sTestLock() (int, error) {
|
|
|
|
logrus.Info("waiting to get test lock")
|
|
|
|
return flock.Acquire(lockFile)
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:22:12 +00:00
|
|
|
// K3sStartServer acquires an exclusive lock on a temporary file, then launches a k3s cluster
|
|
|
|
// with the provided arguments. Subsequent/parallel calls to this function will block until
|
|
|
|
// the original lock is cleared using K3sKillServer
|
2021-10-28 19:35:28 +00:00
|
|
|
func K3sStartServer(inputArgs ...string) (*K3sServer, error) {
|
2021-12-22 22:48:21 +00:00
|
|
|
if !IsRoot() {
|
|
|
|
return nil, errors.New("integration tests must be run as sudo/root")
|
|
|
|
}
|
|
|
|
|
2021-10-28 19:35:28 +00:00
|
|
|
var cmdArgs []string
|
|
|
|
for _, arg := range inputArgs {
|
|
|
|
cmdArgs = append(cmdArgs, strings.Fields(arg)...)
|
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
k3sBin := findK3sExecutable()
|
2021-12-22 22:48:21 +00:00
|
|
|
k3sCmd := append([]string{"server"}, cmdArgs...)
|
|
|
|
cmd := exec.Command(k3sBin, k3sCmd...)
|
2022-01-06 16:05:56 +00:00
|
|
|
// Give the server a new group id so we can kill it and its children later
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
2021-07-26 16:59:33 +00:00
|
|
|
cmdOut, _ := cmd.StderrPipe()
|
|
|
|
cmd.Stderr = os.Stderr
|
2022-02-25 16:42:21 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
return &K3sServer{cmd, bufio.NewScanner(cmdOut)}, err
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 16:05:56 +00:00
|
|
|
// K3sKillServer terminates the running K3s server and its children
|
|
|
|
// and unlocks the file for other tests
|
2022-02-25 16:42:21 +00:00
|
|
|
func K3sKillServer(server *K3sServer) error {
|
2022-01-06 16:05:56 +00:00
|
|
|
pgid, err := syscall.Getpgid(server.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
2022-03-01 01:12:06 +00:00
|
|
|
if errors.Is(err, syscall.ESRCH) {
|
|
|
|
logrus.Warnf("Unable to kill k3s server: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "failed to find k3s process group")
|
2022-01-06 16:05:56 +00:00
|
|
|
}
|
|
|
|
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil {
|
2022-03-01 01:12:06 +00:00
|
|
|
return errors.Wrap(err, "failed to kill k3s process group")
|
|
|
|
}
|
|
|
|
if err := server.cmd.Process.Kill(); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to kill k3s process")
|
|
|
|
}
|
|
|
|
if _, err = server.cmd.Process.Wait(); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to wait for k3s process exit")
|
2022-01-06 16:05:56 +00:00
|
|
|
}
|
2022-03-01 01:12:06 +00:00
|
|
|
return nil
|
2022-01-06 16:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// K3sCleanup attempts to cleanup networking and files leftover from an integration test
|
2022-04-14 18:46:37 +00:00
|
|
|
// this is similar to the k3s-killall.sh script, but we dynamically generate that on
|
|
|
|
// install, so we don't have access to it in testing.
|
2022-02-25 16:42:21 +00:00
|
|
|
func K3sCleanup(k3sTestLock int, dataDir string) error {
|
2022-01-06 16:05:56 +00:00
|
|
|
if cni0Link, err := netlink.LinkByName("cni0"); err == nil {
|
|
|
|
links, _ := netlink.LinkList()
|
|
|
|
for _, link := range links {
|
|
|
|
if link.Attrs().MasterIndex == cni0Link.Attrs().Index {
|
|
|
|
netlink.LinkDel(link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
netlink.LinkDel(cni0Link)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flannel1, err := netlink.LinkByName("flannel.1"); err == nil {
|
|
|
|
netlink.LinkDel(flannel1)
|
|
|
|
}
|
|
|
|
if flannelV6, err := netlink.LinkByName("flannel-v6.1"); err == nil {
|
|
|
|
netlink.LinkDel(flannelV6)
|
|
|
|
}
|
2022-02-08 19:24:34 +00:00
|
|
|
if dataDir == "" {
|
|
|
|
dataDir = "/var/lib/rancher/k3s"
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dataDir); err != nil {
|
2021-12-22 22:48:21 +00:00
|
|
|
return err
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|
2022-02-25 16:42:21 +00:00
|
|
|
return flock.Release(k3sTestLock)
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|
2022-02-08 19:24:34 +00:00
|
|
|
|
2022-04-14 18:46:37 +00:00
|
|
|
// RunCommand Runs command on the host
|
2022-02-08 19:24:34 +00:00
|
|
|
func RunCommand(cmd string) (string, error) {
|
|
|
|
c := exec.Command("bash", "-c", cmd)
|
|
|
|
var out bytes.Buffer
|
|
|
|
c.Stdout = &out
|
|
|
|
err := c.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
return out.String(), nil
|
|
|
|
}
|