2021-07-28 16:28:26 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
2022-09-08 19:27:18 +00:00
|
|
|
"context"
|
2022-10-06 19:27:44 +00:00
|
|
|
"errors"
|
2021-07-28 16:28:26 +00:00
|
|
|
"fmt"
|
2022-10-08 00:36:57 +00:00
|
|
|
"io"
|
2021-12-22 17:51:48 +00:00
|
|
|
"os"
|
2021-07-28 16:28:26 +00:00
|
|
|
"os/exec"
|
2022-02-08 17:34:57 +00:00
|
|
|
"path/filepath"
|
2023-10-11 18:26:36 +00:00
|
|
|
"regexp"
|
2021-12-22 17:51:48 +00:00
|
|
|
"strconv"
|
2021-07-28 16:28:26 +00:00
|
|
|
"strings"
|
2022-09-08 19:27:18 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
json "github.com/json-iterator/go"
|
|
|
|
ginkgo "github.com/onsi/ginkgo/v2"
|
2022-09-08 19:27:18 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-07-28 16:28:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Name string
|
|
|
|
Status string
|
|
|
|
Roles string
|
|
|
|
InternalIP string
|
|
|
|
ExternalIP string
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:53:41 +00:00
|
|
|
func (n Node) String() string {
|
|
|
|
return fmt.Sprintf("Node (name: %s, status: %s, roles: %s)", n.Name, n.Status, n.Roles)
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:28:26 +00:00
|
|
|
type Pod struct {
|
|
|
|
NameSpace string
|
|
|
|
Name string
|
|
|
|
Ready string
|
|
|
|
Status string
|
|
|
|
Restarts string
|
2023-06-09 13:51:21 +00:00
|
|
|
IP string
|
2021-07-28 16:28:26 +00:00
|
|
|
Node string
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:27:44 +00:00
|
|
|
type NodeError struct {
|
|
|
|
Node string
|
|
|
|
Cmd string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2024-10-02 13:33:02 +00:00
|
|
|
type SvcExternalIP struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
ipMode string `json:"ipMode"`
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:06:32 +00:00
|
|
|
type ObjIP struct {
|
|
|
|
Name string
|
|
|
|
IPv4 string
|
|
|
|
IPv6 string
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:27:44 +00:00
|
|
|
func (ne *NodeError) Error() string {
|
|
|
|
return fmt.Sprintf("failed creating cluster: %s: %v", ne.Cmd, ne.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ne *NodeError) Unwrap() error {
|
|
|
|
return ne.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNodeError(cmd, node string, err error) *NodeError {
|
|
|
|
return &NodeError{
|
|
|
|
Cmd: cmd,
|
|
|
|
Node: node,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 17:34:57 +00:00
|
|
|
func CountOfStringInSlice(str string, pods []Pod) int {
|
|
|
|
count := 0
|
|
|
|
for _, pod := range pods {
|
|
|
|
if strings.Contains(pod.Name, str) {
|
|
|
|
count++
|
|
|
|
}
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
return count
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 22:00:22 +00:00
|
|
|
// genNodeEnvs generates the node and testing environment variables for vagrant up
|
|
|
|
func genNodeEnvs(nodeOS string, serverCount, agentCount int) ([]string, []string, string) {
|
2022-03-02 20:42:55 +00:00
|
|
|
serverNodeNames := make([]string, serverCount)
|
2021-12-22 17:51:48 +00:00
|
|
|
for i := 0; i < serverCount; i++ {
|
2022-03-02 20:42:55 +00:00
|
|
|
serverNodeNames[i] = "server-" + strconv.Itoa(i)
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-03-02 20:42:55 +00:00
|
|
|
agentNodeNames := make([]string, agentCount)
|
2021-12-22 17:51:48 +00:00
|
|
|
for i := 0; i < agentCount; i++ {
|
2022-03-02 20:42:55 +00:00
|
|
|
agentNodeNames[i] = "agent-" + strconv.Itoa(i)
|
2021-12-22 17:51:48 +00:00
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
|
2022-08-15 22:00:22 +00:00
|
|
|
nodeRoles := strings.Join(serverNodeNames, " ") + " " + strings.Join(agentNodeNames, " ")
|
2021-12-22 17:51:48 +00:00
|
|
|
nodeRoles = strings.TrimSpace(nodeRoles)
|
2022-08-15 22:00:22 +00:00
|
|
|
|
2022-02-08 17:34:57 +00:00
|
|
|
nodeBoxes := strings.Repeat(nodeOS+" ", serverCount+agentCount)
|
2021-12-22 17:51:48 +00:00
|
|
|
nodeBoxes = strings.TrimSpace(nodeBoxes)
|
2022-03-02 20:42:55 +00:00
|
|
|
|
2022-08-15 22:00:22 +00:00
|
|
|
nodeEnvs := fmt.Sprintf(`E2E_NODE_ROLES="%s" E2E_NODE_BOXES="%s"`, nodeRoles, nodeBoxes)
|
|
|
|
|
|
|
|
return serverNodeNames, agentNodeNames, nodeEnvs
|
|
|
|
}
|
|
|
|
|
2022-09-08 19:27:18 +00:00
|
|
|
func CreateCluster(nodeOS string, serverCount, agentCount int) ([]string, []string, error) {
|
|
|
|
|
|
|
|
serverNodeNames, agentNodeNames, nodeEnvs := genNodeEnvs(nodeOS, serverCount, agentCount)
|
|
|
|
|
|
|
|
var testOptions string
|
|
|
|
for _, env := range os.Environ() {
|
|
|
|
if strings.HasPrefix(env, "E2E_") {
|
|
|
|
testOptions += " " + env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Bring up the first server node
|
|
|
|
cmd := fmt.Sprintf(`%s %s vagrant up %s &> vagrant.log`, nodeEnvs, testOptions, serverNodeNames[0])
|
|
|
|
fmt.Println(cmd)
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
2022-10-06 19:27:44 +00:00
|
|
|
return nil, nil, newNodeError(cmd, serverNodeNames[0], err)
|
2022-09-08 19:27:18 +00:00
|
|
|
}
|
2023-10-17 20:17:32 +00:00
|
|
|
|
2022-09-08 19:27:18 +00:00
|
|
|
// Bring up the rest of the nodes in parallel
|
|
|
|
errg, _ := errgroup.WithContext(context.Background())
|
|
|
|
for _, node := range append(serverNodeNames[1:], agentNodeNames...) {
|
|
|
|
cmd := fmt.Sprintf(`%s %s vagrant up %s &>> vagrant.log`, nodeEnvs, testOptions, node)
|
2024-05-23 13:13:38 +00:00
|
|
|
fmt.Println(cmd)
|
2022-09-08 19:27:18 +00:00
|
|
|
errg.Go(func() error {
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
2022-10-06 19:27:44 +00:00
|
|
|
return newNodeError(cmd, node, err)
|
2022-09-08 19:27:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// We must wait a bit between provisioning nodes to avoid too many learners attempting to join the cluster
|
2023-01-27 17:01:13 +00:00
|
|
|
if strings.Contains(node, "agent") {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
} else {
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
}
|
2022-09-08 19:27:18 +00:00
|
|
|
}
|
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverNodeNames, agentNodeNames, nil
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:53:41 +00:00
|
|
|
func scpK3sBinary(nodeNames []string) error {
|
|
|
|
for _, node := range nodeNames {
|
|
|
|
cmd := fmt.Sprintf(`vagrant scp ../../../dist/artifacts/k3s %s:/tmp/`, node)
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
|
|
|
return fmt.Errorf("failed to scp k3s binary to %s: %v", node, err)
|
|
|
|
}
|
2023-06-07 23:03:36 +00:00
|
|
|
if _, err := RunCmdOnNode("mv /tmp/k3s /usr/local/bin/", node); err != nil {
|
2023-03-11 03:53:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-15 22:00:22 +00:00
|
|
|
// CreateLocalCluster creates a cluster using the locally built k3s binary. The vagrant-scp plugin must be installed for
|
|
|
|
// this function to work. The binary is deployed as an airgapped install of k3s on the VMs.
|
2022-08-18 16:21:56 +00:00
|
|
|
// This is intended only for local testing purposes when writing a new E2E test.
|
2022-08-15 22:00:22 +00:00
|
|
|
func CreateLocalCluster(nodeOS string, serverCount, agentCount int) ([]string, []string, error) {
|
|
|
|
|
|
|
|
serverNodeNames, agentNodeNames, nodeEnvs := genNodeEnvs(nodeOS, serverCount, agentCount)
|
|
|
|
|
|
|
|
var testOptions string
|
2022-09-08 21:31:50 +00:00
|
|
|
var cmd string
|
|
|
|
|
2022-08-15 22:00:22 +00:00
|
|
|
for _, env := range os.Environ() {
|
|
|
|
if strings.HasPrefix(env, "E2E_") {
|
|
|
|
testOptions += " " + env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testOptions += " E2E_RELEASE_VERSION=skip"
|
|
|
|
|
2023-10-17 20:17:32 +00:00
|
|
|
// Provision the first server node. In GitHub Actions, this also imports the VM image into libvirt, which
|
|
|
|
// takes time and can cause the next vagrant up to fail if it is not given enough time to complete.
|
|
|
|
cmd = fmt.Sprintf(`%s %s vagrant up --no-provision %s &> vagrant.log`, nodeEnvs, testOptions, serverNodeNames[0])
|
|
|
|
fmt.Println(cmd)
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
|
|
|
return nil, nil, newNodeError(cmd, serverNodeNames[0], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bring up the rest of the nodes in parallel
|
2022-09-08 21:31:50 +00:00
|
|
|
errg, _ := errgroup.WithContext(context.Background())
|
2023-10-17 20:17:32 +00:00
|
|
|
for _, node := range append(serverNodeNames[1:], agentNodeNames...) {
|
|
|
|
cmd := fmt.Sprintf(`%s %s vagrant up --no-provision %s &>> vagrant.log`, nodeEnvs, testOptions, node)
|
2022-09-08 21:31:50 +00:00
|
|
|
errg.Go(func() error {
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
2023-10-17 20:17:32 +00:00
|
|
|
return newNodeError(cmd, node, err)
|
2022-09-08 21:31:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// libVirt/Virtualbox needs some time between provisioning nodes
|
|
|
|
time.Sleep(10 * time.Second)
|
2022-08-15 22:00:22 +00:00
|
|
|
}
|
2022-09-08 21:31:50 +00:00
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-10-17 20:17:32 +00:00
|
|
|
|
2023-03-11 03:53:41 +00:00
|
|
|
if err := scpK3sBinary(append(serverNodeNames, agentNodeNames...)); err != nil {
|
|
|
|
return nil, nil, err
|
2022-08-15 22:00:22 +00:00
|
|
|
}
|
2022-09-08 21:31:50 +00:00
|
|
|
// Install K3s on all nodes in parallel
|
|
|
|
errg, _ = errgroup.WithContext(context.Background())
|
|
|
|
for _, node := range append(serverNodeNames, agentNodeNames...) {
|
|
|
|
cmd = fmt.Sprintf(`%s %s vagrant provision %s &>> vagrant.log`, nodeEnvs, testOptions, node)
|
|
|
|
errg.Go(func() error {
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
2022-10-06 19:27:44 +00:00
|
|
|
return newNodeError(cmd, node, err)
|
2022-09-08 21:31:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// K3s needs some time between joining nodes to avoid learner issues
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
}
|
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return nil, nil, err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-08-15 22:00:22 +00:00
|
|
|
|
2022-03-02 20:42:55 +00:00
|
|
|
return serverNodeNames, agentNodeNames, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:16:10 +00:00
|
|
|
func DeployWorkload(workload, kubeconfig string, hardened bool) (string, error) {
|
2022-03-15 17:29:56 +00:00
|
|
|
resourceDir := "../amd64_resource_files"
|
2022-08-05 16:16:10 +00:00
|
|
|
if hardened {
|
|
|
|
resourceDir = "../cis_amd64_resource_files"
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-10-08 00:36:57 +00:00
|
|
|
files, err := os.ReadDir(resourceDir)
|
2021-12-22 17:51:48 +00:00
|
|
|
if err != nil {
|
2022-02-08 17:34:57 +00:00
|
|
|
err = fmt.Errorf("%s : Unable to read resource manifest file for %s", err, workload)
|
2021-12-22 17:51:48 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
fmt.Println("\nDeploying", workload)
|
|
|
|
for _, f := range files {
|
2022-03-15 17:29:56 +00:00
|
|
|
filename := filepath.Join(resourceDir, f.Name())
|
2022-02-08 17:34:57 +00:00
|
|
|
if strings.TrimSpace(f.Name()) == workload {
|
|
|
|
cmd := "kubectl apply -f " + filename + " --kubeconfig=" + kubeconfig
|
|
|
|
return RunCommand(cmd)
|
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
return "", nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 17:34:57 +00:00
|
|
|
func DestroyCluster() error {
|
|
|
|
if _, err := RunCommand("vagrant destroy -f"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Remove("vagrant.log")
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 15:40:29 +00:00
|
|
|
func FetchClusterIP(kubeconfig string, servicename string, dualStack bool) (string, error) {
|
|
|
|
if dualStack {
|
|
|
|
cmd := "kubectl get svc " + servicename + " -o jsonpath='{.spec.clusterIPs}' --kubeconfig=" + kubeconfig
|
|
|
|
res, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
res = strings.ReplaceAll(res, "\"", "")
|
|
|
|
return strings.Trim(res, "[]"), nil
|
|
|
|
}
|
2021-07-28 16:28:26 +00:00
|
|
|
cmd := "kubectl get svc " + servicename + " -o jsonpath='{.spec.clusterIP}' --kubeconfig=" + kubeconfig
|
2021-12-22 17:51:48 +00:00
|
|
|
return RunCommand(cmd)
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 13:33:02 +00:00
|
|
|
// FetchExternalIPs fetches the external IPs of a service
|
|
|
|
func FetchExternalIPs(kubeconfig string, servicename string) ([]string, error) {
|
|
|
|
var externalIPs []string
|
|
|
|
cmd := "kubectl get svc " + servicename + " -o jsonpath='{.status.loadBalancer.ingress}' --kubeconfig=" + kubeconfig
|
|
|
|
output, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return externalIPs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var svcExternalIPs []SvcExternalIP
|
|
|
|
err = json.Unmarshal([]byte(output), &svcExternalIPs)
|
|
|
|
if err != nil {
|
|
|
|
return externalIPs, fmt.Errorf("Error unmarshalling JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over externalIPs and append each IP to the ips slice
|
|
|
|
for _, ipEntry := range svcExternalIPs {
|
|
|
|
externalIPs = append(externalIPs, ipEntry.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalIPs, nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 17:34:57 +00:00
|
|
|
func FetchIngressIP(kubeconfig string) ([]string, error) {
|
|
|
|
cmd := "kubectl get ing ingress -o jsonpath='{.status.loadBalancer.ingress[*].ip}' --kubeconfig=" + kubeconfig
|
|
|
|
res, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ingressIP := strings.Trim(res, " ")
|
|
|
|
ingressIPs := strings.Split(ingressIP, " ")
|
|
|
|
return ingressIPs, nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func FetchNodeExternalIP(nodename string) (string, error) {
|
|
|
|
cmd := "vagrant ssh " + nodename + " -c \"ip -f inet addr show eth1| awk '/inet / {print $2}'|cut -d/ -f1\""
|
|
|
|
ipaddr, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ips := strings.Trim(ipaddr, "")
|
|
|
|
ip := strings.Split(ips, "inet")
|
|
|
|
nodeip := strings.TrimSpace(ip[1])
|
|
|
|
return nodeip, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
|
|
|
|
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)
|
2021-12-22 17:51:48 +00:00
|
|
|
if err != nil {
|
2022-02-08 17:34:57 +00:00
|
|
|
return "", err
|
2021-12-22 17:51:48 +00:00
|
|
|
}
|
2023-10-11 18:26:36 +00:00
|
|
|
re := regexp.MustCompile(`(?m)==> vagrant:.*\n`)
|
|
|
|
kubeConfig = re.ReplaceAllString(kubeConfig, "")
|
2022-02-08 17:34:57 +00:00
|
|
|
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 {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-05-05 18:00:47 +00:00
|
|
|
if err := os.Setenv("E2E_KUBECONFIG", kubeConfigFile); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
return kubeConfigFile, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
func GenReport(specReport ginkgo.SpecReport) {
|
|
|
|
state := struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Time time.Duration `json:"time"`
|
|
|
|
}{
|
|
|
|
State: specReport.State.String(),
|
|
|
|
Name: specReport.LeafNodeText,
|
|
|
|
Type: "k3s test",
|
|
|
|
Time: specReport.RunTime,
|
|
|
|
}
|
|
|
|
status, _ := json.Marshal(state)
|
|
|
|
fmt.Printf("%s", status)
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:53:41 +00:00
|
|
|
func GetJournalLogs(node string) (string, error) {
|
|
|
|
cmd := "journalctl -u k3s* --no-pager"
|
|
|
|
return RunCmdOnNode(cmd, node)
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:27:44 +00:00
|
|
|
// GetVagrantLog returns the logs of on vagrant commands that initialize the nodes and provision K3s on each node.
|
|
|
|
// It also attempts to fetch the systemctl logs of K3s on nodes where the k3s.service failed.
|
|
|
|
func GetVagrantLog(cErr error) string {
|
|
|
|
var nodeErr *NodeError
|
|
|
|
nodeJournal := ""
|
|
|
|
if errors.As(cErr, &nodeErr) {
|
2023-03-11 03:53:41 +00:00
|
|
|
nodeJournal, _ = GetJournalLogs(nodeErr.Node)
|
2022-10-06 19:27:44 +00:00
|
|
|
nodeJournal = "\nNode Journal Logs:\n" + nodeJournal
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:28:39 +00:00
|
|
|
log, err := os.Open("vagrant.log")
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2022-10-08 00:36:57 +00:00
|
|
|
bytes, err := io.ReadAll(log)
|
2022-03-01 19:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2022-10-06 19:27:44 +00:00
|
|
|
return string(bytes) + nodeJournal
|
2022-03-01 19:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 17:34:57 +00:00
|
|
|
func ParseNodes(kubeConfig string, print bool) ([]Node, error) {
|
2021-07-28 16:28:26 +00:00
|
|
|
nodes := make([]Node, 0, 10)
|
|
|
|
nodeList := ""
|
2021-12-22 17:51:48 +00:00
|
|
|
|
|
|
|
cmd := "kubectl get nodes --no-headers -o wide -A --kubeconfig=" + kubeConfig
|
|
|
|
res, err := RunCommand(cmd)
|
2022-02-08 17:34:57 +00:00
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
if err != nil {
|
2023-03-11 03:53:41 +00:00
|
|
|
return nil, fmt.Errorf("unable to get nodes: %s: %v", res, err)
|
2021-12-22 17:51:48 +00:00
|
|
|
}
|
|
|
|
nodeList = strings.TrimSpace(res)
|
|
|
|
split := strings.Split(nodeList, "\n")
|
|
|
|
for _, rec := range split {
|
|
|
|
if strings.TrimSpace(rec) != "" {
|
|
|
|
fields := strings.Fields(rec)
|
|
|
|
node := Node{
|
|
|
|
Name: fields[0],
|
|
|
|
Status: fields[1],
|
|
|
|
Roles: fields[2],
|
|
|
|
InternalIP: fields[5],
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
if len(fields) > 6 {
|
|
|
|
node.ExternalIP = fields[6]
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
nodes = append(nodes, node)
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
if print {
|
2021-07-28 16:28:26 +00:00
|
|
|
fmt.Println(nodeList)
|
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
return nodes, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 13:51:21 +00:00
|
|
|
func formatPods(input string) ([]Pod, error) {
|
2021-07-28 16:28:26 +00:00
|
|
|
pods := make([]Pod, 0, 10)
|
2023-06-09 13:51:21 +00:00
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
split := strings.Split(input, "\n")
|
2021-12-22 17:51:48 +00:00
|
|
|
for _, rec := range split {
|
|
|
|
fields := strings.Fields(string(rec))
|
2023-02-06 18:39:46 +00:00
|
|
|
if len(fields) < 8 {
|
|
|
|
return nil, fmt.Errorf("invalid pod record: %s", rec)
|
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
pod := Pod{
|
|
|
|
NameSpace: fields[0],
|
|
|
|
Name: fields[1],
|
|
|
|
Ready: fields[2],
|
|
|
|
Status: fields[3],
|
|
|
|
Restarts: fields[4],
|
2023-06-09 13:51:21 +00:00
|
|
|
IP: fields[6],
|
2021-12-22 17:51:48 +00:00
|
|
|
Node: fields[7],
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
pods = append(pods, pod)
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2023-06-09 13:51:21 +00:00
|
|
|
return pods, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParsePods(kubeConfig string, print bool) ([]Pod, error) {
|
|
|
|
podList := ""
|
|
|
|
|
|
|
|
cmd := "kubectl get pods -o wide --no-headers -A"
|
|
|
|
res, _ := RunCommand(cmd)
|
|
|
|
podList = strings.TrimSpace(res)
|
|
|
|
|
|
|
|
pods, err := formatPods(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
if print {
|
2021-07-28 16:28:26 +00:00
|
|
|
fmt.Println(podList)
|
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
return pods, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2022-02-08 17:34:57 +00:00
|
|
|
|
2022-03-01 19:28:39 +00:00
|
|
|
// RestartCluster restarts the k3s service on each node given
|
|
|
|
func RestartCluster(nodeNames []string) error {
|
|
|
|
for _, nodeName := range nodeNames {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "systemctl restart k3s* --all"
|
2022-03-01 19:28:39 +00:00
|
|
|
if _, err := RunCmdOnNode(cmd, nodeName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-12 17:36:41 +00:00
|
|
|
// StartCluster starts the k3s service on each node given
|
|
|
|
func StartCluster(nodeNames []string) error {
|
|
|
|
for _, nodeName := range nodeNames {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "systemctl start k3s"
|
2023-05-12 17:36:41 +00:00
|
|
|
if strings.Contains(nodeName, "agent") {
|
|
|
|
cmd += "-agent"
|
|
|
|
}
|
|
|
|
if _, err := RunCmdOnNode(cmd, nodeName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCluster starts the k3s service on each node given
|
|
|
|
func StopCluster(nodeNames []string) error {
|
2023-02-06 16:46:19 +00:00
|
|
|
for _, nodeName := range nodeNames {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "systemctl stop k3s*"
|
2023-02-06 16:46:19 +00:00
|
|
|
if _, err := RunCmdOnNode(cmd, nodeName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:05:33 +00:00
|
|
|
// RunCmdOnNode executes a command from within the given node as sudo
|
2022-02-08 17:34:57 +00:00
|
|
|
func RunCmdOnNode(cmd string, nodename string) (string, error) {
|
2023-06-07 23:05:33 +00:00
|
|
|
injectEnv := ""
|
|
|
|
if _, ok := os.LookupEnv("E2E_GOCOVER"); ok && strings.HasPrefix(cmd, "k3s") {
|
|
|
|
injectEnv = "GOCOVERDIR=/tmp/k3scov "
|
|
|
|
}
|
|
|
|
runcmd := "vagrant ssh " + nodename + " -c \"sudo " + injectEnv + cmd + "\""
|
2022-08-15 22:00:22 +00:00
|
|
|
out, err := RunCommand(runcmd)
|
|
|
|
if err != nil {
|
2023-05-05 18:00:47 +00:00
|
|
|
return out, fmt.Errorf("failed to run command: %s on node %s: %s, %v", cmd, nodename, out, err)
|
2022-08-15 22:00:22 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
2022-02-08 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RunCommand(cmd string) (string, error) {
|
|
|
|
c := exec.Command("bash", "-c", cmd)
|
2023-05-05 18:00:47 +00:00
|
|
|
if kc, ok := os.LookupEnv("E2E_KUBECONFIG"); ok {
|
|
|
|
c.Env = append(os.Environ(), "KUBECONFIG="+kc)
|
|
|
|
}
|
2022-03-01 19:28:39 +00:00
|
|
|
out, err := c.CombinedOutput()
|
2023-10-17 20:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return string(out), fmt.Errorf("failed to run command: %s, %v", cmd, err)
|
|
|
|
}
|
2022-03-01 19:28:39 +00:00
|
|
|
return string(out), err
|
2022-02-08 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 03:53:41 +00:00
|
|
|
func UpgradeCluster(nodeNames []string, local bool) error {
|
|
|
|
upgradeVersion := "E2E_RELEASE_CHANNEL=commit"
|
|
|
|
if local {
|
|
|
|
if err := scpK3sBinary(nodeNames); err != nil {
|
2022-02-08 17:34:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-11 03:53:41 +00:00
|
|
|
upgradeVersion = "E2E_RELEASE_VERSION=skip"
|
2022-02-08 17:34:57 +00:00
|
|
|
}
|
2023-03-11 03:53:41 +00:00
|
|
|
for _, nodeName := range nodeNames {
|
|
|
|
cmd := upgradeVersion + " vagrant provision " + nodeName
|
|
|
|
if out, err := RunCommand(cmd); err != nil {
|
|
|
|
fmt.Println("Error Upgrading Cluster", out)
|
2022-02-08 17:34:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-19 13:06:32 +00:00
|
|
|
|
2023-06-05 21:15:17 +00:00
|
|
|
func GetCoverageReport(nodeNames []string) error {
|
2023-08-01 16:32:34 +00:00
|
|
|
if os.Getenv("E2E_GOCOVER") == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-05 21:15:17 +00:00
|
|
|
covDirs := []string{}
|
|
|
|
for _, nodeName := range nodeNames {
|
|
|
|
covDir := nodeName + "-cov"
|
|
|
|
covDirs = append(covDirs, covDir)
|
|
|
|
os.MkdirAll(covDir, 0755)
|
|
|
|
cmd := "vagrant scp " + nodeName + ":/tmp/k3scov/* " + covDir
|
|
|
|
if _, err := RunCommand(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 23:05:33 +00:00
|
|
|
coverageFile := "coverage.out"
|
2023-08-04 17:26:32 +00:00
|
|
|
cmd := "go tool covdata textfmt -i=" + strings.Join(covDirs, ",") + " -o=" + coverageFile
|
2023-06-05 21:15:17 +00:00
|
|
|
if out, err := RunCommand(cmd); err != nil {
|
|
|
|
return fmt.Errorf("failed to generate coverage report: %s, %v", out, err)
|
|
|
|
}
|
2023-06-07 23:05:33 +00:00
|
|
|
|
|
|
|
f, err := os.ReadFile(coverageFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nf := strings.Replace(string(f),
|
|
|
|
"/go/src/github.com/k3s-io/k3s/cmd/server/main.go",
|
|
|
|
"github.com/k3s-io/k3s/cmd/server/main.go", -1)
|
|
|
|
|
|
|
|
if err = os.WriteFile(coverageFile, []byte(nf), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:15:17 +00:00
|
|
|
for _, covDir := range covDirs {
|
|
|
|
if err := os.RemoveAll(covDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:06:32 +00:00
|
|
|
// getPodIPs returns the IPs of all pods
|
|
|
|
func GetPodIPs(kubeConfigFile string) ([]ObjIP, error) {
|
|
|
|
cmd := `kubectl get pods -A -o=jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.podIPs[*].ip}{"\n"}{end}' --kubeconfig=` + kubeConfigFile
|
|
|
|
return GetObjIPs(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNodeIPs returns the IPs of all nodes
|
|
|
|
func GetNodeIPs(kubeConfigFile string) ([]ObjIP, error) {
|
|
|
|
cmd := `kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.addresses[?(@.type == "InternalIP")].address}{"\n"}{end}' --kubeconfig=` + kubeConfigFile
|
|
|
|
return GetObjIPs(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getObjIPs executes a command to collect IPs
|
|
|
|
func GetObjIPs(cmd string) ([]ObjIP, error) {
|
|
|
|
var objIPs []ObjIP
|
|
|
|
res, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
objs := strings.Split(res, "\n")
|
|
|
|
objs = objs[:len(objs)-1]
|
|
|
|
|
|
|
|
for _, obj := range objs {
|
|
|
|
fields := strings.Fields(obj)
|
|
|
|
if len(fields) > 2 {
|
|
|
|
objIPs = append(objIPs, ObjIP{Name: fields[0], IPv4: fields[1], IPv6: fields[2]})
|
|
|
|
} else if len(fields) > 1 {
|
2023-02-06 16:46:19 +00:00
|
|
|
if strings.Contains(fields[1], ".") {
|
|
|
|
objIPs = append(objIPs, ObjIP{Name: fields[0], IPv4: fields[1]})
|
|
|
|
} else {
|
|
|
|
objIPs = append(objIPs, ObjIP{Name: fields[0], IPv6: fields[1]})
|
|
|
|
}
|
2022-10-19 13:06:32 +00:00
|
|
|
} else {
|
|
|
|
objIPs = append(objIPs, ObjIP{Name: fields[0]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objIPs, nil
|
|
|
|
}
|