duanrong0738 2019-05-21 08:33
浏览 40

用kubernetes go-client观看持久卷

In this code I am watching the services, and it is working, my question is, how could I do to watch events when a persistentVolume is being created? I have checked the source code and it seems to return an interface, but I am not really familiar of how to make it work.

package main

import (
    "fmt"
    "time"

     "github.com/golang/glog"

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

func main() {
    config, err := clientcmd.BuildConfigFromFlags("", "")
    if err != nil {
        glog.Errorln(err)
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        glog.Errorln(err)
    }

    watchlist := cache.NewListWatchFromClient(
        clientset.CoreV1().RESTClient(),
        string(v1.ResourceServices),
        v1.NamespaceAll,
        fields.Everything(),
    )
    _, controller := cache.NewInformer( // also take a look at NewSharedIndexInformer
        watchlist,
        &v1.Service{},
        0, //Duration is int64
        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 
")
            },
         },
     )
         // I found it in k8s scheduler module. Maybe it's help if you 
    interested in.
     // serviceInformer := 
    cache.NewSharedIndexInformer(watchlist, 
     &v1.Service{}, 0, cache.Indexers{
     //     cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
     // })
     // go serviceInformer.Run(stop)
    stop := make(chan struct{})
    defer close(stop)
    go controller.Run(stop)
    for {
        time.Sleep(time.Second)
    }
}

Similar of what they are doing here with pods.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!