How-To – Use kubernetes/openshift watch parameter in REST interface

Some api or oapi calls support the watch parameter

E.G: https://docs.openshift.com/enterprise/3.1/rest_api/openshift_v1.html#list-or-watch-objects-of-kind-route

list or watch objects of kind Route

GET /oapi/v1/namespaces/{namespace}/routes

Parameters

Type
Name
Description
Required
Schema
Default

QueryParameter

pretty

If ‘true’, then the output is pretty printed.

false

string

QueryParameter

labelSelector

A selector to restrict the list of returned objects by their labels. Defaults to everything.

false

string

QueryParameter

fieldSelector

A selector to restrict the list of returned objects by their fields. Defaults to everything.

false

string

QueryParameter

watch

Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.

false

boolean

QueryParameter

resourceVersion

When specified with a watch call, shows changes that occur after that particular version of a resource. Defaults to changes from the beginning of history.

false

string

Let’s check this route

$ oc get route helloworld-route
NAME               HOST/PORT                                           PATH      SERVICE      LABELS           INSECURE POLICY   TLS TERMINATION
helloworld-route   spring-boot-helloworld.plainjava.appad4.tsi-af.de             helloworld   app=helloworld                    

 

So as an user for this project/namespace with the necessary rights you’ll get the existing route objects by:

$ curl -k -H "Authorization: Bearer $(oc whoami -t)" -X GET "https://172.30.0.1/oapi/v1/namespaces/plainjava/routes/helloworld-route"
{
  "kind": "Route",
  "apiVersion": "v1",
  "metadata": {
    "name": "helloworld-route",
    "namespace": "plainjava",
    "selfLink": "/oapi/v1/namespaces/plainjava/routes/helloworld-route",
    "uid": "480cbb83-4e5c-11e6-885c-0050560461a7",
    "resourceVersion": "11567709",
    "creationTimestamp": "2016-07-20T09:28:13Z",
    "labels": {
      "app": "helloworld"
    }
  },
  "spec": {
    "host": "spring-boot-helloworld.plainjava.appad4.tsi-af.de",
    "to": {
      "kind": "Service",
      "name": "helloworld"
    },
    "port": {
      "targetPort": 8080
    }
  },
  "status": {}
}

 

Now, Setting a watch and modifying the host of the route and change it back again. Check the modification message for the host.:

{"type":"ADDED","object":{"kind":"Route","apiVersion":"v1","metadata":{"name":"helloworld-route","namespace":"plainjava","selfLink":"/oapi/v1/namespaces/plainjava/routes/helloworld-route","uid":"480cbb83-4e5c-11e6-885c-0050560461a7","resourceVersion":"11567709","creationTimestamp":"2016-07-20T09:28:13Z","labels":{"app":"helloworld"}},"spec":{"host":"spring-boot-helloworld.plainjava.appad4.tsi-af.de","to":{"kind":"Service","name":"helloworld"},"port":{"targetPort":8080}},"status":{}}}
{"type":"MODIFIED","object":{"kind":"Route","apiVersion":"v1","metadata":{"name":"helloworld-route","namespace":"plainjava","selfLink":"/oapi/v1/namespaces/plainjava/routes/helloworld-route","uid":"480cbb83-4e5c-11e6-885c-0050560461a7","resourceVersion":"14600520","creationTimestamp":"2016-07-20T09:28:13Z","labels":{"app":"helloworld"}},"spec":{"host":"spring-boot-helloworld-1.plainjava.appad4.tsi-af.de","to":{"kind":"Service","name":"helloworld"},"port":{"targetPort":8080}},"status":{}}}
{"type":"MODIFIED","object":{"kind":"Route","apiVersion":"v1","metadata":{"name":"helloworld-route","namespace":"plainjava","selfLink":"/oapi/v1/namespaces/plainjava/routes/helloworld-route","uid":"480cbb83-4e5c-11e6-885c-0050560461a7","resourceVersion":"14600675","creationTimestamp":"2016-07-20T09:28:13Z","labels":{"app":"helloworld"}},"spec":{"host":"spring-boot-helloworld.plainjava.appad4.tsi-af.de","to":{"kind":"Service","name":"helloworld"},"port":{"targetPort":8080}},"status":{}}}