用kubernetes go-client观看持久卷

时间:2019-05-21 08:33:09

标签: go kubernetes kubernetes-go-client

在这段代码中,我正在监视服务,并且正在运行,我的问题是,在创建persistentVolume时如何监视事件?我检查了the source code,它似乎返回了一个接口,但是我对如何使其工作并不十分熟悉。

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 \n", obj)
            },
            DeleteFunc: func(obj interface{}) {
                fmt.Printf("service deleted: %s \n", obj)
            },
            UpdateFunc: func(oldObj, newObj interface{}) {
                fmt.Printf("service changed \n")
            },
         },
     )
         // 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)
    }
}

他们在做什么here with pods

0 个答案:

没有答案