2023-04-21 17:01:31 +00:00
|
|
|
package rotateca
|
2023-01-27 20:40:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/k3s-io/k3s/tests/e2e"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Valid nodeOS: generic/ubuntu2004, opensuse/Leap-15.3.x86_64
|
|
|
|
var nodeOS = flag.String("nodeOS", "generic/ubuntu2204", "VM operating system")
|
|
|
|
var serverCount = flag.Int("serverCount", 3, "number of server nodes")
|
|
|
|
var agentCount = flag.Int("agentCount", 1, "number of agent nodes")
|
|
|
|
var ci = flag.Bool("ci", false, "running on CI")
|
2023-08-01 16:32:34 +00:00
|
|
|
var local = flag.Bool("local", false, "deploy a locally built K3s binary")
|
2023-01-27 20:40:29 +00:00
|
|
|
|
|
|
|
// Environment Variables Info:
|
|
|
|
// E2E_RELEASE_VERSION=v1.23.1+k3s2 or nil for latest commit from master
|
|
|
|
|
|
|
|
func Test_E2ECustomCARotation(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
flag.Parse()
|
|
|
|
suiteConfig, reporterConfig := GinkgoConfiguration()
|
2023-03-17 00:56:17 +00:00
|
|
|
RunSpecs(t, "Custom Certificate Rotation Test Suite", suiteConfig, reporterConfig)
|
2023-01-27 20:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeConfigFile string
|
|
|
|
agentNodeNames []string
|
|
|
|
serverNodeNames []string
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = ReportAfterEach(e2e.GenReport)
|
|
|
|
|
|
|
|
var _ = Describe("Verify Custom CA Rotation", Ordered, func() {
|
|
|
|
Context("Custom CA is rotated:", func() {
|
|
|
|
It("Starts up with no issues", func() {
|
|
|
|
var err error
|
2023-08-01 16:32:34 +00:00
|
|
|
if *local {
|
|
|
|
serverNodeNames, agentNodeNames, err = e2e.CreateLocalCluster(*nodeOS, *serverCount, *agentCount)
|
|
|
|
} else {
|
|
|
|
serverNodeNames, agentNodeNames, err = e2e.CreateCluster(*nodeOS, *serverCount, *agentCount)
|
|
|
|
}
|
2023-01-27 20:40:29 +00:00
|
|
|
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 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"))
|
|
|
|
}
|
|
|
|
}, "620s", "5s").Should(Succeed())
|
|
|
|
_, _ = 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "620s", "5s").Should(Succeed())
|
|
|
|
_, _ = e2e.ParsePods(kubeConfigFile, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Generates New CA Certificates", func() {
|
|
|
|
cmds := []string{
|
2023-06-07 23:03:36 +00:00
|
|
|
"mkdir -p /opt/rancher/k3s/server",
|
|
|
|
"cp -r /var/lib/rancher/k3s/server/tls /opt/rancher/k3s/server",
|
|
|
|
"DATA_DIR=/opt/rancher/k3s /tmp/generate-custom-ca-certs.sh",
|
2023-01-27 20:40:29 +00:00
|
|
|
}
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
_, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Rotates CA Certificates", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s certificate rotate-ca --path=/opt/rancher/k3s/server"
|
2023-01-27 20:40:29 +00:00
|
|
|
_, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Restarts K3s servers", func() {
|
|
|
|
Expect(e2e.RestartCluster(serverNodeNames)).To(Succeed())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Restarts K3s agents", func() {
|
|
|
|
Expect(e2e.RestartCluster(agentNodeNames)).To(Succeed())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Checks node and pod 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"))
|
|
|
|
}
|
|
|
|
}, "420s", "5s").Should(Succeed())
|
|
|
|
|
|
|
|
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())
|
|
|
|
_, _ = e2e.ParseNodes(kubeConfigFile, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var failed bool
|
|
|
|
var _ = AfterEach(func() {
|
|
|
|
failed = failed || CurrentSpecReport().Failed()
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
|
|
|
if failed && !*ci {
|
|
|
|
fmt.Println("FAILED!")
|
|
|
|
} else {
|
2023-08-01 16:32:34 +00:00
|
|
|
Expect(e2e.GetCoverageReport(append(serverNodeNames, agentNodeNames...))).To(Succeed())
|
2023-01-27 20:40:29 +00:00
|
|
|
Expect(e2e.DestroyCluster()).To(Succeed())
|
|
|
|
Expect(os.Remove(kubeConfigFile)).To(Succeed())
|
|
|
|
}
|
|
|
|
})
|