From 84d39f7e3653347aee3c9e0d73bf89546e5bbdd3 Mon Sep 17 00:00:00 2001 From: diguage Date: Tue, 24 Sep 2019 20:32:13 +0800 Subject: [PATCH] update client-go to the latest version in order to keep in sync with client-go example --- .../administer-cluster/access-cluster-api.md | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/content/en/docs/tasks/administer-cluster/access-cluster-api.md b/content/en/docs/tasks/administer-cluster/access-cluster-api.md index 178780b951df1..c41547880e193 100644 --- a/content/en/docs/tasks/administer-cluster/access-cluster-api.md +++ b/content/en/docs/tasks/administer-cluster/access-cluster-api.md @@ -161,28 +161,38 @@ Kubernetes officially supports client libraries for [Go](#go-client) and #### Go client -* To get the library, run the following command: `go get k8s.io/client-go//kubernetes` See [https://github.com/kubernetes/client-go](https://github.com/kubernetes/client-go) to see which versions are supported. -* Write an application atop of the client-go clients. Note that client-go defines its own API objects, so if needed, please import API definitions from client-go rather than from the main repository, e.g., `import "k8s.io/client-go/1.4/pkg/api/v1"` is correct. +* To get the library, run the following command: `go get k8s.io/client-go@kubernetes-` See [https://github.com/kubernetes/client-go](https://github.com/kubernetes/client-go) to see which versions are supported. +* Write an application atop of the client-go clients. Note that client-go defines its own API objects, so if needed, import API definitions from client-go rather than from the main repository. For example, `import "k8s.io/client-go/kubernetes"` is correct. + +{{< note >}} + +According to the [README](https://github.com/kubernetes/client-go/blob/master/README.md), the client-go responsitory is still a mirror of [https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/client-go](https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/client-go), the code development is in the staging area. Since Kubernetes 1.8 release, the client-go library syncs the Kubernetes version tags to client-go when syncing the code from the staging area, prefixed with "kubernetes-". For example, if you check out the kubernetes-1.15.3 tag in client-go, the code you get is exactly the same as you check out the v1.15.3 tag in Kubernetes, and change directory to `staging/src/k8s.io/client-go`. + +The `` can also be `HEAD`, and the `HEAD` follows the upstream Kubernetes repo. + +{{< /note >}} The Go client can use the same [kubeconfig file](/docs/concepts/cluster-administration/authenticate-across-clusters-kubeconfig/) as the kubectl CLI does to locate and authenticate to the API server. See this [example](https://git.k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go): ```golang import ( - "fmt" - "k8s.io/client-go/1.4/kubernetes" - "k8s.io/client-go/1.4/pkg/api/v1" - "k8s.io/client-go/1.4/tools/clientcmd" + "fmt" + "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" ) -... - // uses the current context in kubeconfig - config, _ := clientcmd.BuildConfigFromFlags("", "path to kubeconfig") - // creates the clientset - clientset, _:= kubernetes.NewForConfig(config) - // access the API to list pods - pods, _:= clientset.CoreV1().Pods("").List(v1.ListOptions{}) - fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) -... + +func main() { + // uses the current context in kubeconfig + // path to kubeconfig -- e.g., /root/.kube/config + config, _ := clientcmd.BuildConfigFromFlags("", "") + // creates the clientset + clientset, _ := kubernetes.NewForConfig(config) + // access the API to list pods + pods, _ := clientset.CoreV1().Pods("").List(v1.ListOptions{}) + fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) +} ``` If the application is deployed as a Pod in the cluster, please refer to the [next section](#accessing-the-api-from-a-pod).