update client-go to the latest version to keep in sync with client-go example (#16536)
This commit is contained in:
committed by
Kubernetes Prow Robot
parent
900ea77f57
commit
03456f1a72
@@ -161,28 +161,36 @@ Kubernetes officially supports client libraries for [Go](#go-client) and
|
|||||||
|
|
||||||
#### Go client
|
#### Go client
|
||||||
|
|
||||||
* To get the library, run the following command: `go get k8s.io/client-go/<version number>/kubernetes` See [https://github.com/kubernetes/client-go](https://github.com/kubernetes/client-go) to see which versions are supported.
|
* To get the library, run the following command: `go get k8s.io/client-go@kubernetes-<kubernetes-version-number>` See [https://github.com/kubernetes/client-go/releases](https://github.com/kubernetes/client-go/releases) 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.
|
* Write an application atop of the client-go clients.
|
||||||
|
|
||||||
|
{{< note >}}
|
||||||
|
|
||||||
|
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 >}}
|
||||||
|
|
||||||
The Go client can use the same [kubeconfig file](/docs/concepts/cluster-administration/authenticate-across-clusters-kubeconfig/)
|
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):
|
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
|
```golang
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"k8s.io/client-go/1.4/kubernetes"
|
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/1.4/pkg/api/v1"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/1.4/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
...
|
|
||||||
// uses the current context in kubeconfig
|
func main() {
|
||||||
config, _ := clientcmd.BuildConfigFromFlags("", "path to kubeconfig")
|
// uses the current context in kubeconfig
|
||||||
// creates the clientset
|
// path-to-kubeconfig -- for example, /root/.kube/config
|
||||||
clientset, _:= kubernetes.NewForConfig(config)
|
config, _ := clientcmd.BuildConfigFromFlags("", "<path-to-kubeconfig>")
|
||||||
// access the API to list pods
|
// creates the clientset
|
||||||
pods, _:= clientset.CoreV1().Pods("").List(v1.ListOptions{})
|
clientset, _ := kubernetes.NewForConfig(config)
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
// 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).
|
If the application is deployed as a Pod in the cluster, please refer to the [next section](#accessing-the-api-from-a-pod).
|
||||||
|
|||||||
Reference in New Issue
Block a user