• clientgo中使用informer开发自定义operator


    开发operator一共分为5步骤

    1、创建config

    config的创建有两种方式

    1、clientcmd.BuildConfigFromFlags

    config, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
    	if err != nil {
    		//访问集群内
    		inClusterConfig, err := rest.InClusterConfig()
    		if err != nil {
    			log.Fatalln("can't get config")
    		}
    		config = inClusterConfig
    	}
    

    clientcmd.RecommendedHomeFile可以自己指定路径一般为~/.kube/config

    2、config,err :=config.GetConfig()

    2、创建client

    lientset, err := kubernetes.NewForConfig(config)
    	if err != nil {
    		log.Fatalln("can't create client")
    	}
    

    3、创建informer

    factory := informers.NewSharedInformerFactory(clientset, 0)
    serviceInformer := factory.Core().V1().Services()
    ingressInformer := factory.Networking().V1().Ingresses()
    

    4、add event handler

    //注册到eventhandler
    	controller := pkg.NewController(clientset, serviceInformer, ingressInformer)
    	stopCh := make(chan struct{})
    	factory.Start(stopCh)
    	//等待所有的informer缓存同步
    	factory.WaitForCacheSync(stopCh)
    

    在注册到eventhandler中,会进行事件的操作,如:

    func NewController(client kubernetes.Interface, serviceInformer informer.ServiceInformer, ingressInformer netInformer.IngressInformer) controller {
    	c := controller{
    		client:        client,
    		ingressLister: ingressInformer.Lister(),
    		serviceLister: serviceInformer.Lister(),
    		queue:         workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ingressManager"),
    	}
    
    	serviceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
    		AddFunc:    c.addService,
    		UpdateFunc: c.updateService,
    	})
    	//cache.Store()
    
    	ingressInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
    		DeleteFunc: c.deleteIngress,
    	})
    
    	return c
    }

    5、启动informer

    controller.Run(stopCh)
    

    6、informer案例

    案例1:

    案例2:

  • 相关阅读:
    产品列表
    PHP全栈学习笔记5
    PHP全栈学习笔记5
    PHP全栈学习笔记4
    更新YUM源后的arning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY错误
    监听EditText输入完成
    关于PHP接收HTTP模拟POST传JSON格式时$_POST为空的问题
    安卓时间类型的转换和比大小
    Android之观察者/被观察者模式Observer/Observable
    WKWebView--JS调用OC的方法
  • 原文地址:https://www.cnblogs.com/wuchangblog/p/16661637.html
Copyright © 2020-2023  润新知