git clone https://github.com/kubernetes/client-go.git
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration# go build -o app .
go: downloading k8s.io/api v0.0.0-20210702094336-49e8721f8489
o: downloading k8s.io/api v0.0.0-20210702094336-49e8721f8489
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration# ./app
There are 32 pods in the cluster
Pod example-xxxxx in namespace default not found
^C
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration#
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
for {
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("There are %d pods in the cluster
", len(pods.Items))
// Examples for error handling:
// - Use helper functions like e.g. errors.IsNotFound()
// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
namespace := "volcano-system"
pod := "volcano-scheduler-7f48dddb8f-8g6b5"
_, err = clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found
", pod, namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v
",
pod, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s
", pod, namespace)
}
time.Sleep(10 * time.Second)
}
}
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration# ./app
There are 32 pods in the cluster
Found pod volcano-scheduler-7f48dddb8f-8g6b5 in namespace volcano-system
There are 32 pods in the cluster
Found pod volcano-scheduler-7f48dddb8f-8g6b5 in namespace volcano-system
^C
volcano-system volcano-admission-6cc49fdc5-5zgzs 1/1 Running 0 23h 10.244.29.8 bogon <none> <none>
volcano-system volcano-admission-init-qgh9b 0/1 Completed 0 23h 10.244.29.6 bogon <none> <none>
volcano-system volcano-controllers-5f5c4f4785-8dbgl 1/1 Running 0 23h 10.244.29.7 bogon <none> <none>
volcano-system volcano-scheduler-7f48dddb8f-8g6b5 1/1 Running 0 23h 10.244.29.5 bogon <none> <none>
kubernetes.NewForConfig
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
//"time"
//"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
//
// Uncomment to load all auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// 使用 clientsent 获取 Deployments
deployments, err := clientset.AppsV1().Deployments("default").List(context.TODO(),metav1.ListOptions{})
if err != nil {
panic(err)
}
for idx, deploy := range deployments.Items {
fmt.Printf("%d -> %s
", idx+1, deploy.Name)
}
}
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration# go build -o app .
root@ubuntu:~/client-go/examples/out-of-cluster-client-configuration# ./app
1 -> example-foo
root@ubuntu:~/sample-controller# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
example-foo 1/1 1 1 3h16m
root@ubuntu:~/sample-controller#