我将client-go用于kubernetes监视列表时出现一些问题

I have some when I use the code in StackOverflow. Such as

package main

import (
    "flag"
    "fmt"
    "time"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/pkg/api/v1"
    "k8s.io/client-go/pkg/fields"
    "k8s.io/client-go/tools/cache"
    "k8s.io/client-go/tools/clientcmd"
)

var (
    kubeconfig = flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file")
)

func main() {
    flag.Parse()
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }

    watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "services", v1.NamespaceDefault,
        fields.Everything())
    _, controller := cache.NewInformer(
        watchlist,
        &v1.Service{},
        time.Second * 0,
        cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                fmt.Printf("service added: %s 
", obj)
            },
            DeleteFunc: func(obj interface{}) {
                fmt.Printf("service deleted: %s 
", obj)
            },
            UpdateFunc:func(oldObj, newObj interface{}) {
                fmt.Printf("service changed 
")
            },
        },
    )
    stop := make(chan struct{})
    go controller.Run(stop)
    for{
        time.Sleep(time.Second)
    }
}

it have some errors:

  1. i can not find the package "k8s.io/client-go/pkg/api/v1" and "k8s.io/client-go/pkg/fields", i don't know how to find it:

    https://github.com/kubernetes/client-go

  2. the code

watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "services", v1.NamespaceDefault,
        fields.Everything())

has the error:

Cannot use 'clientSet.CoreV1().RESTClient()' (type rest.Interface) as type Getter Type does not implement 'Getter' as some methods are missing: Get() *restclient.Request

Could anyone tell me how to resolve it? Thanks in advance.

doupinyan0186
doupinyan0186 恩..我尝试一下。这似乎是客户端版本的问题。
大约一年之前 回复
dongshanji3102
dongshanji3102 这是ClientSet文件:godoc.org/k8s.io/client-go/kubernetes#Clientset。您可以调用ClientSet.CoreV1(),然后从中获取getter接口之一,并调用WatchAPI来监视资源。
大约一年之前 回复
douchensou6969
douchensou6969 是的,kubernetes已将一些“客户端访问”api合并到其api中,但是我无法找到如何执行监视列表功能。
大约一年之前 回复
duangekui7451
duangekui7451 这些进口看起来不正确。在这里查看示例:github.com/kubernetes/client-go/tree/master/examples
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问