k8s之client-go informer使用
2022/1/9 23:04:54
本文主要是介绍k8s之client-go informer使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import ( "fmt" "time" v1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" ) func demo(config *rest.Config, namespace string) { clientset, err := kubernetes.NewForConfig(config) if err != nil { panic(err) } informerFactory := informers.NewSharedInformerFactory(clientset, time.Minute) informer := informerFactory.Apps().V1().Deployments() informer.Informer().AddEventHandler( cache.ResourceEventHandlerFuncs{ AddFunc: onAdd, UpdateFunc: onUpdate, DeleteFunc: onDelete, }) lister := informer.Lister() stopCh := make(chan struct{}) defer close(stopCh) informerFactory.Start(stopCh) if !cache.WaitForCacheSync(stopCh, informer.Informer().HasSynced) { return } deployments, err := lister.Deployments(namespace).List(labels.Everything()) if err != nil { panic(err) } for _, deployment := range deployments { fmt.Printf("%s\r\n", deployment.Name) } <-stopCh } func onAdd(obj interface{}) { deployment := obj.(*v1.Deployment) fmt.Printf("onAdd:%s\r\n", deployment.Name) } func onUpdate(old, new interface{}) { oldDeployment := old.(*v1.Deployment) newDeployment := new.(*v1.Deployment) fmt.Printf("onUpdate:%s to %s\r\n", oldDeployment.Name, newDeployment.Name) } func onDelete(obj interface{}) { deployment := obj.(*v1.Deployment) fmt.Printf("onDelete:%s\r\n", deployment.Name) }
这篇关于k8s之client-go informer使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15在Kubernetes (k8s) 中搭建三台 Nginx 服务器怎么实现?-icode9专业技术文章分享
- 2024-11-05基于Kubernetes的自定义AWS云平台搭建指南
- 2024-11-05基于Kubernetes Gateway API的现代流量管理方案
- 2024-11-05在Kubernetes上部署你的第一个应用:Nginx服务器
- 2024-11-05利用拓扑感知路由控制Kubernetes中的流量
- 2024-11-05Kubernetes中的层次命名空间:更灵活的资源管理方案
- 2024-11-055分钟上手 Kubernetes:精简实用的 Kubectl 命令速查宝典!
- 2024-10-30K8s 容器的定向调度与亲和性
- 2024-10-28云原生周刊:K8s未来三大发展方向 丨2024.10.28
- 2024-10-25亚马逊弹性Kubernetes服务(EKS)实战:轻松搭建Kubernetes平台