From 3674b39abb3b86307fbc9d9fb4d0d4682f2838d9 Mon Sep 17 00:00:00 2001 From: Manuel Buil Date: Fri, 16 Feb 2024 18:23:10 +0100 Subject: [PATCH] Add a multus e2e test Signed-off-by: Manuel Buil --- tests/e2e/multus/Vagrantfile | 97 +++++++++++++++++++ tests/e2e/multus/multus_test.go | 111 ++++++++++++++++++++++ tests/e2e/resource_files/multus-pods.yaml | 74 +++++++++++++++ tests/e2e/scripts/ipv6.sh | 6 +- 4 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/multus/Vagrantfile create mode 100644 tests/e2e/multus/multus_test.go create mode 100644 tests/e2e/resource_files/multus-pods.yaml diff --git a/tests/e2e/multus/Vagrantfile b/tests/e2e/multus/Vagrantfile new file mode 100644 index 0000000000..5bc5a31263 --- /dev/null +++ b/tests/e2e/multus/Vagrantfile @@ -0,0 +1,97 @@ +ENV['VAGRANT_NO_PARALLEL'] = 'no' +NODE_ROLES = (ENV['E2E_NODE_ROLES'] || ["server-0", "agent-0" ]) +NODE_BOXES = (ENV['E2E_NODE_BOXES'] || ['generic/ubuntu2004', 'generic/ubuntu2004']) +GITHUB_BRANCH = (ENV['E2E_GITHUB_BRANCH'] || "master") +RELEASE_VERSION = (ENV['E2E_RELEASE_VERSION'] || "") +NODE_CPUS = (ENV['E2E_NODE_CPUS'] || 2).to_i +NODE_MEMORY = (ENV['E2E_NODE_MEMORY'] || 3072).to_i +CNI = (ENV['E2E_CNI'] || "multus,canal") # canal, cilium (with cni.exclusive: false in the chart values) and calico supported +NETWORK4_PREFIX = "10.10.10" +NETWORK6_PREFIX = "fd11:decf:c0ff:ee" +install_type = "" + +def provision(vm, roles, role_num, node_num) + vm.box = NODE_BOXES[node_num] + vm.hostname = "#{roles[0]}-#{role_num}" + node_ip4 = "#{NETWORK4_PREFIX}.#{100+node_num}" + node_ip6 = "#{NETWORK6_PREFIX}::#{10+node_num}" + node_ip6_gw = "#{NETWORK6_PREFIX}::1" + # Only works with libvirt, which allows IPv4 + IPv6 on a single network/interface + vm.network "private_network", + :ip => node_ip4, + :netmask => "255.255.255.0", + :libvirt__dhcp_enabled => false, + :libvirt__forward_mode => "none", + :libvirt__guest_ipv6 => "yes", + :libvirt__ipv6_address => "#{NETWORK6_PREFIX}::1", + :libvirt__ipv6_prefix => "64" + + vagrant_defaults = File.exists?("./vagrantdefaults.rb") ? "./vagrantdefaults.rb" : "../vagrantdefaults.rb" + load vagrant_defaults + + defaultOSConfigure(vm) + + scripts_location = Dir.exists?("./scripts") ? "./scripts" : "../scripts" + vm.provision "IPv6 Setup", type: "shell", path: scripts_location + "/ipv6.sh", args: [node_ip4, node_ip6, node_ip6_gw, CNI, vm.box] + + install_type = getInstallType(vm, RELEASE_VERSION, GITHUB_BRANCH) + vm.provision "Ping Check", type: "shell", inline: "ping -4 -c 2 rke2.io" + + if roles.include?("server") + vm.provision :rke2, run: 'once' do |rke2| + rke2.env = %W[INSTALL_RKE2_TYPE=server #{install_type}] + rke2.config_mode = '0644' # side-step https://github.com/k3s-io/k3s/issues/4321 + rke2.config = <<~YAML + write-kubeconfig-mode: '0644' + node-external-ip: #{node_ip4},#{node_ip6} + node-ip: #{node_ip4},#{node_ip6} + token: vagrant-rke2 + cluster-cidr: 10.42.0.0/16,2001:cafe:42:0::/56 + service-cidr: 10.43.0.0/16,2001:cafe:43:0::/112 + bind-address: #{NETWORK4_PREFIX}.100 + cni: #{CNI} + YAML + end + end + if roles.include?("agent") + vm.provision :rke2, run: 'once' do |rke2| + rke2.env = %W[INSTALL_RKE2_TYPE=agent #{install_type}] + rke2.config_mode = '0644' # side-step https://github.com/k3s-io/k3s/issues/4321 + rke2.install_path = false + rke2.config = <<~YAML + write-kubeconfig-mode: '0644' + node-external-ip: #{node_ip4},#{node_ip6} + node-ip: #{node_ip4},#{node_ip6} + server: https://#{NETWORK4_PREFIX}.100:9345 + token: vagrant-rke2 + cni: #{CNI} + YAML + end + end +end + +Vagrant.configure("2") do |config| + config.vagrant.plugins = ["vagrant-rke2", "vagrant-reload", "vagrant-libvirt"] + config.vm.provider "libvirt" do |v| + v.cpus = NODE_CPUS + v.memory = NODE_MEMORY + end + + if NODE_ROLES.kind_of?(String) + NODE_ROLES = NODE_ROLES.split(" ", -1) + end + if NODE_BOXES.kind_of?(String) + NODE_BOXES = NODE_BOXES.split(" ", -1) + end + + # Must iterate on the index, vagrant does not understand iterating + # over the node roles themselves + NODE_ROLES.length.times do |i| + name = NODE_ROLES[i] + config.vm.define name do |node| + roles = name.split("-", -1) + role_num = roles.pop.to_i + provision(node.vm, roles, role_num, i) + end + end +end diff --git a/tests/e2e/multus/multus_test.go b/tests/e2e/multus/multus_test.go new file mode 100644 index 0000000000..24f28a3b54 --- /dev/null +++ b/tests/e2e/multus/multus_test.go @@ -0,0 +1,111 @@ +package multus + +import ( + "flag" + "fmt" + + "os" + "strings" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/rancher/rke2/tests/e2e" +) + +// Valid nodeOS: generic/ubuntu2004, opensuse/Leap-15.3.x86_64 +var nodeOS = flag.String("nodeOS", "generic/ubuntu2004", "VM operating system") +var serverCount = flag.Int("serverCount", 1, "number of server nodes") +var agentCount = flag.Int("agentCount", 1, "number of agent nodes") +var ci = flag.Bool("ci", false, "running on CI") + +// Environment Variables Info: +// E2E_RELEASE_VERSION=v1.23.1+rke2r1 or nil for latest commit from master + +func Test_E2Emultus(t *testing.T) { + flag.Parse() + RegisterFailHandler(Fail) + suiteConfig, reporterConfig := GinkgoConfiguration() + RunSpecs(t, "Validate multus Test Suite", suiteConfig, reporterConfig) +} + +var ( + kubeConfigFile string + serverNodeNames []string + agentNodeNames []string +) +var _ = ReportAfterEach(e2e.GenReport) + +var _ = Describe("Verify multus Configuration", Ordered, func() { + + It("Starts up with no issues", func() { + var err error + serverNodeNames, agentNodeNames, err = e2e.CreateCluster(*nodeOS, *serverCount, *agentCount) + Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err)) + fmt.Println("CLUSTER CONFIG") + fmt.Println("OS:", *nodeOS) + fmt.Println("Server Nodes:", serverNodeNames) + fmt.Println("Agent Nodes:", agentNodeNames) + kubeConfigFile, err = e2e.GenKubeConfigFile(serverNodeNames[0]) + Expect(err).NotTo(HaveOccurred()) + }) + + It("Checks Node Status", func() { + 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")) + } + }, "620s", "5s").Should(Succeed()) + _, err := e2e.ParseNodes(kubeConfigFile, true) + Expect(err).NotTo(HaveOccurred()) + }) + + It("Checks Pod Status", func() { + 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) + } + } + }, "420s", "5s").Should(Succeed()) + _, err := e2e.ParsePods(kubeConfigFile, true) + Expect(err).NotTo(HaveOccurred()) + }) + + It("Verifies multus daemonset comes up", func() { + Eventually(func() (string, error) { + cmd := "kubectl get ds rke2-multus -n kube-system -o jsonpath='{.status.numberReady}' --kubeconfig=" + kubeConfigFile + return e2e.RunCommand(cmd) + }, "120s", "5s").Should(ContainSubstring("2")) + }) + + It("Verifies macvlan communication via multus is working", func() { + _, err := e2e.DeployWorkload("multus-pods.yaml", kubeConfigFile) + Expect(err).NotTo(HaveOccurred()) + cmd := "kubectl exec pod-macvlan --kubeconfig=" + kubeConfigFile + " -- ping -c 1 -w 2 10.1.1.102" + Eventually(func() (string, error) { + return e2e.RunCommand(cmd) + }, "20s", "3s").Should(ContainSubstring("0% packet loss"), "failed cmd: "+cmd) + + }) +}) + +var failed bool +var _ = AfterEach(func() { + failed = failed || CurrentSpecReport().Failed() +}) + +var _ = AfterSuite(func() { + if failed && !*ci { + fmt.Println("FAILED!") + } else { + Expect(e2e.DestroyCluster()).To(Succeed()) + Expect(os.Remove(kubeConfigFile)).To(Succeed()) + } +}) diff --git a/tests/e2e/resource_files/multus-pods.yaml b/tests/e2e/resource_files/multus-pods.yaml new file mode 100644 index 0000000000..d93f9b1c68 --- /dev/null +++ b/tests/e2e/resource_files/multus-pods.yaml @@ -0,0 +1,74 @@ +apiVersion: "k8s.cni.cncf.io/v1" +kind: NetworkAttachmentDefinition +metadata: + name: macvlan-conf +spec: + config: '{ + "cniVersion": "0.3.1", + "plugins": [ + { + "type": "macvlan", + "capabilities": { "ips": true }, + "master": "eth1", + "mode": "bridge", + "ipam": { + "type": "static", + "routes": [ + { + "dst": "0.0.0.0/0", + "gw": "10.1.1.1" + } + ] + } + }, { + "capabilities": { "mac": true }, + "type": "tuning" + } + ] + }' +--- + +apiVersion: v1 +kind: Pod +metadata: + labels: + app: pod-macvlan + name: pod-macvlan + annotations: + k8s.v1.cni.cncf.io/networks: '[ + { "name": "macvlan-conf", + "ips": [ "10.1.1.101/24" ], + "mac": "c2:b0:57:49:47:f1", + "gateway": [ "10.1.1.1" ] + }]' +spec: + containers: + - image: praqma/network-multitool + imagePullPolicy: Always + name: multitool + securityContext: + capabilities: + add: ["NET_ADMIN","NET_RAW"] +--- + +apiVersion: v1 +kind: Pod +metadata: + labels: + app: pod2-macvlan + name: pod2-macvlan + annotations: + k8s.v1.cni.cncf.io/networks: '[ + { "name": "macvlan-conf", + "ips": [ "10.1.1.102/24" ], + "mac": "c2:b0:57:45:47:f1", + "gateway": [ "10.1.1.1" ] + }]' +spec: + containers: + - image: praqma/network-multitool + imagePullPolicy: Always + name: multitool + securityContext: + capabilities: + add: ["NET_ADMIN","NET_RAW"] diff --git a/tests/e2e/scripts/ipv6.sh b/tests/e2e/scripts/ipv6.sh index a3d0da37e0..a29b364d99 100644 --- a/tests/e2e/scripts/ipv6.sh +++ b/tests/e2e/scripts/ipv6.sh @@ -34,7 +34,7 @@ net.ipv6.conf.eth1.accept_dad=0" > /etc/sysctl.conf mkdir -p /var/lib/rancher/rke2/server/manifests case "$cni" in - "canal") + *canal*) echo "Creating canal chart" echo "apiVersion: helm.cattle.io/v1 kind: HelmChartConfig @@ -50,7 +50,7 @@ spec: ip6AutoDetectionMethod: \"interface=eth1.*\"" >> /var/lib/rancher/rke2/server/manifests/e2e-canal.yaml ;; - "cilium") + *cilium*) echo "Creating cilium chart" echo "apiVersion: helm.cattle.io/v1 kind: HelmChartConfig @@ -64,7 +64,7 @@ spec: enabled: true">> /var/lib/rancher/rke2/server/manifests/e2e-cilium.yaml ;; - "calico") + *calico*) echo "Creating calico chart" echo "apiVersion: helm.cattle.io/v1 kind: HelmChartConfig