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

controller: reject adding ifaces to host networked pods #89

Merged
merged 1 commit into from
Nov 21, 2022
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
15 changes: 15 additions & 0 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ func (pnc *PodNetworksController) handlePodUpdate(oldObj interface{}, newObj int
oldPod := oldObj.(*corev1.Pod)
newPod := newObj.(*corev1.Pod)

if newPod.Spec.HostNetwork {
klog.Warningf(
"rejecting to add interfaces for host networked pod: %s",
annotations.NamespacedName(newPod.GetNamespace(), newPod.GetName()),
)
pnc.Eventf(newPod, corev1.EventTypeWarning, "InterfaceAddRejected", rejectInterfaceAddEventFormat(newPod))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming we want to both throw an event an log ... Maybe that's too much.

Any thoughts @kmabda ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to throw an event here to give a feedback to the user

Copy link
Collaborator

@kmabda kmabda Nov 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing, since in https://github.com/k8snetworkplumbingwg/multus-dynamic-networks-controller/pull/90/files#diff-449e8d1bd46b9c978e208dde912a1d7a76d17ce553067f8d0a67a27f17338d26R152 we need to filter out hostnetwork pods as well but we can't tel before hand if an interface got added by the user or not until we process the request we could move this logic from handlePodUpdate to processNextWorkItem function.

Pro same treatment logic would be in one place i.e. we have a consistency w.r.t. filtering out unwanted pods and how we log/throw out an event to the user in case an interface got added for these pods in one place
Con a little more overhead by adding an item to the workqueue to processing it than throwing away

If you go ahead with these changes I can rebase my pull request once this one get merged.

return
}
if !didNetworkSelectionElementsChange(oldPod, newPod) {
return
}
Expand Down Expand Up @@ -413,6 +421,13 @@ func removeIfaceEventFormat(pod *corev1.Pod, network *nadv1.NetworkSelectionElem
)
}

func rejectInterfaceAddEventFormat(pod *corev1.Pod) string {
return fmt.Sprintf(
"pod [%s]: will not add interface to host networked pod",
annotations.NamespacedName(pod.GetNamespace(), pod.GetName()),
)
}

func interfaceAttributes(networkData nadv1.NetworkSelectionElement) *multusapi.DelegateInterfaceAttributes {
if len(networkData.IPRequest) > 0 || networkData.MacRequest != "" {
return &multusapi.DelegateInterfaceAttributes{
Expand Down
65 changes: 57 additions & 8 deletions pkg/controller/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
pod *corev1.Pod
networkToAdd string
stopChannel chan struct{}
nadClient nadclient.Interface
)

networkStatusNames := func(statuses []nad.NetworkStatus) []string {
Expand All @@ -90,15 +91,24 @@ var _ = Describe("Dynamic Attachment controller", func() {

BeforeEach(func() {
pod = podSpec(podName, namespace, networkName)
k8sClient = fake.NewSimpleClientset(pod)
networkToAdd = fmt.Sprintf("%s-2", networkName)
nadClient, err := newFakeNetAttachDefClient(

var err error
nadClient, err = newFakeNetAttachDefClient(
netAttachDef(networkName, namespace, dummyNetSpec(networkName, cniVersion)),
netAttachDef(networkToAdd, namespace, dummyNetSpec(networkToAdd, cniVersion)))
Expect(err).NotTo(HaveOccurred())
stopChannel = make(chan struct{})
const maxEvents = 5
eventRecorder = record.NewFakeRecorder(maxEvents)
})

AfterEach(func() {
close(stopChannel)
})

JustBeforeEach(func() {
k8sClient = fake.NewSimpleClientset(pod)
Expect(
newDummyPodController(
k8sClient,
Expand Down Expand Up @@ -127,12 +137,8 @@ var _ = Describe("Dynamic Attachment controller", func() {
)
})

AfterEach(func() {
close(stopChannel)
})

When("an attachment is added to the pod's network annotations", func() {
BeforeEach(func() {
JustBeforeEach(func() {
var err error
_, err = k8sClient.CoreV1().Pods(namespace).UpdateStatus(
context.TODO(),
Expand Down Expand Up @@ -169,7 +175,7 @@ var _ = Describe("Dynamic Attachment controller", func() {
})

When("an attachment is removed from the pod's network annotations", func() {
BeforeEach(func() {
JustBeforeEach(func() {
var err error
_, err = k8sClient.CoreV1().Pods(namespace).UpdateStatus(
context.TODO(),
Expand Down Expand Up @@ -202,6 +208,43 @@ var _ = Describe("Dynamic Attachment controller", func() {
}).Should(BeEmpty())
})
})

When("an attachment is added to a host networked pod", func() {
BeforeEach(func() {
pod = hostNetworkedPodSpec(podName, namespace, networkName)
})

JustAfterEach(func() {
_, err := k8sClient.CoreV1().Pods(namespace).UpdateStatus(
context.TODO(),
updatePodSpec(pod, networkName, networkToAdd),
metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())
})

It("the attachment request is ignored", func() {
Eventually(func() ([]nad.NetworkStatus, error) {
updatedPod, err := k8sClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return nil, err
}
status, err := annotations.PodDynamicNetworkStatus(updatedPod)
if err != nil {
return nil, err
}
return status, nil
}).Should(ConsistOf(
ifaceStatus(namespace, networkName, "net0", "")))
})

It("throws an event indicating the interface add operation is rejected", func() {
expectedEventPayload := fmt.Sprintf(
"Warning InterfaceAddRejected pod [%s]: will not add interface to host networked pod",
annotations.NamespacedName(namespace, podName),
)
Eventually(<-eventRecorder.Events).Should(Equal(expectedEventPayload))
})
})
})
})
})
Expand Down Expand Up @@ -444,3 +487,9 @@ func ifaceStatus(namespace, networkName, ifaceName, macAddress string) nad.Netwo
Mac: macAddress,
}
}

func hostNetworkedPodSpec(name string, namespace string, networks ...string) *corev1.Pod {
pod := podSpec(name, namespace, networks...)
pod.Spec.HostNetwork = true
return pod
}