k8s client-go访问service的informer示例2之创建NewSharedIndexInformer
2022/2/20 23:56:44
本文主要是介绍k8s client-go访问service的informer示例2之创建NewSharedIndexInformer,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
这个实例通过svcInformer := cache.NewSharedIndexInformer创建informer, 不是直接使用系统也有coreinformers.ServiceInformer
package main import ( "flag" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/watch" "sync" rt "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/informers" coreinformers "k8s.io/client-go/informers/core/v1" "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" "k8s.io/kubernetes/pkg/controller" "math/rand" "path/filepath" "time" ) // Interval of synchronizing service status from apiserver const ( LocalServiceSyncPeriod = 30 * time.Second ) func main() { var wg sync.WaitGroup wg.Add(1) rand.Seed(time.Now().UnixNano()) klog.InitFlags(nil) var kubeconfigTemp *string if home := homedir.HomeDir(); home != "" { kubeconfigPath := filepath.Join(home, ".kube", "config") kubeconfigTemp = flag.String("kubeconfig1", kubeconfigPath, "(optional) absolute path to the kubeconfig file") } else { kubeconfigTemp = flag.String("kubeconfig1", "", "absolute path to the kubeconfig file") } flag.Parse() config, err := clientcmd.BuildConfigFromFlags("", *kubeconfigTemp) klog.Infof("test02") CheckErr(err) clientset, err := kubernetes.NewForConfig(config) CheckErr(err) //sharedInformers := informers.NewSharedInformerFactory(clientset, 0) stopCh := make(chan struct{}) defer close(stopCh) fieldSelectorNotSys1 := fields.OneTermEqualSelector("metadata.namespace", "default") //fieldSelectorNotSys2 := fields.OneTermNotEqualSelector("metadata.namespace", "polar") selectorStr := fields.AndSelectors(fieldSelectorNotSys1).String() klog.Infof("selectorStr:%s", selectorStr) svcInformer := cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options metav1.ListOptions) (rt.Object, error) { //options.FieldSelector = selectorStr return clientset.CoreV1().Services("").List(options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { //options.FieldSelector = selectorStr return clientset.CoreV1().Services("").Watch(options) }, }, &v1.Service{}, LocalServiceSyncPeriod, cache.Indexers{ cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, }, ) svcInformer.AddEventHandlerWithResyncPeriod( cache.FilteringResourceEventHandler{ FilterFunc: func(obj interface{}) bool { newSvc := obj.(*v1.Service) if newSvc.Namespace != "default" { klog.Infof("svc: filter svc, skip svc [%s/%s]\n", newSvc.Namespace, newSvc.Name) return false } else { klog.Infof("svc: filter svc, svc [%s/%s]\n", newSvc.Namespace, newSvc.Name) return true } }, Handler: cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { newSvc := obj.(*v1.Service) klog.Infof("controller: add svc, svc [%s/%s]\n", newSvc.Namespace, newSvc.Name) }, UpdateFunc: func(oldObj, newObj interface{}) { newSvc := newObj.(*v1.Service) klog.Infof("controller: Update svc, pod [%s/%s]\n", newSvc.Namespace, newSvc.Name) }, DeleteFunc: func(obj interface{}) { delSvc := obj.(*v1.Service) klog.Infof("controller: Delete svc, pod [%s/%s]\n", delSvc.Namespace, delSvc.Name) }, }, }, LocalServiceSyncPeriod, ) klog.Infof("svcInformer start of run") go svcInformer.Run(stopCh) if !controller.WaitForCacheSync("service", stopCh, svcInformer.HasSynced) { klog.Infof("svcInformer start of run") return } //time.Sleep(5 * time.Minute) wg.Wait() } func initInformer(clientset *kubernetes.Clientset) coreinformers.PodInformer { generate a shared informerFactory sharedInformerFactory := informers.NewSharedInformerFactory(clientset, 0) // create pod informer and start it podInformer := sharedInformerFactory.Core().V1().Pods() return podInformer } func CheckErr(err error) { if err != nil { panic(err) } }
这篇关于k8s client-go访问service的informer示例2之创建NewSharedIndexInformer的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 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平台