Skip to content

Commit ca32dce

Browse files
author
sivakami
committed
add datapath tests in gingko for long running pipeline
1 parent 913120a commit ca32dce

File tree

6 files changed

+262
-0
lines changed

6 files changed

+262
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: {{ .PodName }}
5+
labels:
6+
kubernetes.azure.com/pod-network-instance: {{ .PNIName }}
7+
kubernetes.azure.com/pod-network: {{ .PNName }}
8+
spec:
9+
nodeName: {{ .NodeName }}
10+
nodeSelector:
11+
kubernetes.io/os: {{ .OS }}
12+
containers:
13+
- name: net-debugger
14+
image: {{ .Image }}
15+
command: ["/bin/sh", "-c"]
16+
args:
17+
- |
18+
echo "Pod Network Diagnostics started on $(hostname)";
19+
while true; do
20+
ip addr show
21+
ip route show
22+
sleep 60
23+
done
24+
resources:
25+
limits:
26+
cpu: 300m
27+
memory: 600Mi
28+
requests:
29+
cpu: 300m
30+
memory: 600Mi
31+
securityContext:
32+
privileged: true
33+
restartPolicy: Always
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: acn.azure.com/v1alpha1
2+
kind: PodNetwork
3+
metadata:
4+
name: {{ .PNName }}
5+
{{- if .SubnetToken }}
6+
labels:
7+
kubernetes.azure.com/override-subnet-token: "{{ .SubnetToken }}"
8+
{{- end }}
9+
spec:
10+
networkID: "{{ .VnetGUID }}"
11+
{{- if not .SubnetToken }}
12+
subnetGUID: "{{ .SubnetGUID }}"
13+
{{- end }}
14+
subnetResourceID: "{{ .SubnetARMID }}"
15+
deviceType: VnetNIC
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: acn.azure.com/v1alpha1
2+
kind: PodNetworkInstance
3+
metadata:
4+
name: {{ .PNIName }}
5+
namespace: {{ .Namespace }}
6+
spec:
7+
podNetworkConfigs:
8+
- podNetwork: {{ .PNName }}
9+
{{- if eq .Type "explicit" }}
10+
podIPReservationSize: {{ .Reservations }}
11+
{{- end }}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strings"
7+
)
8+
9+
func runAzCommand(cmd string, args ...string) string {
10+
out, err := exec.Command(cmd, args...).CombinedOutput()
11+
if err != nil {
12+
panic(fmt.Sprintf("Failed to run %s %v: %s", cmd, args, string(out)))
13+
}
14+
return strings.TrimSpace(string(out))
15+
}
16+
17+
func GetVnetGUID(rg, vnet string) string {
18+
return runAzCommand("az", "network", "vnet", "show", "--resource-group", rg, "--name", vnet, "--query", "id", "-o", "tsv")
19+
}
20+
21+
func GetSubnetARMID(rg, vnet, subnet string) string {
22+
return runAzCommand("az", "network", "vnet", "subnet", "show", "--resource-group", rg, "--vnet-name", vnet, "--name", subnet, "--query", "id", "-o", "tsv")
23+
}
24+
25+
func GetSubnetGUID(rg, vnet, subnet string) string {
26+
subnetID := GetSubnetARMID(rg, vnet, subnet)
27+
return runAzCommand("az", "resource", "show", "--ids", subnetID, "--api-version", "2023-09-01", "--query", "properties.serviceAssociationLinks[0].properties.subnetId", "-o", "tsv")
28+
}
29+
30+
func GetSubnetToken(rg, vnet, subnet string) string {
31+
// Optionally implement if you use subnet token override
32+
return ""
33+
}
34+
35+
// GetClusterNodes returns a slice of node names from a cluster using the given kubeconfig
36+
func GetClusterNodes(kubeconfig string) []string {
37+
cmd := exec.Command("kubectl", "--kubeconfig", kubeconfig, "get", "nodes", "-o", "name")
38+
out, err := cmd.CombinedOutput()
39+
if err != nil {
40+
panic(fmt.Sprintf("Failed to get nodes using kubeconfig %s: %s\n%s", kubeconfig, err, string(out)))
41+
}
42+
43+
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
44+
nodes := make([]string, 0, len(lines))
45+
46+
for _, line := range lines {
47+
// kubectl returns "node/<node-name>", we strip the prefix
48+
if strings.HasPrefix(line, "node/") {
49+
nodes = append(nodes, strings.TrimPrefix(line, "node/"))
50+
}
51+
}
52+
return nodes
53+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package long_running_cluster
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
10+
"github.com/Azure/azure-container-networking/test/integration/swiftv2/helpers"
11+
)
12+
13+
func TestDatapath(t *testing.T) {
14+
RegisterFailHandler(ginkgo.Fail)
15+
ginkgo.RunSpecs(t, "Datapath Suite")
16+
}
17+
18+
var _ = ginkgo.Describe("Datapath Tests", func() {
19+
RG := "siglin-143139088-westus2"
20+
BUILD_ID := "001"
21+
CLUSTER2 := "aks-2"
22+
KUBECONFIG2 := fmt.Sprintf("/tmp/%s.kubeconfig", CLUSTER2)
23+
24+
PN_NAME := fmt.Sprintf("pn-%s-c2", BUILD_ID)
25+
PNI_NAME := fmt.Sprintf("pni-%s-c2", BUILD_ID)
26+
27+
ginkgo.It("creates PodNetwork", func() {
28+
vnetName := "cx_vnet_b1"
29+
subnetName := "s1"
30+
31+
vnetGUID := helpers.GetVnetGUID(RG, vnetName)
32+
subnetGUID := helpers.GetSubnetGUID(RG, vnetName, subnetName)
33+
subnetARMID := helpers.GetSubnetARMID(RG, vnetName, subnetName)
34+
subnetToken := helpers.GetSubnetToken(RG, vnetName, subnetName)
35+
36+
err := CreatePodNetwork(KUBECONFIG2, PodNetworkData{
37+
PNName: PN_NAME,
38+
VnetGUID: vnetGUID,
39+
SubnetGUID: subnetGUID,
40+
SubnetARMID: subnetARMID,
41+
SubnetToken: subnetToken,
42+
}, "./templates/podnetwork.yaml.tmpl")
43+
Expect(err).To(BeNil())
44+
})
45+
46+
ginkgo.It("creates PodNetworkInstance", func() {
47+
err := CreatePodNetworkInstance(KUBECONFIG2, PNIData{
48+
PNIName: PNI_NAME,
49+
PNName: PN_NAME,
50+
Namespace: PN_NAME, // namespace same as PN
51+
Type: "explicit",
52+
Reservations: 2,
53+
}, "./templates/podnetworkinstance.yaml.tmpl")
54+
Expect(err).To(BeNil())
55+
})
56+
57+
ginkgo.It("creates pods on each node", func() {
58+
nodes := helpers.GetClusterNodes(KUBECONFIG2)
59+
Expect(len(nodes)).To(BeNumerically(">", 0))
60+
61+
for i, node := range nodes[:2] {
62+
podName := fmt.Sprintf("pod-c2-%d", i)
63+
err := CreatePod(KUBECONFIG2, PodData{
64+
PodName: podName,
65+
NodeName: node,
66+
OS: "linux",
67+
PNName: PN_NAME,
68+
PNIName: PNI_NAME,
69+
Image: "weibeld/ubuntu-networking",
70+
}, "./templates/pod.yaml.tmpl")
71+
Expect(err).To(BeNil())
72+
}
73+
})
74+
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package long_running_cluster
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
"text/template"
8+
)
9+
10+
func applyTemplate(templatePath string, data interface{}, kubeconfig string) error {
11+
tmpl, err := template.ParseFiles(templatePath)
12+
if err != nil {
13+
return err
14+
}
15+
16+
var buf bytes.Buffer
17+
if err := tmpl.Execute(&buf, data); err != nil {
18+
return err
19+
}
20+
21+
cmd := exec.Command("kubectl", "--kubeconfig", kubeconfig, "apply", "-f", "-")
22+
cmd.Stdin = &buf
23+
out, err := cmd.CombinedOutput()
24+
if err != nil {
25+
return fmt.Errorf("kubectl apply failed: %s\n%s", err, string(out))
26+
}
27+
28+
fmt.Println(string(out))
29+
return nil
30+
}
31+
32+
// -------------------------
33+
// PodNetwork
34+
// -------------------------
35+
type PodNetworkData struct {
36+
PNName string
37+
VnetGUID string
38+
SubnetGUID string
39+
SubnetARMID string
40+
SubnetToken string
41+
}
42+
43+
func CreatePodNetwork(kubeconfig string, data PodNetworkData, templatePath string) error {
44+
return applyTemplate(templatePath, data, kubeconfig)
45+
}
46+
47+
// -------------------------
48+
// PodNetworkInstance
49+
// -------------------------
50+
type PNIData struct {
51+
PNIName string
52+
PNName string
53+
Namespace string
54+
Type string
55+
Reservations int
56+
}
57+
58+
func CreatePodNetworkInstance(kubeconfig string, data PNIData, templatePath string) error {
59+
return applyTemplate(templatePath, data, kubeconfig)
60+
}
61+
62+
// -------------------------
63+
// Pod
64+
// -------------------------
65+
type PodData struct {
66+
PodName string
67+
NodeName string
68+
OS string
69+
PNName string
70+
PNIName string
71+
Image string
72+
}
73+
74+
func CreatePod(kubeconfig string, data PodData, templatePath string) error {
75+
return applyTemplate(templatePath, data, kubeconfig)
76+
}

0 commit comments

Comments
 (0)