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"
|
2022-06-13 20:32:13 +00:00
|
|
|
"context"
|
2021-08-26 23:26:19 +00:00
|
|
|
"encoding/json"
|
2022-02-08 19:24:34 +00:00
|
|
|
"fmt"
|
2022-10-08 00:36:57 +00:00
|
|
|
"io"
|
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"
|
2022-08-31 22:40:17 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2022-06-13 20:32:13 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
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 {
|
2022-08-31 22:40:17 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
log *os.File
|
2021-10-28 19:35:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-08-27 01:33:13 +00:00
|
|
|
// cmdEx1, err := K3sCmd("etcd-snapshot", "ls")
|
|
|
|
// cmdEx2, err := K3sCmd("kubectl get pods -A")
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-06-13 20:32:13 +00:00
|
|
|
// K3sDefaultDeployments checks if the default deployments for K3s are ready, otherwise returns an error
|
|
|
|
func K3sDefaultDeployments() error {
|
|
|
|
return CheckDeployments([]string{"coredns", "local-path-provisioner", "metrics-server", "traefik"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDeployments checks if the provided list of deployments are ready, otherwise returns an error
|
|
|
|
func CheckDeployments(deployments []string) error {
|
|
|
|
|
|
|
|
deploymentSet := make(map[string]bool)
|
|
|
|
for _, d := range deployments {
|
|
|
|
deploymentSet[d] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := k8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deploymentList, err := client.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, deployment := range deploymentList.Items {
|
|
|
|
if _, ok := deploymentSet[deployment.Name]; ok && deployment.Status.ReadyReplicas == deployment.Status.Replicas {
|
|
|
|
deploymentSet[deployment.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for d, found := range deploymentSet {
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("failed to deploy %s", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:40:17 +00:00
|
|
|
func ParsePods(opts metav1.ListOptions) ([]corev1.Pod, error) {
|
2022-06-13 20:32:13 +00:00
|
|
|
clientSet, err := k8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-31 22:40:17 +00:00
|
|
|
pods, err := clientSet.CoreV1().Pods("").List(context.Background(), opts)
|
2022-06-13 20:32:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pods.Items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseNodes() ([]corev1.Node, error) {
|
|
|
|
clientSet, err := k8sClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodes, err := clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes.Items, nil
|
|
|
|
}
|
|
|
|
|
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}
|
2022-08-31 22:40:17 +00:00
|
|
|
// Pipe output to a file for debugging later
|
|
|
|
f, err := os.Create("./k3log.txt")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd.Stderr = f
|
|
|
|
err = cmd.Start()
|
|
|
|
return &K3sServer{cmd, f}, err
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:43:31 +00:00
|
|
|
// K3sStopServer gracefully stops the running K3s server and does not kill its children.
|
|
|
|
// Equivalent to stopping the K3s service
|
|
|
|
func K3sStopServer(server *K3sServer) error {
|
|
|
|
if server.log != nil {
|
|
|
|
server.log.Close()
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// K3sKillServer terminates the running K3s server and its children.
|
|
|
|
// Equivalent to k3s-killall.sh
|
2022-02-25 16:42:21 +00:00
|
|
|
func K3sKillServer(server *K3sServer) error {
|
2022-10-08 00:36:57 +00:00
|
|
|
if server.log != nil {
|
|
|
|
server.log.Close()
|
|
|
|
os.Remove(server.log.Name())
|
|
|
|
}
|
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-08-31 22:40:17 +00:00
|
|
|
//Unmount all the associated filesystems
|
|
|
|
unmountFolder("/run/k3s")
|
|
|
|
unmountFolder("/run/netns/cni-")
|
|
|
|
|
2022-03-01 01:12:06 +00:00
|
|
|
return nil
|
2022-01-06 16:05:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 20:32:13 +00:00
|
|
|
// K3sCleanup unlocks the test-lock and
|
|
|
|
// attempts to cleanup networking and files leftover from an integration test.
|
|
|
|
// This is similar to the k3s-killall.sh script, but we dynamically generate that on
|
|
|
|
// install, so we don't have access to it during 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-06-13 20:32:13 +00:00
|
|
|
if k3sTestLock != -1 {
|
|
|
|
return flock.Release(k3sTestLock)
|
|
|
|
}
|
|
|
|
return nil
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|
2022-02-08 19:24:34 +00:00
|
|
|
|
2023-01-12 21:43:31 +00:00
|
|
|
func K3sSaveLog(server *K3sServer, dump bool) error {
|
2022-08-31 22:40:17 +00:00
|
|
|
server.log.Close()
|
2023-01-12 21:43:31 +00:00
|
|
|
if !dump {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-31 22:40:17 +00:00
|
|
|
log, err := os.Open(server.log.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer log.Close()
|
2022-10-08 00:36:57 +00:00
|
|
|
b, err := io.ReadAll(log)
|
2022-08-31 22:40:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Server Log Dump:\n\n%s\n\n", b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-06-13 20:32:13 +00:00
|
|
|
|
2022-08-31 22:40:17 +00:00
|
|
|
func unmountFolder(folder string) error {
|
|
|
|
mounts := []string{}
|
|
|
|
f, err := os.ReadFile("/proc/self/mounts")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, line := range strings.Split(string(f), "\n") {
|
|
|
|
columns := strings.Split(line, " ")
|
|
|
|
if len(columns) > 1 {
|
|
|
|
mounts = append(mounts, columns[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if strings.Contains(mount, folder) {
|
|
|
|
if err := unix.Unmount(mount, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = os.Remove(mount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 20:32:13 +00:00
|
|
|
func k8sClient() (*kubernetes.Clientset, error) {
|
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", "/etc/rancher/k3s/k3s.yaml")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clientSet, err := kubernetes.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clientSet, nil
|
|
|
|
}
|