2016-12-05 54 views

回答

9

這是可以做到這樣的:

package main 

import (
    "fmt" 
    "flag" 
    "time" 

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

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 \n", obj) 
      }, 
      DeleteFunc: func(obj interface{}) { 
       fmt.Printf("service deleted: %s \n", obj) 
      }, 
      UpdateFunc:func(oldObj, newObj interface{}) { 
       fmt.Printf("service changed \n") 
      }, 
     }, 
    ) 
    stop := make(chan struct{}) 
    go controller.Run(stop) 
    for{ 
     time.Sleep(time.Second) 
    } 
} 
+0

這並不因爲進口的編譯。你能否更新它? – orkun1675

相關問題