2022-10-31 18:02:44 +00:00
|
|
|
package splitserver
|
2022-03-17 23:56:10 +00:00
|
|
|
|
|
|
|
import (
|
2024-04-29 20:57:22 +00:00
|
|
|
"context"
|
2022-03-17 23:56:10 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2024-04-29 20:57:22 +00:00
|
|
|
"time"
|
2022-03-17 23:56:10 +00:00
|
|
|
|
|
|
|
"github.com/k3s-io/k3s/tests/e2e"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2024-04-29 20:57:22 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-03-17 23:56:10 +00:00
|
|
|
)
|
|
|
|
|
2024-03-27 16:55:13 +00:00
|
|
|
// Valid nodeOS: generic/ubuntu2310, opensuse/Leap-15.3.x86_64
|
|
|
|
var nodeOS = flag.String("nodeOS", "generic/ubuntu2310", "VM operating system")
|
2024-02-21 18:27:22 +00:00
|
|
|
var etcdCount = flag.Int("etcdCount", 3, "number of server nodes only deploying etcd")
|
2022-03-17 23:56:10 +00:00
|
|
|
var controlPlaneCount = flag.Int("controlPlaneCount", 1, "number of server nodes acting as control plane")
|
|
|
|
var agentCount = flag.Int("agentCount", 1, "number of agent nodes")
|
2023-01-12 00:09:45 +00:00
|
|
|
var ci = flag.Bool("ci", false, "running on CI")
|
2024-04-29 20:57:22 +00:00
|
|
|
var local = flag.Bool("local", false, "deploy a locally built K3s binary")
|
2022-08-05 16:16:10 +00:00
|
|
|
var hardened = flag.Bool("hardened", false, "true or false")
|
2022-03-17 23:56:10 +00:00
|
|
|
|
|
|
|
// Environment Variables Info:
|
|
|
|
// E2E_RELEASE_VERSION=v1.23.1+k3s2 or nil for latest commit from master
|
|
|
|
|
2024-04-29 20:57:22 +00:00
|
|
|
// createSplitCluster creates a split server cluster with the given nodeOS, etcdCount, controlPlaneCount, and agentCount.
|
|
|
|
// It duplicates and merges functionality found in the e2e.CreateCluster and e2e.CreateLocalCluster functions.
|
|
|
|
func createSplitCluster(nodeOS string, etcdCount, controlPlaneCount, agentCount int, local bool) ([]string, []string, []string, error) {
|
2022-03-17 23:56:10 +00:00
|
|
|
etcdNodeNames := make([]string, etcdCount)
|
2024-04-29 20:57:22 +00:00
|
|
|
cpNodeNames := make([]string, controlPlaneCount)
|
|
|
|
agentNodeNames := make([]string, agentCount)
|
|
|
|
|
2022-03-17 23:56:10 +00:00
|
|
|
for i := 0; i < etcdCount; i++ {
|
|
|
|
etcdNodeNames[i] = "server-etcd-" + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
for i := 0; i < controlPlaneCount; i++ {
|
|
|
|
cpNodeNames[i] = "server-cp-" + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
for i := 0; i < agentCount; i++ {
|
|
|
|
agentNodeNames[i] = "agent-" + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
nodeRoles := strings.Join(etcdNodeNames, " ") + " " + strings.Join(cpNodeNames, " ") + " " + strings.Join(agentNodeNames, " ")
|
|
|
|
|
|
|
|
nodeRoles = strings.TrimSpace(nodeRoles)
|
|
|
|
nodeBoxes := strings.Repeat(nodeOS+" ", etcdCount+controlPlaneCount+agentCount)
|
|
|
|
nodeBoxes = strings.TrimSpace(nodeBoxes)
|
|
|
|
|
2024-04-29 20:57:22 +00:00
|
|
|
allNodes := append(etcdNodeNames, cpNodeNames...)
|
|
|
|
allNodes = append(allNodes, agentNodeNames...)
|
|
|
|
|
2022-03-17 23:56:10 +00:00
|
|
|
var testOptions string
|
|
|
|
for _, env := range os.Environ() {
|
|
|
|
if strings.HasPrefix(env, "E2E_") {
|
|
|
|
testOptions += " " + env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 20:57:22 +00:00
|
|
|
// Provision the first etcd 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(`E2E_NODE_ROLES="%s" E2E_NODE_BOXES="%s" vagrant up --no-provision %s &> vagrant.log`, nodeRoles, nodeBoxes, etcdNodeNames[0])
|
2022-03-17 23:56:10 +00:00
|
|
|
fmt.Println(cmd)
|
|
|
|
if _, err := e2e.RunCommand(cmd); err != nil {
|
2024-04-29 20:57:22 +00:00
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bring up the rest of the nodes in parallel
|
|
|
|
errg, _ := errgroup.WithContext(context.Background())
|
|
|
|
for _, node := range allNodes[1:] {
|
|
|
|
cmd := fmt.Sprintf(`E2E_NODE_ROLES="%s" E2E_NODE_BOXES="%s" vagrant up --no-provision %s &>> vagrant.log`, nodeRoles, nodeBoxes, node)
|
|
|
|
errg.Go(func() error {
|
|
|
|
_, err := e2e.RunCommand(cmd)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
// libVirt/Virtualbox needs some time between provisioning nodes
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if local {
|
|
|
|
testOptions += " E2E_RELEASE_VERSION=skip"
|
|
|
|
for _, node := range allNodes {
|
|
|
|
cmd := fmt.Sprintf(`E2E_NODE_ROLES=%s vagrant scp ../../../dist/artifacts/k3s %s:/tmp/`, node, node)
|
|
|
|
if _, err := e2e.RunCommand(cmd); err != nil {
|
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, fmt.Errorf("failed to scp k3s binary to %s: %v", node, err)
|
|
|
|
}
|
|
|
|
if _, err := e2e.RunCmdOnNode("mv /tmp/k3s /usr/local/bin/", node); err != nil {
|
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Install K3s on all nodes in parallel
|
|
|
|
errg, _ = errgroup.WithContext(context.Background())
|
|
|
|
for _, node := range allNodes {
|
|
|
|
cmd = fmt.Sprintf(`E2E_NODE_ROLES="%s" E2E_NODE_BOXES="%s" %s vagrant provision %s &>> vagrant.log`, nodeRoles, nodeBoxes, testOptions, node)
|
|
|
|
errg.Go(func() error {
|
|
|
|
_, err := e2e.RunCommand(cmd)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
// K3s needs some time between joining nodes to avoid learner issues
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, err
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
return etcdNodeNames, cpNodeNames, agentNodeNames, nil
|
|
|
|
}
|
2024-04-29 20:57:22 +00:00
|
|
|
|
2022-03-17 23:56:10 +00:00
|
|
|
func Test_E2ESplitServer(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
flag.Parse()
|
2022-11-04 23:07:12 +00:00
|
|
|
suiteConfig, reporterConfig := GinkgoConfiguration()
|
|
|
|
RunSpecs(t, "Split Server Test Suite", suiteConfig, reporterConfig)
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeConfigFile string
|
|
|
|
etcdNodeNames []string
|
|
|
|
cpNodeNames []string
|
|
|
|
agentNodeNames []string
|
|
|
|
)
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
var _ = ReportAfterEach(e2e.GenReport)
|
|
|
|
|
2022-09-06 19:56:37 +00:00
|
|
|
var _ = Describe("Verify Create", Ordered, func() {
|
2022-03-17 23:56:10 +00:00
|
|
|
Context("Cluster :", func() {
|
|
|
|
It("Starts up with no issues", func() {
|
|
|
|
var err error
|
2024-04-29 20:57:22 +00:00
|
|
|
etcdNodeNames, cpNodeNames, agentNodeNames, err = createSplitCluster(*nodeOS, *etcdCount, *controlPlaneCount, *agentCount, *local)
|
2022-10-06 19:27:44 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
2022-03-17 23:56:10 +00:00
|
|
|
fmt.Println("CLUSTER CONFIG")
|
|
|
|
fmt.Println("OS:", *nodeOS)
|
|
|
|
fmt.Println("Etcd Server Nodes:", etcdNodeNames)
|
|
|
|
fmt.Println("Control Plane Server Nodes:", cpNodeNames)
|
|
|
|
fmt.Println("Agent Nodes:", agentNodeNames)
|
|
|
|
kubeConfigFile, err = e2e.GenKubeConfigFile(cpNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Checks Node and Pod Status", func() {
|
|
|
|
fmt.Printf("\nFetching node status\n")
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
nodes, err := e2e.ParseNodes(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, node := range nodes {
|
|
|
|
g.Expect(node.Status).Should(Equal("Ready"))
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-03-17 23:56:10 +00:00
|
|
|
_, _ = e2e.ParseNodes(kubeConfigFile, true)
|
|
|
|
|
|
|
|
fmt.Printf("\nFetching Pods status\n")
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
pods, err := e2e.ParsePods(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, pod := range pods {
|
|
|
|
if strings.Contains(pod.Name, "helm-install") {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Completed"), pod.Name)
|
|
|
|
} else {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Running"), pod.Name)
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-03-17 23:56:10 +00:00
|
|
|
_, _ = e2e.ParsePods(kubeConfigFile, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies ClusterIP Service", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("clusterip.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Cluster IP manifest not deployed")
|
|
|
|
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd := "kubectl get pods -o=name -l k8s-app=nginx-app-clusterip --field-selector=status.phase=Running --kubeconfig=" + kubeConfigFile
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-clusterip"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
|
2022-06-14 15:40:29 +00:00
|
|
|
clusterip, _ := e2e.FetchClusterIP(kubeConfigFile, "nginx-clusterip-svc", false)
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd = "curl -L --insecure http://" + clusterip + "/name.html"
|
2022-03-17 23:56:10 +00:00
|
|
|
for _, nodeName := range cpNodeNames {
|
2022-06-13 20:36:26 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCmdOnNode(cmd, nodeName)
|
|
|
|
}, "120s", "10s").Should(ContainSubstring("test-clusterip"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies NodePort Service", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("nodeport.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "NodePort manifest not deployed")
|
|
|
|
|
|
|
|
for _, nodeName := range cpNodeNames {
|
|
|
|
nodeExternalIP, _ := e2e.FetchNodeExternalIP(nodeName)
|
|
|
|
cmd := "kubectl get service nginx-nodeport-svc --kubeconfig=" + kubeConfigFile + " --output jsonpath=\"{.spec.ports[0].nodePort}\""
|
|
|
|
nodeport, err := e2e.RunCommand(cmd)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd = "kubectl get pods -o=name -l k8s-app=nginx-app-nodeport --field-selector=status.phase=Running --kubeconfig=" + kubeConfigFile
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-nodeport"), "nodeport pod was not created")
|
2022-03-17 23:56:10 +00:00
|
|
|
|
|
|
|
cmd = "curl -L --insecure http://" + nodeExternalIP + ":" + nodeport + "/name.html"
|
2022-06-13 20:36:26 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-nodeport"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies LoadBalancer Service", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("loadbalancer.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Loadbalancer manifest not deployed")
|
|
|
|
|
|
|
|
for _, nodeName := range cpNodeNames {
|
|
|
|
ip, _ := e2e.FetchNodeExternalIP(nodeName)
|
|
|
|
|
|
|
|
cmd := "kubectl get service nginx-loadbalancer-svc --kubeconfig=" + kubeConfigFile + " --output jsonpath=\"{.spec.ports[0].port}\""
|
|
|
|
port, err := e2e.RunCommand(cmd)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd = "kubectl get pods -o=name -l k8s-app=nginx-app-loadbalancer --field-selector=status.phase=Running --kubeconfig=" + kubeConfigFile
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-loadbalancer"), "failed cmd: "+cmd)
|
|
|
|
|
|
|
|
cmd = "curl -L --insecure http://" + ip + ":" + port + "/name.html"
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-loadbalancer"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies Ingress", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("ingress.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Ingress manifest not deployed")
|
|
|
|
|
|
|
|
for _, nodeName := range cpNodeNames {
|
|
|
|
ip, _ := e2e.FetchNodeExternalIP(nodeName)
|
|
|
|
cmd := "curl --header host:foo1.bar.com" + " http://" + ip + "/name.html"
|
2022-06-13 20:36:26 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "240s", "5s").Should(ContainSubstring("test-ingress"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies Daemonset", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("daemonset.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Daemonset manifest not deployed")
|
|
|
|
|
|
|
|
Eventually(func(g Gomega) {
|
2022-06-13 20:36:26 +00:00
|
|
|
pods, _ := e2e.ParsePods(kubeConfigFile, false)
|
2022-03-17 23:56:10 +00:00
|
|
|
count := e2e.CountOfStringInSlice("test-daemonset", pods)
|
|
|
|
fmt.Println("POD COUNT")
|
|
|
|
fmt.Println(count)
|
2022-06-13 20:36:26 +00:00
|
|
|
fmt.Println("CP COUNT")
|
|
|
|
fmt.Println(len(cpNodeNames))
|
|
|
|
g.Expect(len(cpNodeNames)).Should((Equal(count)), "Daemonset pod count does not match cp node count")
|
|
|
|
}, "240s", "10s").Should(Succeed())
|
2022-03-17 23:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies dns access", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("dnsutils.yaml", kubeConfigFile, *hardened)
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "dnsutils manifest not deployed")
|
|
|
|
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd := "kubectl get pods dnsutils --kubeconfig=" + kubeConfigFile
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "420s", "2s").Should(ContainSubstring("dnsutils"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
|
2022-06-13 20:36:26 +00:00
|
|
|
cmd = "kubectl --kubeconfig=" + kubeConfigFile + " exec -i -t dnsutils -- nslookup kubernetes.default"
|
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCommand(cmd)
|
|
|
|
}, "420s", "2s").Should(ContainSubstring("kubernetes.default.svc.cluster.local"), "failed cmd: "+cmd)
|
2022-03-17 23:56:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
var failed bool
|
2022-03-17 23:56:10 +00:00
|
|
|
var _ = AfterEach(func() {
|
2022-09-13 21:09:28 +00:00
|
|
|
failed = failed || CurrentSpecReport().Failed()
|
2022-03-17 23:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2024-04-29 20:57:22 +00:00
|
|
|
if !failed {
|
|
|
|
allNodes := append(cpNodeNames, etcdNodeNames...)
|
|
|
|
allNodes = append(allNodes, agentNodeNames...)
|
|
|
|
Expect(e2e.GetCoverageReport(allNodes)).To(Succeed())
|
|
|
|
}
|
2024-03-05 20:17:20 +00:00
|
|
|
if !failed || *ci {
|
2022-03-17 23:56:10 +00:00
|
|
|
Expect(e2e.DestroyCluster()).To(Succeed())
|
|
|
|
Expect(os.Remove(kubeConfigFile)).To(Succeed())
|
|
|
|
}
|
|
|
|
})
|