背景

需要监听K8s中CRD资源的变动, 做出相应的处理, 需要针对 CRD资源建立informer

实现

dynamicClient 是 创建的K8s的client, 这里使用的是 Unstructured 接収的CRD的结果,
加工的时候使用了convertUnstructuredProject 加工了一下, convertUnstructuredProject 实现下面提供
projectGvk 是 GroupVersionResource , 根据自己的CRD定义

		// 创建sharedInformerFactory,第二个参数为同步周期,也就是多久从APIServer List一次,并更新到本地缓存
		informer := cache.NewSharedInformer(&cache.ListWatch{
			ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
				return dynamicClient.Resource(projectGvk).List(ctx, options)
			},
			WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
				return dynamicClient.Resource(projectGvk).Watch(ctx, options)
			},
		},
			&unstructured.Unstructured{},
			60*time.Second)
		// 创建informer

		if err != nil {
			log.Error("Failed to create informer", zap.Error(err))
			continue
		}
		// 注册资源事件处理方法
		informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {},
			UpdateFunc: func(oldObj, newObj interface{}) {
				newData, err := convertUnstructuredProject(newObj)
				if err != nil {
					log.Error("Failed to convert unstructured project", zap.Error(err))
				}
				oldData, err := convertUnstructuredProject(oldObj)
				if err != nil {
					log.Error("Failed to convert unstructured project", zap.Error(err))
				}
				if oldData.Generation != newData.Generation {
					fmt.Println("update", newData)
					saveProject(newData, clsuter.Platform)
				}
			},
			DeleteFunc: func(obj interface{}) {
				data, err := convertUnstructuredProject(obj)
				if err != nil {
					log.Error("Failed to convert unstructured project", zap.Error(err))
				}
				fmt.Println("delete", data)
				deleteProject(data, clsuter.Platform)
			},
		})
		// 启动
		stopCh := make(chan struct{})
		log.Info("Starting project informer factory")
		informer.Run(stopCh)
		// 等待父协程处理
		<-ctx.Done()
		close(stopCh)
		log.Info("Shutting down project informer factory")

convertUnstructuredProject的实现 Project 是实现的 runtime.Object 接口的struct

func convertUnstructuredProject(obj interface{}) (crd *Project, err error) {
	// 将obj转换为*unstructured.Unstructured类型
	u, ok := obj.(*unstructured.Unstructured)
	if !ok {
		fmt.Println("Failed to convert object to *unstructured.Unstructured")
		return
	}

	// 将u对象转换为YourCRDType类型
	crd = &Project{}
	err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), crd)
	if err != nil {
		fmt.Println("Failed to convert object to YourCRDType")
		return
	}
	return
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐