How to access the Kubernetes API in Go and run kubectl commands

9/14/2018

I want to access my Kubernetes cluster API in Go to run kubectl command to get available namespaces in my k8s cluster which is running on google cloud.

My sole purpose is to get namespaces available in my cluster by running kubectl command: kindly let me know if there is any alternative.

-- MSA
google-kubernetes-engine
kubectl
kubernetes

1 Answer

9/14/2018

You can start with kubernetes/client-go, the Go client for Kubernetes, made for talking to a kubernetes cluster. (not through kubectl though: directly through the Kubernetes API)

It includes a NamespaceLister, which helps list Namespaces.

See "Building stuff with the Kubernetes API — Using Go" from Vladimir Vivien

https://cdn-images-1.medium.com/max/1000/1*4noxYkVSvMPmlbt1kSOIHw.png

Michael Hausenblas (Developer Advocate at Red Hat) proposes in the comments documentations with using-client-go.cloudnative.sh

A versioned collection of snippets showing how to use client-go.

-- VonC
Source: StackOverflow