Skip to content

Commit 6a9427a

Browse files
committed
Add webhook test for hugepages mount and downward api
Signed-off-by: Sebastian Sch <[email protected]>
1 parent a131302 commit 6a9427a

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

test/conformance/tests/test_sriov_operator.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,93 @@ var _ = Describe("[sriov] operator", Ordered, func() {
465465
FieldPath: "metadata.annotations",
466466
},
467467
})))
468+
469+
By("checking the label is present in the pod")
470+
stdout, stderr, err := pod.ExecCommand(clients, runningPod, "/bin/bash", "-c", "cat /etc/podnetinfo/labels")
471+
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("stdout: %s, stderr: %s", stdout, stderr))
472+
Expect(stdout).To(ContainSubstring("anyname=\"anyvalue\""))
473+
})
474+
475+
It("should inject also hugepages if requested in the pod", func() {
476+
var hugepagesName string
477+
var hupagesAmount int64
478+
479+
hasHugepages := false
480+
nodeObj := &corev1.Node{}
481+
Eventually(func() error {
482+
return clients.Get(context.Background(), runtimeclient.ObjectKey{Name: node}, nodeObj)
483+
}, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
484+
485+
for resourceName, resource := range nodeObj.Status.Allocatable {
486+
if strings.HasPrefix(string(resourceName), "hugepages") && resource.Value() > 0 {
487+
hasHugepages = true
488+
hugepagesName = string(resourceName)
489+
hupagesAmount = resource.Value()
490+
break
491+
}
492+
}
493+
if !hasHugepages {
494+
Skip("No hugepages found on the node")
495+
}
496+
497+
sriovNetwork := &sriovv1.SriovNetwork{
498+
ObjectMeta: metav1.ObjectMeta{
499+
Name: "test-apivolnetwork",
500+
Namespace: operatorNamespace,
501+
},
502+
Spec: sriovv1.SriovNetworkSpec{
503+
ResourceName: resourceName,
504+
IPAM: `{"type":"host-local","subnet":"10.10.10.0/24","rangeStart":"10.10.10.171","rangeEnd":"10.10.10.181"}`,
505+
NetworkNamespace: namespaces.Test,
506+
}}
507+
err := clients.Create(context.Background(), sriovNetwork)
508+
Expect(err).ToNot(HaveOccurred())
509+
510+
waitForNetAttachDef("test-apivolnetwork", namespaces.Test)
511+
512+
podDefinition := pod.RedefineWithHugepages(pod.DefineWithNetworks([]string{sriovNetwork.Name}), hugepagesName, hupagesAmount)
513+
created, err := clients.Pods(namespaces.Test).Create(context.Background(), podDefinition, metav1.CreateOptions{})
514+
Expect(err).ToNot(HaveOccurred())
515+
516+
runningPod := waitForPodRunning(created)
517+
518+
var downwardVolume *corev1.Volume
519+
for _, v := range runningPod.Spec.Volumes {
520+
if v.Name == volumePodNetInfo {
521+
downwardVolume = v.DeepCopy()
522+
break
523+
}
524+
}
525+
526+
// In the DownwardAPI the resource injector rename the hugepage size with underscores
527+
// example hugepages-1Gi -> hugepages_1Gi
528+
result := strings.Replace(hugepagesName, "-", "_", 1)
529+
if len(result) > 0 {
530+
result = result[:len(result)-1]
531+
}
532+
533+
Expect(downwardVolume).ToNot(BeNil(), "Downward volume not found")
534+
Expect(downwardVolume.DownwardAPI).ToNot(BeNil(), "Downward api not found in volume")
535+
Expect(downwardVolume.DownwardAPI.Items).To(SatisfyAll(
536+
ContainElement(MatchFields(IgnoreExtras, Fields{
537+
"Path": Equal(fmt.Sprintf("%s_request_test", result)),
538+
"ResourceFieldRef": PointTo(MatchFields(IgnoreExtras, Fields{
539+
"ContainerName": Equal("test"),
540+
"Resource": Equal(fmt.Sprintf("requests.%s", hugepagesName)),
541+
})),
542+
})), ContainElement(MatchFields(IgnoreExtras, Fields{
543+
"Path": Equal(fmt.Sprintf("%s_limit_test", result)),
544+
"ResourceFieldRef": PointTo(MatchFields(IgnoreExtras, Fields{
545+
"ContainerName": Equal("test"),
546+
"Resource": Equal(fmt.Sprintf("limits.%s", hugepagesName)),
547+
})),
548+
})), ContainElement(MatchFields(IgnoreExtras, Fields{
549+
"Path": Equal("annotations"),
550+
"FieldRef": PointTo(MatchFields(IgnoreExtras, Fields{
551+
"APIVersion": Equal("v1"),
552+
"FieldPath": Equal("metadata.annotations"),
553+
})),
554+
}))))
468555
})
469556
})
470557

test/util/pod/pod.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"time"
99

1010
corev1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/api/resource"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/client-go/kubernetes/scheme"
1314
"k8s.io/client-go/tools/remotecommand"
14-
"k8s.io/utils/pointer"
15+
"k8s.io/utils/ptr"
1516

1617
testclient "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/client"
1718
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/images"
@@ -26,7 +27,7 @@ func GetDefinition() *corev1.Pod {
2627
GenerateName: "testpod-",
2728
Namespace: namespaces.Test},
2829
Spec: corev1.PodSpec{
29-
TerminationGracePeriodSeconds: pointer.Int64Ptr(0),
30+
TerminationGracePeriodSeconds: ptr.To[int64](0),
3031
Containers: []corev1.Container{{Name: "test",
3132
Image: images.Test(),
3233
SecurityContext: &corev1.SecurityContext{
@@ -103,6 +104,31 @@ func RedefineWithCapabilities(pod *corev1.Pod, capabilitiesList []corev1.Capabil
103104
return pod
104105
}
105106

107+
func RedefineWithHugepages(pod *corev1.Pod, hugepagesName string, hugepagesAmount int64) *corev1.Pod {
108+
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
109+
Name: "hugepages",
110+
VolumeSource: corev1.VolumeSource{
111+
EmptyDir: &corev1.EmptyDirVolumeSource{
112+
Medium: corev1.StorageMediumHugePages,
113+
},
114+
},
115+
})
116+
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
117+
Name: "hugepages",
118+
MountPath: "/hugepages",
119+
})
120+
121+
resources := corev1.ResourceList{
122+
corev1.ResourceName(hugepagesName): *resource.NewQuantity(hugepagesAmount, resource.BinarySI),
123+
corev1.ResourceCPU: *resource.NewMilliQuantity(50, resource.DecimalSI),
124+
}
125+
126+
pod.Spec.Containers[0].Resources.Requests = resources
127+
pod.Spec.Containers[0].Resources.Limits = resources
128+
129+
return pod
130+
}
131+
106132
// ExecCommand runs command in the pod and returns buffer output
107133
func ExecCommand(cs *testclient.ClientSet, pod *corev1.Pod, command ...string) (string, string, error) {
108134
var buf, errbuf bytes.Buffer

0 commit comments

Comments
 (0)