Get cluster id with Kubernetes client-go

2/14/2019

I'm using client-go for Kubernetes and trying to get the id of the current cluster, i.e. something similar to the output of kubectl cluster-info. I found a function called getCluster:

func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
    // check that getAuthInfo, getContext, and getCluster do not return an error.
    // Do this before checking if the current config is usable in the event that an
    // AuthInfo, Context, or Cluster config with user-defined names are not found.
    // This provides a user with the immediate cause for error if one is found
    configAuthInfo, err := config.getAuthInfo()
    if err != nil {
        return nil, err
    }

    _, err = config.getContext()
    if err != nil {
        return nil, err
    }

    configClusterInfo, err := config.getCluster()
    if err != nil {
        return nil, err
    }

    ...
}

When I write the following in my code

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
clusterInfo, err := config.getCluster()

I get the error config.getCluster undefined (type *rest.Config has no field or method getCluster)

How can I use this function? Is there any other way to get the cluster id?

-- lawful_neutral
client-go
kubernetes

2 Answers

2/22/2019
$ kubectl cluster-info

1. Kubernetes master is running at https://10.156.0.3:6443
2. KubeDNS is running at https://10.156.0.3:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

If you run kubectl cluster-info -v8 you can see the following:

The first line is basicaly taken from your ~/.kube/config file and kubectl just checked if it works, with the simple GET request, searching for something, that should be definitely present in the cluster:

I0222 11:21:18.015482   18150 round_trippers.go:416] GET https://10.156.0.3:6443/api/v1/namespaces/kube-system/services?labelSelector=kubernetes.io%2Fcluster-service%3Dtrue

You can get the result of this command by starting kubectl proxy and then, in other console:

curl http://127.0.0.1:8001/api/v1/namespaces/kube-system/services?labelSelector=kubernetes.io%2Fcluster-service%3Dtrue 

As you can see in the response, there are no lines with master URL there.

So, to get the value specified in the first line of the output of kubectl cluster-info you just need to read correct part of the Kubernetes config file, because you can have several cluster configuration there.

To read and deserialize Kubernetes config file there is a function in the loader.go:

// LoadFromFile takes a filename and deserializes the contents into Config object
func LoadFromFile(filename string) (*clientcmdapi.Config, error)

or another function in the config.go:

// getConfigFromFile tries to read a kubeconfig file and if it can't, returns an error.  One exception, missing files result in empty configs, not an error.
func getConfigFromFile(filename string) (*clientcmdapi.Config, error) 
-- VAS
Source: StackOverflow

2/17/2019

From the link you provided it seems like you need to use clusterInfo, err := config.getCluster() instead of configAuthInfo.getCluster()

-- Kseniia Churiumova
Source: StackOverflow