How to get Kubernetes cluster name from K8s API mentions that
curl http://metadata/computeMetadata/v1/instance/attributes/cluster-name -H "Metadata-Flavor: Google"
(from within the cluster), or
kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name
(from outside the cluster), can be used to retrieve the cluster name. That works.
Is there a way to perform the same programmatically using the k8s client-go
library? Maybe using the RESTClient()? I've tried but kept getting the server could not find the requested resource
.
UPDATE
What I'm trying to do is to get the cluster-name
from an app that runs either in a local computer or within a k8s cluster. the k8s client-go
allows to initialise the clientset
via in cluster or out of cluster authentication.
With the two commands mentioned at the top that is achievable. I was wondering if there was a way from the client-go
library to achieve the same, instead of having to do kubectl
or curl
depending on where the service is run from.