mirror of https://github.com/k3s-io/k3s
add e2e startup test for rootless k3s (#8383)
* add test for rootless k3s Signed-off-by: Ian Cardoso <osodracnai@gmail.com> * fix comments Signed-off-by: Ian Cardoso <osodracnai@gmail.com> * Cleanup rootless e2e test, simplify logic Signed-off-by: Derek Nola <derek.nola@suse.com> --------- Signed-off-by: Ian Cardoso <osodracnai@gmail.com> Signed-off-by: Derek Nola <derek.nola@suse.com> Co-authored-by: Derek Nola <derek.nola@suse.com>pull/9195/head
parent
64dbbba996
commit
df5e983fc8
|
@ -0,0 +1,68 @@
|
|||
ENV['VAGRANT_NO_PARALLEL'] = 'no'
|
||||
NODE_ROLES = (ENV['E2E_NODE_ROLES'] || ["server-0"])
|
||||
NODE_BOXES = (ENV['E2E_NODE_BOXES'] || ['generic/ubuntu2204'])
|
||||
GITHUB_BRANCH = (ENV['E2E_GITHUB_BRANCH'] || "master")
|
||||
RELEASE_VERSION = (ENV['E2E_RELEASE_VERSION'] || "")
|
||||
GOCOVER = (ENV['E2E_GOCOVER'] || "")
|
||||
NODE_CPUS = (ENV['E2E_NODE_CPUS'] || 2).to_i
|
||||
NODE_MEMORY = (ENV['E2E_NODE_MEMORY'] || 2048).to_i
|
||||
# Virtualbox >= 6.1.28 require `/etc/vbox/network.conf` for expanded private networks
|
||||
NETWORK_PREFIX = "10.10.10"
|
||||
install_type = ""
|
||||
|
||||
def provision(vm, role, role_num, node_num)
|
||||
vm.box = NODE_BOXES[node_num]
|
||||
vm.hostname = role
|
||||
# An expanded netmask is required to allow VM<-->VM communication, virtualbox defaults to /32
|
||||
vm.network "private_network", ip: "#{NETWORK_PREFIX}.#{100+node_num}", netmask: "255.255.255.0"
|
||||
|
||||
vagrant_defaults = '../vagrantdefaults.rb'
|
||||
scripts_location = Dir.exists?("./scripts") ? "./scripts" : "../scripts"
|
||||
load vagrant_defaults if File.exists?(vagrant_defaults)
|
||||
|
||||
defaultOSConfigure(vm)
|
||||
addCoverageDir(vm, role, GOCOVER)
|
||||
|
||||
vm.provision "shell", inline: "ping -c 2 k3s.io"
|
||||
vm.provision 'k3s-install', type: 'k3s', run: 'once' do |k3s|
|
||||
k3s.args = "server "
|
||||
k3s.env = %W[K3S_KUBECONFIG_MODE=0644 INSTALL_K3S_SKIP_START=true INSTALL_K3S_SKIP_ENABLE=true]
|
||||
k3s.env_owner = "vagrant:vagrant"
|
||||
k3s.config_mode = '0644' # side-step https://github.com/k3s-io/k3s/issues/4321
|
||||
k3s.config_owner = "vagrant:vagrant"
|
||||
k3s.config = <<~YAML
|
||||
token: vagrant
|
||||
node-external-ip: #{NETWORK_PREFIX}.100
|
||||
YAML
|
||||
end
|
||||
|
||||
vm.provision "Download k3s-rootless and enable cgroup v2", type: "shell", path: scripts_location + "/setup_rootless.sh"
|
||||
vm.provision 'k3s-reload', type: 'reload', run: 'once'
|
||||
end
|
||||
|
||||
Vagrant.configure("2") do |config|
|
||||
config.vagrant.plugins = ["vagrant-k3s", "vagrant-reload"]
|
||||
# Default provider is libvirt, virtualbox is only provided as a backup
|
||||
config.vm.provider "libvirt" do |v|
|
||||
v.cpus = NODE_CPUS
|
||||
v.memory = NODE_MEMORY
|
||||
end
|
||||
config.vm.provider "virtualbox" 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
|
||||
|
||||
NODE_ROLES.each_with_index do |name, i|
|
||||
role_num = name.split("-", -1).pop.to_i
|
||||
config.vm.define name do |node|
|
||||
provision(node.vm, name, role_num, i)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,177 @@
|
|||
package rootless
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/k3s-io/k3s/tests/e2e"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
// Rootless is only valid on a single node, but requires node/kernel configuration, requiring a E2E test environment.
|
||||
|
||||
// Valid nodeOS: generic/ubuntu2004, opensuse/Leap-15.3.x86_64
|
||||
var nodeOS = flag.String("nodeOS", "generic/ubuntu2204", "VM operating system")
|
||||
var ci = flag.Bool("ci", false, "running on CI")
|
||||
var local = flag.Bool("local", false, "deploy a locally built K3s binary")
|
||||
|
||||
// Environment Variables Info:
|
||||
// E2E_RELEASE_VERSION=v1.27.1+k3s2 or nil for latest commit from master
|
||||
|
||||
func Test_E2ERootlessStartupValidation(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
flag.Parse()
|
||||
suiteConfig, reporterConfig := GinkgoConfiguration()
|
||||
RunSpecs(t, "Startup Test Suite", suiteConfig, reporterConfig)
|
||||
}
|
||||
|
||||
var (
|
||||
kubeConfigFile string
|
||||
serverNodeNames []string
|
||||
)
|
||||
|
||||
func StartK3sCluster(nodes []string, serverYAML string) error {
|
||||
for _, node := range nodes {
|
||||
|
||||
resetCmd := "head -n 3 /etc/rancher/k3s/config.yaml > /tmp/config.yaml && sudo mv /tmp/config.yaml /etc/rancher/k3s/config.yaml"
|
||||
yamlCmd := fmt.Sprintf("echo '%s' >> /etc/rancher/k3s/config.yaml", serverYAML)
|
||||
startCmd := "systemctl --user restart k3s-rootless"
|
||||
|
||||
if _, err := e2e.RunCmdOnNode(resetCmd, node); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := e2e.RunCmdOnNode(yamlCmd, node); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := RunCmdOnRootlesNode("systemctl --user daemon-reload", node); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := RunCmdOnRootlesNode(startCmd, node); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func KillK3sCluster(nodes []string) error {
|
||||
for _, node := range nodes {
|
||||
if _, err := RunCmdOnRootlesNode(`systemctl --user stop k3s-rootless`, node); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := RunCmdOnRootlesNode("k3s-killall.sh", node); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := RunCmdOnRootlesNode("rm -rf /home/vagrant/.rancher/k3s/server/db", node); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ = ReportAfterEach(e2e.GenReport)
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
var err error
|
||||
if *local {
|
||||
serverNodeNames, _, err = e2e.CreateLocalCluster(*nodeOS, 1, 0)
|
||||
} else {
|
||||
serverNodeNames, _, err = e2e.CreateCluster(*nodeOS, 1, 0)
|
||||
}
|
||||
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
||||
//Checks if system is using cgroup v2
|
||||
_, err = e2e.RunCmdOnNode("cat /sys/fs/cgroup/cgroup.controllers", serverNodeNames[0])
|
||||
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
||||
|
||||
})
|
||||
|
||||
var _ = Describe("Various Startup Configurations", Ordered, func() {
|
||||
Context("Verify standard startup :", func() {
|
||||
It("Starts K3s with no issues", func() {
|
||||
err := StartK3sCluster(serverNodeNames, "")
|
||||
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
||||
|
||||
fmt.Println("CLUSTER CONFIG")
|
||||
fmt.Println("OS:", *nodeOS)
|
||||
fmt.Println("Server Nodes:", serverNodeNames)
|
||||
kubeConfigFile, err = GenRootlessKubeConfigFile(serverNodeNames[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"))
|
||||
}
|
||||
}, "360s", "5s").Should(Succeed())
|
||||
_, _ = e2e.ParseNodes(kubeConfigFile, false)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}, "360s", "5s").Should(Succeed())
|
||||
_, _ = e2e.ParsePods(kubeConfigFile, false)
|
||||
})
|
||||
|
||||
It("Returns pod metrics", func() {
|
||||
cmd := "kubectl top pod -A"
|
||||
Eventually(func() error {
|
||||
_, err := e2e.RunCommand(cmd)
|
||||
return err
|
||||
}, "600s", "5s").Should(Succeed())
|
||||
})
|
||||
|
||||
It("Returns node metrics", func() {
|
||||
cmd := "kubectl top node"
|
||||
_, err := e2e.RunCommand(cmd)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Runs an interactive command a pod", func() {
|
||||
cmd := "kubectl run busybox --rm -it --restart=Never --image=rancher/mirrored-library-busybox:1.34.1 -- uname -a"
|
||||
_, err := e2e.RunCommand(cmd)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Collects logs from a pod", func() {
|
||||
cmd := "kubectl logs -n kube-system -l app.kubernetes.io/name=traefik -c traefik"
|
||||
_, err := e2e.RunCommand(cmd)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Kills the cluster", func() {
|
||||
err := KillK3sCluster(serverNodeNames)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
var failed bool
|
||||
var _ = AfterEach(func() {
|
||||
failed = failed || CurrentSpecReport().Failed()
|
||||
})
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
if failed && !*ci {
|
||||
fmt.Println("FAILED!")
|
||||
} else {
|
||||
Expect(e2e.GetCoverageReport(serverNodeNames)).To(Succeed())
|
||||
Expect(e2e.DestroyCluster()).To(Succeed())
|
||||
Expect(os.Remove(kubeConfigFile)).To(Succeed())
|
||||
}
|
||||
})
|
|
@ -0,0 +1,43 @@
|
|||
package rootless
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/k3s-io/k3s/tests/e2e"
|
||||
)
|
||||
|
||||
// RunCmdOnRootlesNode executes a command from within the given node as user vagrant
|
||||
func RunCmdOnRootlesNode(cmd string, nodename string) (string, error) {
|
||||
injectEnv := ""
|
||||
if _, ok := os.LookupEnv("E2E_GOCOVER"); ok && strings.HasPrefix(cmd, "k3s") {
|
||||
injectEnv = "GOCOVERDIR=/tmp/k3scov "
|
||||
}
|
||||
runcmd := "vagrant ssh " + nodename + " -c \"" + injectEnv + cmd + "\""
|
||||
out, err := e2e.RunCommand(runcmd)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("failed to run command: %s on node %s: %s, %v", cmd, nodename, out, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func GenRootlessKubeConfigFile(serverName string) (string, error) {
|
||||
kubeConfig, err := RunCmdOnRootlesNode("cat /home/vagrant/.kube/k3s.yaml", serverName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nodeIP, err := e2e.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
|
||||
}
|
||||
if err := os.Setenv("E2E_KUBECONFIG", kubeConfigFile); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return kubeConfigFile, nil
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#!/bin/sh
|
||||
|
||||
# GitHub repository URL
|
||||
github_url="https://raw.githubusercontent.com/k3s-io/k3s/master/k3s-rootless.service"
|
||||
|
||||
# Destination file path
|
||||
destination_path="/home/vagrant/.config/systemd/user/"
|
||||
|
||||
# Download the file from GitHub using curl
|
||||
curl -LJO "$github_url"
|
||||
|
||||
# Check if the download was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
# Move the downloaded file to the desired destination
|
||||
mkdir -p "$destination_path"
|
||||
mv "k3s-rootless.service" "$destination_path/k3s-rootless.service"
|
||||
# Add EnvironmentFile=-/etc/k3s-rootless.env to the service file
|
||||
sed -i 's/^\[Service\]$/\[Service\]\nEnvironmentFile=-\/etc\/rancher\/k3s\/k3s.env/' "$destination_path/k3s-rootless.service"
|
||||
chown -R vagrant:vagrant /home/vagrant/.config/
|
||||
|
||||
echo "File downloaded and moved to $destination_path"
|
||||
else
|
||||
echo "Failed to download the file from GitHub."
|
||||
fi
|
||||
|
||||
|
||||
# Enable IPv4 forwarding
|
||||
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
|
||||
sysctl --system
|
||||
|
||||
|
||||
# Check if the string is already in GRUB_CMDLINE_LINUX
|
||||
if grep -qxF "GRUB_CMDLINE_LINUX=\"systemd.unified_cgroup_hierarchy=1 \"" /etc/default/grub; then
|
||||
echo "String is already in GRUB_CMDLINE_LINUX. No changes made."
|
||||
else
|
||||
# Add the string to GRUB_CMDLINE_LINUX
|
||||
sed -i "s/\(GRUB_CMDLINE_LINUX=\)\"\(.*\)\"/\1\"systemd.unified_cgroup_hierarchy=1 \2\"/" /etc/default/grub
|
||||
|
||||
# Update GRUB
|
||||
update-grub
|
||||
|
||||
echo "String 'systemd.unified_cgroup_hierarchy=1' added to GRUB_CMDLINE_LINUX and GRUB updated successfully."
|
||||
fi
|
||||
|
||||
mkdir -p /etc/systemd/system/user@.service.d
|
||||
echo "[Service]
|
||||
Delegate=cpu cpuset io memory pids
|
||||
">> /etc/systemd/system/user@.service.d/delegate.conf
|
||||
apt-get install -y uidmap
|
||||
|
||||
systemctl daemon-reload
|
||||
loginctl enable-linger vagrant
|
||||
# We need to run this as vagrant user, because rootless k3s will be run as vagrant user
|
||||
su -c 'XDG_RUNTIME_DIR="/run/user/$UID" DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" systemctl --user daemon-reload' vagrant
|
||||
su -c 'XDG_RUNTIME_DIR="/run/user/$UID" DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" systemctl --user enable --now k3s-rootless' vagrant
|
||||
|
Loading…
Reference in New Issue