在kubernetes儀表盤,可以查看事件命名空間:例如」 拉形象‘你好,世界’,成功扳回一像‘你好,世界’等如何使用它的客戶端觀看kubernetes事件細節?
有沒有辦法使用來獲得所有這些事件?在它的旅途客戶
非常感謝
在kubernetes儀表盤,可以查看事件命名空間:例如」 拉形象‘你好,世界’,成功扳回一像‘你好,世界’等如何使用它的客戶端觀看kubernetes事件細節?
有沒有辦法使用來獲得所有這些事件?在它的旅途客戶
非常感謝
使用NewInformer()
函數來創建特定類型的事件通知
下面是一個最小的例子(source):
import (
"fmt"
"log"
"net/http"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/restclient"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/util/wait"
)
func podCreated(obj interface{}) {
pod := obj.(*api.Pod)
fmt.Println("Pod created: " + pod.ObjectMeta.Name)
}
func podDeleted(obj interface{}) {
pod := obj.(*api.Pod)
fmt.Println("Pod deleted: " + pod.ObjectMeta.Name)
}
func watchPods(client *client.Client, store cache.Store) cache.Store {
//Define what we want to look for (Pods)
watchlist := cache.NewListWatchFromClient(client, "pods", api.NamespaceAll, fields.Everything())
resyncPeriod := 30 * time.Minute
//Setup an informer to call functions when the watchlist changes
eStore, eController := framework.NewInformer(
watchlist,
&api.Pod{},
resyncPeriod,
framework.ResourceEventHandlerFuncs{
AddFunc: podCreated,
DeleteFunc: podDeleted,
},
)
//Run the controller as a goroutine
go eController.Run(wait.NeverStop)
return eStore
}
func main() {
//Configure cluster info
config := &
restclient.Config{
Host: "https://xxx.yyy.zzz:443",
Username: "kube",
Password: "supersecretpw",
Insecure: true,
}
//Create a new client to interact with cluster and freak if it doesn't work
kubeClient, err := client.New(config)
if err != nil {
log.Fatalln("Client not created sucessfully:", err)
}
//Create a cache to store Pods
var podsStore cache.Store
//Watch for Pods
podsStore = watchPods(kubeClient, podsStore)
//Keep alive
log.Fatal(http.ListenAndServe(":8080", nil))
}
這其他客戶端看起來相當不錯https://godoc.org/bitbucket.org/amdatulabs/amdatu-kubernetes-go/client#Client.WatchPodsWithLabel –