k8s
k8s copied to clipboard
Is it possible to do pod eviction?
This is not an issue report, but a question.
I'm implementing a piece of code which retrieves the list of nodes and list of pods, and then based on some conditions I'd like to evict some pods. Listing the nodes and pods and getting their details works very nicely, but I couldn't find a way to do pod eviction yet.
Is it possible to do pod eviction with this library? I couldn't find a dedicated function for it, but in the K8s docs I see there is a REST API endpoint, which needs POSTing a small Json input to the route /api/v1/namespaces/default/pods/{podname}/eviction. Is there maybe a way with the library to send an arbitrary REST request to the K8s API, but reuse the HTTP client with the base address and the necessary headers, which is maintained by the client?
You want https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#create-eviction-pod-v1-core
func evictPod(ctx context.Context, c *k8s.Client, namespace, name string) error {
var pod corev1.Pod
if err := c.Get(ctx, namespace, name, &pod); err != nil {
return fmt.Errorf("get pod %s/%s: %v", namespace, name, err)
}
if err := c.Create(ctx, &pod, k8s.Subresource("eviction")); err != nil {
return fmt.Errorf("creating pod eviction: %v", err)
}
// TODO: wait for pod to actually be evicted
return nil
}
(haven't tested that code)
Hi @ericchiang,
Thanks for the reply!
I tested this code, but so far I couldn't get it to work. The issue is that the Create call will try sending the POST to the URL
/api/v1/namespaces/{namespace}/pods/eviction
But it should go to
/api/v1/namespaces/{namespace}/pods/{name}/eviction
I think this is caused by the fact that Create internally passes false to the resourceURL function in the withName parameter.
I thought about using Update instead, but that sends a PUT, so that's also not good.
Do you know a way I could get around this?
Thanks!
Ah that's a bug then :)
The URL should contain the "{name}" in the path
On Mon, Mar 18, 2019, 2:18 PM Mark Vincze [email protected] wrote:
Hi @ericchiang https://github.com/ericchiang,
Thanks for the reply! I tested this code, but so far I couldn't get it to work. The issue is that the Create call will try sending the POST to the URL
/api/v1/namespaces/{namespace}/pods/eviction
But it should go to
/api/v1/namespaces/{namespace}/pods/{name}/eviction
I think this is caused by the fact that Create internally passes false to the resourceURL function in the withName parameter. I thought about using Update instead, but that sends a PUT, so that's also not good.
Do you know a way I could get around this?
Thanks!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ericchiang/k8s/issues/114#issuecomment-474105178, or mute the thread https://github.com/notifications/unsubscribe-auth/ACO_XUQRO9BmtAZq9t5Ym7QuNKM0WxOHks5vYAKxgaJpZM4b4qeJ .
I guess what happens is normally for Create it's the correct thing to omit the {name}, for example if we create a pod, we want to POST to
/api/v1/namespaces/{namespace}/pods
But in this case we need the name because we're creating a subresource.
So maybe the code here should be changed so that if there is a subresource among the ...options argument, then we pass in true instead of false as withName?
Btw I tried just manually passing in true as a test, but I'm getting a 400 with this response:
Failure 400 Eviction in version "v1beta1" cannot be handled as a Eviction: proto: wrong wireType = 2 for field GracePeriodSeconds
So it seems that this endpoint needs the eviction to be sent in the POST body, which has to look like this:
{
"apiVersion": "policy/v1beta1",
"kind": "Eviction",
"metadata": {
"name": "pod_name",
"namespace": "namespace"
}
}