2021-07-28 16:28:26 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-12-22 17:51:48 +00:00
|
|
|
"os"
|
2021-07-28 16:28:26 +00:00
|
|
|
"os/exec"
|
2021-12-22 17:51:48 +00:00
|
|
|
"strconv"
|
2021-07-28 16:28:26 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Name string
|
|
|
|
Status string
|
|
|
|
Roles string
|
|
|
|
InternalIP string
|
|
|
|
ExternalIP string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pod struct {
|
|
|
|
NameSpace string
|
|
|
|
Name string
|
|
|
|
Ready string
|
|
|
|
Status string
|
|
|
|
Restarts string
|
|
|
|
NodeIP string
|
|
|
|
Node string
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
//Runs command passed from within the node ServerIP
|
|
|
|
func RunCmdOnNode(cmd string, nodename string) (string, error) {
|
|
|
|
runcmd := "vagrant ssh server-0 -c " + cmd
|
|
|
|
c := exec.Command("bash", "-c", runcmd)
|
2021-07-28 16:28:26 +00:00
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
var errOut bytes.Buffer
|
|
|
|
c.Stdout = &out
|
|
|
|
c.Stderr = &errOut
|
|
|
|
if err := c.Run(); err != nil {
|
|
|
|
return errOut.String(), err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
return out.String(), nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
// RunCommand Runs command on the cluster accessing the cluster through kubeconfig file
|
|
|
|
func RunCommand(cmd string) (string, error) {
|
|
|
|
c := exec.Command("bash", "-c", cmd)
|
|
|
|
var out bytes.Buffer
|
|
|
|
c.Stdout = &out
|
|
|
|
if err := c.Run(); err != nil {
|
|
|
|
return "", err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
return out.String(), nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func CreateCluster(nodeos string, serverCount int, agentCount int) ([]string, []string, error) {
|
|
|
|
serverNodenames := make([]string, serverCount+1)
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
serverNodenames[i] = "server-" + strconv.Itoa(i)
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
agentNodenames := make([]string, agentCount+1)
|
|
|
|
for i := 0; i < agentCount; i++ {
|
|
|
|
agentNodenames[i] = "agent-" + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
nodeRoles := strings.Join(serverNodenames, " ") + strings.Join(agentNodenames, " ")
|
|
|
|
nodeRoles = strings.TrimSpace(nodeRoles)
|
|
|
|
nodeBoxes := strings.Repeat(nodeos+" ", serverCount+agentCount)
|
|
|
|
nodeBoxes = strings.TrimSpace(nodeBoxes)
|
|
|
|
cmd := fmt.Sprintf("NODE_ROLES=\"%s\" NODE_BOXES=\"%s\" vagrant up &> vagrant.log", nodeRoles, nodeBoxes)
|
|
|
|
if out, err := RunCommand(cmd); err != nil {
|
|
|
|
fmt.Println("Error Creating Cluster", out)
|
|
|
|
return nil, nil, err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
return serverNodenames, agentNodenames, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func DestroyCluster() error {
|
|
|
|
if _, err := RunCommand("vagrant destroy -f"); err != nil {
|
|
|
|
return err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
return os.Remove("vagrant.log")
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func GenKubeConfigFile(serverName string) (string, error) {
|
|
|
|
cmd := fmt.Sprintf("vagrant ssh %s -c \"cat /etc/rancher/k3s/k3s.yaml\"", serverName)
|
|
|
|
kubeConfig, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
2021-12-22 17:51:48 +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
|
|
|
|
}
|
|
|
|
return kubeConfigFile, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeployWorkload(workload string, kubeconfig string) (string, error) {
|
|
|
|
cmd := "kubectl apply -f " + workload + " --kubeconfig=" + kubeconfig
|
|
|
|
return RunCommand(cmd)
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func FetchClusterIP(kubeconfig string, servicename string) (string, error) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
func FetchIngressIP(kubeconfig string) ([]string, error) {
|
2021-07-28 16:28:26 +00:00
|
|
|
cmd := "kubectl get ing ingress -o jsonpath='{.status.loadBalancer.ingress[*].ip}' --kubeconfig=" + kubeconfig
|
2021-12-22 17:51:48 +00:00
|
|
|
res, err := RunCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ingressIP := strings.Trim(res, " ")
|
|
|
|
ingressIPs := strings.Split(ingressIP, " ")
|
|
|
|
return ingressIPs, nil
|
2021-07-28 16:28:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func ParseNodes(kubeConfig string, debug 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)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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],
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
if debug {
|
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
|
|
|
}
|
|
|
|
|
2021-12-22 17:51:48 +00:00
|
|
|
func ParsePods(kubeconfig string, debug bool) ([]Pod, error) {
|
2021-07-28 16:28:26 +00:00
|
|
|
pods := make([]Pod, 0, 10)
|
|
|
|
podList := ""
|
2021-12-22 17:51:48 +00:00
|
|
|
|
|
|
|
cmd := "kubectl get pods -o wide --no-headers -A --kubeconfig=" + kubeconfig
|
|
|
|
res, _ := RunCommand(cmd)
|
|
|
|
res = strings.TrimSpace(res)
|
|
|
|
podList = res
|
|
|
|
|
|
|
|
split := strings.Split(res, "\n")
|
|
|
|
for _, rec := range split {
|
|
|
|
fields := strings.Fields(string(rec))
|
|
|
|
pod := Pod{
|
|
|
|
NameSpace: fields[0],
|
|
|
|
Name: fields[1],
|
|
|
|
Ready: fields[2],
|
|
|
|
Status: fields[3],
|
|
|
|
Restarts: fields[4],
|
|
|
|
NodeIP: fields[6],
|
|
|
|
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
|
|
|
}
|
2021-12-22 17:51:48 +00:00
|
|
|
if debug {
|
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
|
|
|
}
|