How should I retrieve the current kubernetes server version via Golang codes?

10/5/2021

For example, in terminal, we can do so via kubectl version.

I wish to obtain the kubernetes server version via Golang codes. I believe it should be covered by the client-go (k8s.io/client-go/kubernetes) and/or controller-runtime (sigs.k8s.io/controller-runtime) codes, but couldn't find any.

What is the most efficient way to retrieve the kubernetes server version?

-- jtee
kubernetes

1 Answer

3/8/2022

you can use this package:

"k8s.io/client-go/discovery"

call this function: discovery.NewDiscoveryClientForConfig(cfg) which returns DiscoveryClient object which you can use to call discClient.ServerVersion()

cfg in the above function is the kubeconfig of the cluster that you can provide, which you can get by calling the function: config.GetConfigOrDie() from this package

"sigs.k8s.io/controller-runtime/pkg/client/config"

-- Himanshu
Source: StackOverflow