/
sample_device_plugin.go
105 lines (91 loc) · 2.92 KB
/
sample_device_plugin.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
105
package sample_device_plugin
import (
"context"
"fmt"
"strings"
"time"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
DefaultDevicePath = "/dev/null"
)
type deviceRequest struct {
Name string
Amount int64
}
var _ = Describe("sample device plugin", func() {
Context("with pod requesting devices ", func() {
var testpod *corev1.Pod
AfterEach(func() {
err := Client.Delete(context.TODO(), testpod)
Expect(err).ToNot(HaveOccurred())
err = WaitForPodDeletion(testpod, 60*time.Second)
Expect(err).ToNot(HaveOccurred())
})
table.DescribeTable("should run with", func(devReqs []deviceRequest) {
var err error
testpod = GetTestPod()
testpod.Namespace = TestingNamespace.Name
if len(devReqs) > 0 {
for idx := 0; idx < len(testpod.Spec.Containers); idx++ {
testpod.Spec.Containers[idx].Resources.Requests = make(map[corev1.ResourceName]resource.Quantity)
testpod.Spec.Containers[idx].Resources.Limits = make(map[corev1.ResourceName]resource.Quantity)
for _, devReq := range devReqs {
testpod.Spec.Containers[idx].Resources.Requests[corev1.ResourceName(devReq.Name)] = *resource.NewQuantity(devReq.Amount, resource.DecimalSI)
testpod.Spec.Containers[idx].Resources.Limits[corev1.ResourceName(devReq.Name)] = *resource.NewQuantity(devReq.Amount, resource.DecimalSI)
}
}
}
err = Client.Create(context.TODO(), testpod)
Expect(err).ToNot(HaveOccurred())
err = WaitForPodCondition(testpod, corev1.PodReady, corev1.ConditionTrue, 10*time.Minute)
Expect(err).ToNot(HaveOccurred())
devicesMatchCount := 0
for _, devReq := range devReqs {
envVars, err := ExecCommandOnPod(testpod, []string{"env"})
Expect(err).ToNot(HaveOccurred())
for _, envVar := range strings.Split(string(envVars), "\n") {
env := strings.TrimSpace(envVar)
devName := strings.Map(func(r rune) rune {
if r == '.' || r == '/' {
return -1
}
return r
}, devReq.Name)
if len(env) == 0 || !strings.HasPrefix(env, devName) {
continue
}
Expect(env).To(HavePrefix(strings.ToUpper(devName)))
}
data, err := ExecCommandOnPod(testpod, []string{"/bin/sh", "-c", fmt.Sprintf("/bin/stat -c %%F %s", DefaultDevicePath)})
Expect(err).ToNot(HaveOccurred())
for _, devDesc := range strings.Split(string(data), "\n") {
line := strings.TrimSpace(devDesc)
if len(line) == 0 {
continue
}
Expect(devDesc).To(ContainSubstring("character special file"))
devicesMatchCount++
}
}
Expect(devicesMatchCount).To(Equal(len(devReqs)))
},
table.Entry("a single device A", []deviceRequest{
{
Name: "example.com/deviceA",
Amount: 1,
},
}),
table.Entry("a single device B", []deviceRequest{
{
Name: "example.com/deviceB",
Amount: 1,
},
}),
)
})
})