Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pods without volumes #140

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ func patchEmptyResources(patch []types.JsonPatchOperation, containerIndex uint,
}

func addVolDownwardAPI(patch []types.JsonPatchOperation, hugepageResourceList []hugepageResourceData, pod *corev1.Pod) []types.JsonPatchOperation {

if len(pod.Spec.Volumes) == 0 {
patch = append(patch, types.JsonPatchOperation{
Operation: "add",
Path: "/spec/volumes",
Value: []corev1.Volume{},
})
}

dAPIItems := []corev1.DownwardAPIVolumeFile{}

if pod.Labels != nil && len(pod.Labels) > 0 {
Expand Down Expand Up @@ -491,14 +500,21 @@ func addVolDownwardAPI(patch []types.JsonPatchOperation, hugepageResourceList []
return patch
}

func addVolumeMount(patch []types.JsonPatchOperation, containersLen int) []types.JsonPatchOperation {
func addVolumeMount(patch []types.JsonPatchOperation, containers []corev1.Container) []types.JsonPatchOperation {

vm := corev1.VolumeMount{
Name: "podnetinfo",
ReadOnly: true,
MountPath: types.DownwardAPIMountPath,
}
for containerIndex := 0; containerIndex < containersLen; containerIndex++ {
for containerIndex, container := range containers {
if len(container.VolumeMounts) == 0 {
patch = append(patch, types.JsonPatchOperation{
Operation: "add",
Path: "/spec/containers/" + strconv.Itoa(containerIndex) + "/volumeMounts",
Value: []corev1.VolumeMount{},
})
}
patch = append(patch, types.JsonPatchOperation{
Operation: "add",
Path: "/spec/containers/" + strconv.Itoa(containerIndex) + "/volumeMounts/-",
Expand All @@ -510,7 +526,7 @@ func addVolumeMount(patch []types.JsonPatchOperation, containersLen int) []types
}

func createVolPatch(patch []types.JsonPatchOperation, hugepageResourceList []hugepageResourceData, pod *corev1.Pod) []types.JsonPatchOperation {
patch = addVolumeMount(patch, len(pod.Spec.Containers))
patch = addVolumeMount(patch, pod.Spec.Containers)
patch = addVolDownwardAPI(patch, hugepageResourceList, pod)
return patch
}
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/resourcename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cniv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
)

var _ = Describe("Verify that resource and POD which consumes resource cannot be in different namespaces", func() {
Expand Down Expand Up @@ -154,4 +155,36 @@ var _ = Describe("Network injection testing", func() {
Expect(limNo.String()).Should(Equal("1"))
})
})

Context("one network request and automountServiceAccountToken=false", func() {
BeforeEach(func() {
nad = util.GetResourceSelectorOnly(testNetworkName, *testNs, testNetworkResName)
Expect(util.ApplyNetworkAttachmentDefinition(networkClient.K8sCniCncfIoV1Interface, nad, timeout)).Should(BeNil())

pod = util.GetOneNetwork(testNetworkName, *testNs, defaultPodName)
pod.Spec.AutomountServiceAccountToken = pointer.Bool(false)

Expect(util.CreateRunningPod(cs.CoreV1Interface, pod, timeout, interval)).Should(BeNil())
Expect(pod.Name).ShouldNot(BeNil())
pod, err = util.UpdatePodInfo(cs.CoreV1Interface, pod, timeout)
Expect(err).Should(BeNil())
})

AfterEach(func() {
_ = util.DeletePod(cs.CoreV1Interface, pod, timeout)
Expect(util.DeleteNetworkAttachmentDefinition(networkClient.K8sCniCncfIoV1Interface, testNetworkName, nad, timeout)).Should(BeNil())
})

It("should have one limit injected", func() {
limNo, ok := pod.Spec.Containers[0].Resources.Limits[testNetworkResName]
Expect(ok).Should(BeTrue())
Expect(limNo.String()).Should(Equal("1"))
})

It("should have one request injected", func() {
limNo, ok := pod.Spec.Containers[0].Resources.Requests[testNetworkResName]
Expect(ok).Should(BeTrue())
Expect(limNo.String()).Should(Equal("1"))
})
})
})