Bootstrap FreeKB - OpenShift - Delete pods using REST API
OpenShift - Delete pods using REST API

Updated:   |  OpenShift articles

This assumes you have used the REST API to obtain an OAuth bearer token. Let's say the bearer token is sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U.

Here is how you would list the pods in the "foo" namespace using the REST API. The oc config view or oc get apiserver commands can be used to display the API Server URL (api.openshift.example.com in this example).

curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods"

 

If something like this is returned, this means that there are no pods in the "foo" namespace.

{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/foo/pods",
    "resourceVersion": "92373341"
  },
  "items": []
}

 

On the other hand, if the items list contains key value pairs, this means the namespace contains one or more pods.

{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/foo/pods",
    "resourceVersion": "92368763"
  },
  "items": [
    {
      "metadata": {
        "name": "helloworld-0.0.5-snapshot-master-3-2-b89cd49c8-rvg77",
        "generateName": "helloworld-0.0.5-snapshot-master-3-2-b89cd49c8-",
        "namespace": "foo",
        "selfLink": "/api/v1/namespaces/foo/pods/helloworld-0.0.5-snapshot-master-3-2-b89cd49c8-rvg77"

 

Or if you know the name of the pod, you can limit the output to a single pod.

curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods/example-pod"

 

If something like this is returned, this means that there are no pod named "example-pod" in the "foo" namespace.

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "pods \"example-pod\" not found",
  "reason": "NotFound",
  "details": {
    "name": "example-pod",
    "kind": "pods"
  },
  "code": 404
}

 

Then the DELETE method can be used to delete the pod.

curl
--insecure
--request DELETE
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods/example-pod"

 

You probably should then re-issue the GET request to verify 404 and NotFound are returned.

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5f7d84 in the box below so that we can be sure you are a human.