[英]Client-Go, How to watch for newly created Pods in Kubernetes
I need to write a golang
application with the help of client-go which will listen/watch a particular namespace for any of these events:我需要在client-go的帮助下编写一个
golang
应用程序,它将监听/监视任何这些事件的特定命名空间:
And I want to communicate this information to another application application running in other namespace.我想将此信息传达给在其他命名空间中运行的另一个应用程序应用程序。
I am really new to the client-go
library and I searched their documentation but couldn't find something similar to Events in Kopf我是
client-go
库的新手,我搜索了他们的文档,但找不到与Kopf 中的事件类似的内容
I am new to this library and I couldn't find a method/function of doing this.我是这个库的新手,找不到执行此操作的方法/功能。 I don't need to have the full code of doing this, but I appreciate where I can look into, so I can find my way out
我不需要拥有执行此操作的完整代码,但我很欣赏我可以查看的地方,因此我可以找到出路
Can someone help me on this?有人可以帮我吗?
You can create a clientset from parsing the kubeconfig file and then use this clientset to create a sharedInformerfactory for your particular namespace.您可以通过解析 kubeconfig 文件创建一个客户端集,然后使用该客户端集为您的特定命名空间创建一个 sharedInformerfactory。 Get a informer for your pods and add Event Handler functions.
为您的 pod 获取线人并添加事件处理函数。 Implement those functions according to your requirement.
根据您的要求实现这些功能。 You can check for container updates between oldPod and newPod in the
OnUpdate
function. Use the clientset for however you want to communicate with other applications.您可以在
OnUpdate
function 中检查 oldPod 和 newPod 之间的容器更新。使用客户端集来与其他应用程序通信。 I would say explore the methods that clientset implements to get a detailed idea how it works.我想说的是探索 clientset 实现的方法,以详细了解它的工作原理。
package main
import (
"flag"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
"path/filepath"
"time"
)
func main() {
// parse the .kubeconfig file
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// create config from the kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
// create the clientset
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
// stop signal for the informer
stopper := make(chan struct{})
defer close(stopper)
// create shared informers for resources in all known API group versions with a reSync period and namespace
factory := informers.NewSharedInformerFactoryWithOptions(clientSet, 10*time.Second, informers.WithNamespace("demo"))
podInformer := factory.Core().V1().Pods().Informer()
defer runtime.HandleCrash()
// start informer ->
go factory.Start(stopper)
// start to sync and call list
if !cache.WaitForCacheSync(stopper, podInformer.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
return
}
podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: onAdd, // register add eventhandler
UpdateFunc: onUpdate,
DeleteFunc: onDelete,
})
// block the main go routine from exiting
<-stopper
}
func onAdd(obj interface{}) {
pod := obj.(*corev1.Pod)
klog.Infof("POD CREATED: %s/%s", pod.Namespace, pod.Name)
}
func onUpdate(oldObj interface{}, newObj interface{}) {
oldPod := oldObj.(*corev1.Pod)
newPod := newObj.(*corev1.Pod)
klog.Infof(
"POD UPDATED. %s/%s %s",
oldPod.Namespace, oldPod.Name, newPod.Status.Phase,
)
}
func onDelete(obj interface{}) {
pod := obj.(*corev1.Pod)
klog.Infof("POD DELETED: %s/%s", pod.Namespace, pod.Name)
}
You could use something like kube.netes-event-exporter or kube-eventer and send messages with different sinks.您可以使用kube.netes-event-exporter或kube-eventer 之类的东西,并使用不同的接收器发送消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.