From 1ba01e4a2272ed82f7eb8311f4f8635112be91e1 Mon Sep 17 00:00:00 2001 From: Chao Xu Date: Tue, 30 Aug 2016 15:00:27 -0700 Subject: [PATCH] add client-go --- docs/user-guide/accessing-the-cluster.md | 37 +++++++++++++++++++----- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/user-guide/accessing-the-cluster.md b/docs/user-guide/accessing-the-cluster.md index cf15072373..8997b354bf 100644 --- a/docs/user-guide/accessing-the-cluster.md +++ b/docs/user-guide/accessing-the-cluster.md @@ -100,13 +100,36 @@ with future high-availability support. ### Programmatic access to the API -There are [client libraries](https://github.com/kubernetes/kubernetes/tree/{{page.githubbranch}}/docs/devel/client-libraries.md) for accessing the API -from several languages. The Kubernetes project-supported -[Go](http://releases.k8s.io/{{page.githubbranch}}/pkg/client/) -client library can use the same [kubeconfig file](/docs/user-guide/kubeconfig-file) -as the kubectl CLI does to locate and authenticate to the apiserver. +The Kubernetes project-supported Go client library is at [https://github.com/kubernetes/client-go](https://github.com/kubernetes/client-go). -See documentation for other libraries for how they authenticate. +To use it, +* 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. + +The Go client can use the same [kubeconfig file](/docs/user-guide/kubeconfig-file) +as the kubectl CLI does to locate and authenticate to the apiserver. See this [example](https://github.com/kubernetes/client-go/examples/out-of-cluster.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" +) +... + // 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.Core().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). + +There are [client libraries](https://github.com/kubernetes/kubernetes/tree/{{page.githubbranch}}/docs/devel/client-libraries.md) for accessing the API from other languages. See documentation for other libraries for how they authenticate. ### Accessing the API from a Pod @@ -138,7 +161,7 @@ From within a pod the recommended ways to connect to API are: in any container of the pod can access it. See this [example of using kubectl proxy in a pod](https://github.com/kubernetes/kubernetes/tree/{{page.githubbranch}}/examples/kubectl-container/). - use the Go client library, and create a client using the `client.NewInCluster()` factory. - This handles locating and authenticating to the apiserver. + This handles locating and authenticating to the apiserver. [example](https://github.com/kubernetes/client-go/examples/in-cluster.go) In each case, the credentials of the pod are used to communicate securely with the apiserver.