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

Delete pods to acclerate pods rescheduling #12

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions pkg/resources/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package resources

import (
"context"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// DeletePods deletes all the pods from the unhealthy node
Copy link
Member

Choose a reason for hiding this comment

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

Nit: in that context this method just deletes all the pods from the node (i.e we don't know whether the node is unhealthy or not)

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, I will fix it.

func DeletePods(ctx context.Context, r client.Client, nodeName string) error {
log := ctrl.Log.WithName("commons-resource")
zero := int64(0)
backgroundDeletePolicy := metav1.DeletePropagationBackground

deleteOptions := &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": nodeName}),
Namespace: "",
Limit: 0,
},
DeleteOptions: client.DeleteOptions{
GracePeriodSeconds: &zero,
PropagationPolicy: &backgroundDeletePolicy,
},
}

namespaces := corev1.NamespaceList{}
if err := r.List(ctx, &namespaces); err != nil {
log.Error(err, "failed to list namespaces")
return err
}

log.Info("starting to delete pods", "node name", nodeName)

pod := &corev1.Pod{}
for _, ns := range namespaces.Items {
deleteOptions.Namespace = ns.Name
err := r.DeleteAllOf(ctx, pod, deleteOptions)
if err != nil {
log.Error(err, "failed to delete pods of unhealthy node", "namespace", ns.Name)
return err
}
}

log.Info("done deleting pods", "node name", nodeName)

return nil
}