针对kubernetes 1.14
    针对版本问题,从网上抄到一个
    来源: 链接

    1. go mod edit -require="k8s.io/client-go@kubernetes-1.14.6"
    2. go mod edit -require="k8s.io/api@v0.0.0-20190816222004-e3a6b8045b0b"
    3. go mod edit -require="k8s.io/apimachinery@v0.0.0-20190816221834-a9f1d8a9c101"
    4. go mod edit -require="k8s.io/utils@v0.0.0-20190221042446-c2654d5206da"
    5. go mod edit -require="sigs.k8s.io/yaml@v1.1.0"

    代码如下:

    package main
    
    import (
        "encoding/json"
        "flag"
        "fmt"
        v1 "k8s.io/api/apps/v1"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/fields"
        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/tools/cache"
        "k8s.io/client-go/tools/clientcmd"
        "os"
        "path/filepath"
        "time"
        //
        // 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() {
        token := ""
        fmt.Println(token)
        masterUrl := ""
        fmt.Println(masterUrl)
        fmt.Println("===================")
    
        var kubeconfig *string
        if home := 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()
        fmt.Println(*kubeconfig)
    
        // use the current context in kubeconfig
        config, err := clientcmd.BuildConfigFromFlags(masterUrl, *kubeconfig)
        fmt.Println(config)
        if err != nil {
            panic(err.Error())
        }
    
        // create the clientset
        clientset, err := kubernetes.NewForConfig(config)
        if err != nil {
            panic(err.Error())
        }
    
        pods, err := clientset.CoreV1().Pods("ops").List(metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }
        fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
    
        // watch
        watchList := cache.NewListWatchFromClient(clientset.AppsV1().RESTClient(), "deployments", "ops", fields.Everything())
        _, controller := cache.NewInformer(watchList, &v1.Deployment{}, time.Second*0, cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                jsn, _ := json.Marshal(obj)
                fmt.Printf("Deployment added: %s\n", jsn)
            },
        })
        //fmt.Println(controller)
        stop := make(chan struct{})
        go controller.Run(stop)
        for {
            time.Sleep(time.Second)
        }
    
    }
    
    func homeDir() string {
        if h := os.Getenv("HOME"); h != "" {
            return h
        }
        return os.Getenv("USERPROFILE") // windows
    }