How to get namespace inside container in OpenShift?

I would like to access the OpenShift and Kubernetes API from within the module to query and modify objects in the application to which the module belongs.

In the documentation ( https://docs.openshift.org/latest/dev_guide/service_accounts.html ) I found this description on how to access the api:

$ TOKEN="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" $ curl --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \ "https://openshift.default.svc.cluster.local/oapi/v1/users/~" \ -H "Authorization: Bearer $TOKEN" 

The problem is that when I, for example, want to access the module, I need to know the namespace in which I am located:

https://openshift.default.svc.cluster.local/oapi/v1/namespaces/${namespace}/pods

The only way I've found so far is to represent the namespace as an environment variable, but I would not want the user to enter this information.

+5
source share
3 answers

You can get the namespace of your package, which will be automatically populated as an environment variable using the top-down API .

+8
source

At least in the 1.5.3 kubernetes, I also see the namespace in /var/run/secrets/kubernetes.io/serviceaccount/namespace .

+9
source

I found a solution, keeping track of what the web console is doing.

I am allowed to request a list of projects without installation at the administrator cluster level at the following URL:

 https://openshift.default.svc.cluster.local/oapi/v1/projects 

Only projects that have rights are listed, and then you can define the current project, whose name is also a namespace.

It would be interesting if there was a simpler solution, but it works.

0
source

All Articles