Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Is it possible to do pod eviction? #114

Open
markvincze opened this issue Mar 17, 2019 · 6 comments
Open

Is it possible to do pod eviction? #114

markvincze opened this issue Mar 17, 2019 · 6 comments

Comments

@markvincze
Copy link

markvincze commented Mar 17, 2019

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?

@ericchiang
Copy link
Owner

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)

@markvincze
Copy link
Author

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!

@ericchiang
Copy link
Owner

ericchiang commented Mar 18, 2019 via email

@ericchiang ericchiang reopened this Mar 18, 2019
@markvincze
Copy link
Author

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?

@markvincze
Copy link
Author

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

@markvincze
Copy link
Author

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"
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants