当前位置: 首页 > news >正文

做笔记的网站黑帽seo教程

做笔记的网站,黑帽seo教程,温岭公司做网站,建筑师证报考条件sample-controller sample-controller 是 K8s 官方自定义 CDR 及控制器是实现的例子 通过使用这个自定义 CDR 控制器及阅读它的代码,基本可以了解如何制作一个 CDR 控制器 CDR 运作原理 网上有更好的文章,说明其运作原理: https://www.z…

sample-controller

sample-controller 是 K8s 官方自定义 CDR 及控制器是实现的例子

通过使用这个自定义 CDR 控制器及阅读它的代码,基本可以了解如何制作一个 CDR 控制器

CDR 运作原理

网上有更好的文章,说明其运作原理:

  • https://www.zhaohuabing.com/post/2023-03-09-how-to-create-a-k8s-controller/
  • https://www.zhaohuabing.com/post/2023-04-04-how-to-create-a-k8s-controller-2/

CDR 定义 yaml 文件格式细节

官方文档: https://kubernetes.io/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

sample-controller 目录文件说明

fananchong@myubuntu:~/k8s.io/sample-controller$ tree -L 2
.
|-- artifacts
|   `-- examples                            # CRD yaml 文件定义例子
|-- controller.go                           # 控制器实现
|-- hack                                    # k8s.io/code-generator 提供的生成 pkg/generated/ 、 pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
|   |-- boilerplate.go.txt
|   |-- custom-boilerplate.go.txt
|   |-- tools.go
|   |-- update-codegen.sh
|   `-- verify-codegen.sh
|-- main.go                                 # main 函数
|-- pkg
|   |-- apis                                # k8s.io/code-generator 根据 types.go 、 doc.go 来生成
|   |-- generated                           # 自动生成
|   `-- signals
`-- vendor                                  # go mod vendor|-- github.com|-- golang.org|-- google.golang.org|-- gopkg.in|-- k8s.io|-- modules.txt`-- sigs.k8s.io

controller.go 关键代码分析

  1. Informer 监听事件
    fooInformer 监听 Foo CDR 事件
    deploymentInformer 监听 Deployment 事件

        // Set up an event handler for when Foo resources changefooInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{AddFunc: controller.enqueueFoo,UpdateFunc: func(old, new interface{}) {controller.enqueueFoo(new)},})// Set up an event handler for when Deployment resources change. This// handler will lookup the owner of the given Deployment, and if it is// owned by a Foo resource then the handler will enqueue that Foo resource for// processing. This way, we don't need to implement custom logic for// handling Deployment resources. More info on this pattern:// https://github.com/kubernetes/community/blob/8cafef897a22026d42f5e5bb3f104febe7e29830/contributors/devel/controllers.mddeploymentInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{AddFunc: controller.handleObject,UpdateFunc: func(old, new interface{}) {newDepl := new.(*appsv1.Deployment)oldDepl := old.(*appsv1.Deployment)if newDepl.ResourceVersion == oldDepl.ResourceVersion {// Periodic resync will send update events for all known Deployments.// Two different versions of the same Deployment will always have different RVs.return}controller.handleObject(new)},DeleteFunc: controller.handleObject,})
    
  2. Foo CDR 事件处理
    压入工作队列

    // enqueueFoo takes a Foo resource and converts it into a namespace/name
    // string which is then put onto the work queue. This method should *not* be
    // passed resources of any type other than Foo.
    func (c *Controller) enqueueFoo(obj interface{}) {var key stringvar err errorif key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {utilruntime.HandleError(err)return}c.workqueue.Add(key)
    }
    
  3. Deployment 事件处理
    判断是属于 Foo 控制器创建的 Deployment ,压入队列

    // handleObject will take any resource implementing metav1.Object and attempt
    // to find the Foo resource that 'owns' it. It does this by looking at the
    // objects metadata.ownerReferences field for an appropriate OwnerReference.
    // It then enqueues that Foo resource to be processed. If the object does not
    // have an appropriate OwnerReference, it will simply be skipped.
    func (c *Controller) handleObject(obj interface{}) {var object metav1.Objectvar ok boollogger := klog.FromContext(context.Background())if object, ok = obj.(metav1.Object); !ok {tombstone, ok := obj.(cache.DeletedFinalStateUnknown)if !ok {utilruntime.HandleError(fmt.Errorf("error decoding object, invalid type"))return}object, ok = tombstone.Obj.(metav1.Object)if !ok {utilruntime.HandleError(fmt.Errorf("error decoding object tombstone, invalid type"))return}logger.V(4).Info("Recovered deleted object", "resourceName", object.GetName())}logger.V(4).Info("Processing object", "object", klog.KObj(object))if ownerRef := metav1.GetControllerOf(object); ownerRef != nil {// If this object is not owned by a Foo, we should not do anything more// with it.if ownerRef.Kind != "Foo" {return}foo, err := c.foosLister.Foos(object.GetNamespace()).Get(ownerRef.Name)if err != nil {logger.V(4).Info("Ignore orphaned object", "object", klog.KObj(object), "foo", ownerRef.Name)return}c.enqueueFoo(foo)return}
    }
    
  4. Controller.Run 处理
    从队列中取出元素
    如果副本预期不一致,做 scale 处理

    // syncHandler compares the actual state with the desired, and attempts to
    // converge the two. It then updates the Status block of the Foo resource
    // with the current status of the resource.
    func (c *Controller) syncHandler(ctx context.Context, key string) error {// Convert the namespace/name string into a distinct namespace and namelogger := klog.LoggerWithValues(klog.FromContext(ctx), "resourceName", key)namespace, name, err := cache.SplitMetaNamespaceKey(key)if err != nil {utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))return nil}// Get the Foo resource with this namespace/namefoo, err := c.foosLister.Foos(namespace).Get(name)if err != nil {// The Foo resource may no longer exist, in which case we stop// processing.if errors.IsNotFound(err) {utilruntime.HandleError(fmt.Errorf("foo '%s' in work queue no longer exists", key))return nil}return err}deploymentName := foo.Spec.DeploymentNameif deploymentName == "" {// We choose to absorb the error here as the worker would requeue the// resource otherwise. Instead, the next time the resource is updated// the resource will be queued again.utilruntime.HandleError(fmt.Errorf("%s: deployment name must be specified", key))return nil}// Get the deployment with the name specified in Foo.specdeployment, err := c.deploymentsLister.Deployments(foo.Namespace).Get(deploymentName)// If the resource doesn't exist, we'll create itif errors.IsNotFound(err) {deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Create(context.TODO(), newDeployment(foo), metav1.CreateOptions{})}// If an error occurs during Get/Create, we'll requeue the item so we can// attempt processing again later. This could have been caused by a// temporary network failure, or any other transient reason.if err != nil {return err}// If the Deployment is not controlled by this Foo resource, we should log// a warning to the event recorder and return error msg.if !metav1.IsControlledBy(deployment, foo) {msg := fmt.Sprintf(MessageResourceExists, deployment.Name)c.recorder.Event(foo, corev1.EventTypeWarning, ErrResourceExists, msg)return fmt.Errorf("%s", msg)}// If this number of the replicas on the Foo resource is specified, and the// number does not equal the current desired replicas on the Deployment, we// should update the Deployment resource.if foo.Spec.Replicas != nil && *foo.Spec.Replicas != *deployment.Spec.Replicas {logger.V(4).Info("Update deployment resource", "currentReplicas", *foo.Spec.Replicas, "desiredReplicas", *deployment.Spec.Replicas)deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Update(context.TODO(), newDeployment(foo), metav1.UpdateOptions{})}// If an error occurs during Update, we'll requeue the item so we can// attempt processing again later. This could have been caused by a// temporary network failure, or any other transient reason.if err != nil {return err}// Finally, we update the status block of the Foo resource to reflect the// current state of the worlderr = c.updateFooStatus(foo, deployment)if err != nil {return err}c.recorder.Event(foo, corev1.EventTypeNormal, SuccessSynced, MessageResourceSynced)return nil
    }
    
http://www.tj-hxxt.cn/news/27652.html

相关文章:

  • 南昌做网站和微信小程序的公司品牌宣传策略有哪些
  • 怎样在在农行网站上做风险评估海南网站制作公司
  • 烟台莱州网站建设邵阳疫情最新消息
  • 网站建设栏目添加百度互联网营销顾问
  • 同城便民网站开发渠道网官网
  • 网站连接怎么做广州seo搜索
  • 网站宣传推广平台网络推广的网站有哪些
  • 请人帮忙做淘宝网站多少钱上海网络推广外包
  • 网站建设杭州哪家便宜南昌seo优化
  • 做网站和seo流程网络推广计划制定步骤
  • 西安做网站培训百度下载安装2019
  • 学校网站建设先进事迹西安网站公司推广
  • 自己建网站做外贸怎么在百度上发表文章
  • 网站空间 按流量计费外贸推广优化公司
  • 哪里有做美食的视频网站seo自学教程
  • 网站推广营销方案关键词挖掘站长工具
  • 建网站如果不买域名别人能不能访问站长工具seo综合查询腾讯
  • 企业网站建站 费用外链网站大全
  • nike网站策划与建设天津搜索引擎优化
  • 门户网站构建seo排名优化北京
  • 做网站超链接用什么软件河南最新消息
  • 网站开发功能书seo网站推广简历
  • 在线做动漫图的网站网站推广平台
  • 做app的公司有哪些seo优化排名怎么做
  • 做动态网站需要多少钱营销平台有哪些
  • 怎样自己做网站模板中国万网域名注册官网
  • 厦门市网站建设app开发营销型网站建设易网拓
  • 济南营销网站制作公司兰州seo实战优化
  • 深圳网站建设哪个公司号杭州百度seo
  • 最新有限公司网站网络营销策划案例