forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod.go
104 lines (86 loc) · 3.25 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package e2e_test
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
)
// Returns the name of a pod that's a member of the 'debug' daemonset, running on an aks-nodepool node.
func getDebugPodName(kube *kubeclient) (string, error) {
podList := corev1.PodList{}
if err := kube.dynamic.List(context.Background(), &podList, client.MatchingLabels{"app": "debug"}); err != nil {
return "", fmt.Errorf("failed to list debug pod: %w", err)
}
if len(podList.Items) < 1 {
return "", fmt.Errorf("failed to find debug pod, list by selector returned no results")
}
podName := podList.Items[0].Name
return podName, nil
}
func getPodIP(ctx context.Context, kube *kubeclient, namespaceName, podName string) (string, error) {
pod, err := kube.typed.CoreV1().Pods(namespaceName).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("unable to get pod %s/%s: %w", namespaceName, podName, err)
}
return pod.Status.PodIP, nil
}
func ensureDebugDaemonset(ctx context.Context, kube *kubeclient) error {
manifest := getDebugDaemonset()
var ds appsv1.DaemonSet
if err := yaml.Unmarshal([]byte(manifest), &ds); err != nil {
return fmt.Errorf("failed to unmarshal debug daemonset manifest: %w", err)
}
desired := ds.DeepCopy()
_, err := controllerutil.CreateOrUpdate(ctx, kube.dynamic, &ds, func() error {
ds = *desired
return nil
})
if err != nil {
return fmt.Errorf("failed to apply debug daemonset: %w", err)
}
return nil
}
func ensureTestNginxPod(ctx context.Context, kube *kubeclient, nodeName string) (string, error) {
nginxPodName := fmt.Sprintf("%s-nginx", nodeName)
nginxPodManifest := getNginxPodTemplate(nodeName)
if err := ensurePod(ctx, kube, nginxPodName, nginxPodManifest); err != nil {
return "", fmt.Errorf("failed to ensure test nginx pod %q: %w", nginxPodName, err)
}
return nginxPodName, nil
}
func ensureWasmPods(ctx context.Context, kube *kubeclient, nodeName string) (string, error) {
spinPodName := fmt.Sprintf("%s-wasm-spin", nodeName)
spinPodManifest := getWasmSpinPodTemplate(nodeName)
if err := ensurePod(ctx, kube, spinPodName, spinPodManifest); err != nil {
return "", fmt.Errorf("failed to ensure wasm spin pod %q: %w", spinPodName, err)
}
return spinPodName, nil
}
func applyPodManifest(ctx context.Context, kube *kubeclient, manifest string) error {
var podObj corev1.Pod
if err := yaml.Unmarshal([]byte(manifest), &podObj); err != nil {
return fmt.Errorf("failed to unmarshal Pod manifest: %w", err)
}
desired := podObj.DeepCopy()
_, err := controllerutil.CreateOrUpdate(ctx, kube.dynamic, &podObj, func() error {
podObj = *desired
return nil
})
if err != nil {
return fmt.Errorf("failed to apply Pod manifest: %w", err)
}
return nil
}
func ensurePod(ctx context.Context, kube *kubeclient, podName, manifest string) error {
if err := applyPodManifest(ctx, kube, manifest); err != nil {
return fmt.Errorf("failed to ensure pod: %w", err)
}
if err := waitUntilPodRunning(ctx, kube, podName); err != nil {
return fmt.Errorf("failed to wait for pod to be in running state: %w", err)
}
return nil
}