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 draining DaemonSet pods using sriov devices #840

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion controllers/drain_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func expectNodeStateAnnotation(nodeState *sriovnetworkv1.SriovNetworkNodeState,
g.Expect(utils.ObjectHasAnnotation(nodeState, constants.NodeStateDrainAnnotationCurrent, expectedAnnotationValue)).
To(BeTrue(),
"Node[%s] annotation[%s] == '%s'. Expected '%s'", nodeState.Name, constants.NodeDrainAnnotation, nodeState.GetLabels()[constants.NodeStateDrainAnnotationCurrent], expectedAnnotationValue)
}, "20s", "1s").Should(Succeed())
}, "200s", "1s").Should(Succeed())
}

func expectNumberOfDrainingNodes(numbOfDrain int, nodesState ...*sriovnetworkv1.SriovNetworkNodeState) {
Expand Down
106 changes: 83 additions & 23 deletions pkg/drain/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/kubectl/pkg/drain"
Expand Down Expand Up @@ -68,29 +69,35 @@ func (d *Drainer) DrainNode(ctx context.Context, node *corev1.Node, fullNodeDrai
return false, nil
}

drainHelper := createDrainHelper(d.kubeClient, ctx, fullNodeDrain)
backoff := wait.Backoff{
Steps: 5,
Duration: 10 * time.Second,
Duration: 5 * time.Second,
Factor: 2,
}
var lastErr error

reqLogger.Info("drainNode(): Start draining")
if err = wait.ExponentialBackoff(backoff, func() (bool, error) {
if err = wait.ExponentialBackoffWithContext(ctx, backoff, func(ctx context.Context) (bool, error) {
drainHelper := createDrainHelper(d.kubeClient, ctx, fullNodeDrain)
err := drain.RunCordonOrUncordon(drainHelper, node, true)
if err != nil {
lastErr = err
reqLogger.Info("drainNode(): Cordon failed, retrying", "error", err)
return false, nil
}
err = drain.RunNodeDrain(drainHelper, node.Name)
if err == nil {
return true, nil
if err != nil {
lastErr = err
reqLogger.Info("drainNode(): Draining failed, retrying", "error", err)
return false, nil
}
lastErr = err
reqLogger.Info("drainNode(): Draining failed, retrying", "error", err)
return false, nil

err = d.removeDaemonSetsFromNode(ctx, node.Name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not entirely clear on how this works. I understand the process of selecting and removing DS pods, but I'm unsure how we prevent the DS controller from restarting pods on the node we intend to drain. Could you clarify?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So the idea is that if the daemon use vfs the new pod will stuck on pending so after we finish the configuration the daemon will be able to start

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the DS-managed Pod will be in Pending state?
Is it because of the cordon/taint? I think that the fact that the node is cordoned will not prevent DS pods to restart on it.
Or Is it because SRIOV resources are not exposed on the node? If so, Are we sure that SRIOV resources will not be exposed on the node at the moment when we try to kill the DS-managed pod? How we ensure this?

I don’t have a complete picture of how the operator’s drain logic works, so I might be missing something obvious—but I think it’s worth asking :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

in case of reconfiguration ds pod may get back to running state if there are still sriov resources on the worker IMO.

indeed cordon does not affect daemonset pods (tested in kind cluster).
adding a custom taint with effect NoSchedule will prevent DS pod from getting rescheduled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding a commit from the community meeting.

instead if using taint that can be harmful for the cluster (if some daemonset get deleted they will stuck until the sriov operator finish the work)

we agree to implement in the drain controller a removal of the device plugin label from the node + the removal of the running device plugin pod

@adrianchiris @ykulazhenkov @zeeke let me know what do you think before I start coding it please :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 from me

Copy link
Collaborator

Choose a reason for hiding this comment

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

we roll more responsibilites to the drain controller.

maybe it should be the role of config daemon to ensure device plugin no longer runs on node before requesting drain ?
then put node label back after config completed.

just a different apporach to consider. im not against the current one.

if err != nil {
lastErr = err
return false, nil
}

return true, nil
}); err != nil {
if wait.Interrupted(err) {
reqLogger.Info("drainNode(): failed to drain node", "steps", backoff.Steps, "error", lastErr)
Expand Down Expand Up @@ -131,6 +138,28 @@ func (d *Drainer) CompleteDrainNode(ctx context.Context, node *corev1.Node) (boo
return completed, nil
}

// removeDaemonSetsFromNode go over all the remain pods and search for DaemonSets that have SR-IOV devices to remove them
// we can't use the drain from core kubernetes as it doesn't support removing pods that are part of a DaemonSets
func (d *Drainer) removeDaemonSetsFromNode(ctx context.Context, nodeName string) error {
reqLogger := log.FromContext(ctx)
reqLogger.Info("drainNode(): remove DaemonSets using sriov devices from node", "nodeName", nodeName)

podList, err := d.kubeClient.CoreV1().Pods("").List(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("spec.nodeName=%s", nodeName)})
if err != nil {
reqLogger.Info("drainNode(): Failed to list pods, retrying", "error", err)
return err
}

// remove pods that are owned by a DaemonSet and use SR-IOV devices
dsPodsList := getDsPodsToRemove(podList)
drainHelper := createDrainHelper(d.kubeClient, ctx, true)
err = drainHelper.DeleteOrEvictPods(dsPodsList)
if err != nil {
reqLogger.Error(err, "failed to delete or evict pods from node", "nodeName", nodeName)
}
return err
}

// createDrainHelper function to create a drain helper
// if fullDrain is false we only remove pods that have the resourcePrefix
// if not we remove all the pods in the node
Expand All @@ -150,25 +179,21 @@ func createDrainHelper(kubeClient kubernetes.Interface, ctx context.Context, ful
}
log.Log.Info(fmt.Sprintf("%s pod from Node %s/%s", verbStr, pod.Namespace, pod.Name))
},
Ctx: ctx,
Out: writer{logger.Info},
ErrOut: writer{func(msg string, kv ...interface{}) { logger.Error(nil, msg, kv...) }},
Ctx: ctx,
Out: writer{logger.Info},
ErrOut: writer{func(msg string, kv ...interface{}) {
logger.Error(nil, msg, kv...)
}},
}

// when we just want to drain and not reboot we can only remove the pods using sriov devices
if !fullDrain {
deleteFunction := func(p corev1.Pod) drain.PodDeleteStatus {
for _, c := range p.Spec.Containers {
if c.Resources.Requests != nil {
for r := range c.Resources.Requests {
if strings.HasPrefix(r.String(), vars.ResourcePrefix) {
return drain.PodDeleteStatus{
Delete: true,
Reason: "pod contain SR-IOV device",
Message: "SR-IOV network operator draining the node",
}
}
}
if podHasSRIOVDevice(&p) {
return drain.PodDeleteStatus{
Delete: true,
Reason: "pod contains SR-IOV device",
Message: "SR-IOV network operator draining the node",
}
}
return drain.PodDeleteStatus{Delete: false}
Expand All @@ -179,3 +204,38 @@ func createDrainHelper(kubeClient kubernetes.Interface, ctx context.Context, ful

return drainer
}

func podHasSRIOVDevice(p *corev1.Pod) bool {
for _, c := range p.Spec.Containers {
if c.Resources.Requests != nil {
for r := range c.Resources.Requests {
if strings.HasPrefix(r.String(), vars.ResourcePrefix) {
return true
}
}
}
}

return false
}

func podsHasDSOwner(p *corev1.Pod) bool {
for _, o := range p.OwnerReferences {
if o.Kind == "DaemonSet" {
return true
}
}

return false
}

func getDsPodsToRemove(pl *corev1.PodList) []corev1.Pod {
podsToRemove := []corev1.Pod{}
for _, pod := range pl.Items {
if podsHasDSOwner(&pod) && podHasSRIOVDevice(&pod) {
podsToRemove = append(podsToRemove, pod)
}
}

return podsToRemove
}
1 change: 1 addition & 0 deletions test/conformance/tests/test_policy_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ var _ = Describe("[sriov] operator", Ordered, func() {
createTestPod(nodeToTest, []string{sriovNetworkName})
})
})

Context("PF shutdown", func() {
// 29398
It("Should be able to create pods successfully if PF is down.Pods are able to communicate with each other on the same node", func() {
Expand Down
Loading
Loading