在 Kubernetes Operator 中,如何实现对特定 Kubernetes 资源实例的监视?
来源:stackoverflow
时间:2024-02-07 13:45:22 445浏览 收藏
今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《在 Kubernetes Operator 中,如何实现对特定 Kubernetes 资源实例的监视?》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!
我有一个操作员,它创建了许多守护进程集,这些守护进程集又创建了 pod。可以说operator -> daemonset1 -> pod1_1,operator -> daemonset2 - > {pod2_1,pod2_2})。 现在,我想捕获 daemonset2 pod 之一(假设 pod2_1)发生变化(例如创建一个新的而不是旧的)。我阅读了可用的文档,包括有关外部管理资源的文档,但我很难理解如何实现这个用例。我不想“监视”所有守护进程集或 pod,而只想与操作员在同一命名空间中“监视”一个特定守护进程集拥有的 pod。
声明 watches(&source.kind{type: &appsv1.daemonset{}}
不太适合,因为它监视所有命名空间中的所有 pod(如果我错了,请纠正我)。 owns(&appsv1.daemonset{ })
还按类型过滤,但不按特定 daemonset 实例过滤。
我还按照此答案中的建议尝试了 enqueuerequestsfrommapfunc
(请参阅下面的 golang 代码片段),但它确实为我不需要的其他 pod 调用了协调(请参阅下面的输出)。
func (r *mytest1reconciler) setupwithmanager(mgr ctrl.manager) error { log.info("mapfunc ", "a.getnamespace():", a.getnamespace(), "a.getname(): ", a.getname()) mapfn := handler.mapfunc( func(a client.object) []reconcile.request { return []reconcile.request{ {namespacedname: types.namespacedname{ name: a.getname(), namespace: "test", }}, } }) p := predicate.funcs{ //trying to filter out createfunc: func(e event.createevent) bool { namespace := e.object.getnamespace() name := e.object.getname() ok := ((namespace == "test") && (strings.contains(name, "pod2"))) return ok }, } mgr.getfieldindexer().indexfield() return ctrl.newcontrollermanagedby(mgr). for(&mytest1v1.mytest1{}). watches(&source.kind{type: &appsv1.daemonset{}}, handler.enqueuerequestsfrommapfunc(mapfn), builder.withpredicates(p)). complete(r) }
在协调中添加了调试日志记录以检查 pod 中的对象: log.info("协调:","命名空间:",req.namespace,"名称:",req.name)
并且发现了一堆来自外来 pod 的请求,尽管“test”命名空间是新的,并且“kubectl get pods”仅列出了我创建的那些 pod:
2022-09-12T02:43:59.735-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-cluster-csi-drivers", "Name: ": "aws-ebs-csi-driver-node"} 2022-09-12T02:43:59.779-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-cluster-node-tuning-operator", "a.GetName(): ": "tuned"} 2022-09-12T02:43:59.779-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-cluster-node-tuning-operator", "a.GetName(): ": "tuned"} 2022-09-12T02:43:59.779-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-cluster-node-tuning-operator", "Name: ": "tuned"} 2022-09-12T02:43:59.779-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-cluster-node-tuning-operator", "Name: ": "tuned"} 2022-09-12T02:43:59.884-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-monitoring", "Name: ": "node-exporter"} 2022-09-12T02:43:59.885-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-monitoring", "Name: ": "node-exporter"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-image-registry", "a.GetName(): ": "node-ca"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-image-registry", "a.GetName(): ": "node-ca"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-image-registry", "Name: ": "node-ca"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-multus", "a.GetName(): ": "network-metrics-daemon"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-multus", "a.GetName(): ": "network-metrics-daemon"} 2022-09-12T02:43:59.931-0700 INFO controller_xcrypt Reconcile: {"Namespace:": "openshift-multus", "Name: ": "network-metrics-daemon"} 2022-09-12T02:43:59.935-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-dns", "a.GetName(): ": "node-resolver"} 2022-09-12T02:43:59.935-0700 INFO controller_xcrypt MapFunc {"a.GetNamespace():": "openshift-dns", "a.GetName(): ": "node-resolver"}
即我收到了一大堆与我的案例无关的协调请求,甚至来自其他命名空间。不确定为什么每次协调都会被调用两次。
我遇到的大多数示例(例如这个)都包括按资源类型(例如 corev1.pod{})缩小范围,但不包括按该类型的特定实例缩小范围。这个问题看起来很相似,但与我的情况不同,观看的资源不是任意的。而且 enqueuerequestsfrommapfunc 建议不太适合我的用例(见上文)。
有没有一种好方法可以从 k8s 操作员处监视不直接由控制器拥有、而是通过子资源拥有的特定资源实例,例如 daemonset(监视由特定 daemonset 创建的 pod) ,由控制器创建)?
正确答案
操作员根据其 crd 中定义的范围值监视由其创建的对象。如果范围定义为“集群”,那么它将监视所有名称空间。如果范围定义为“命名空间”,那么它将仅监视特定命名空间中的对象。更多详情请参考CRD documentation
# either Namespaced or Cluster scope: Namespaced
对于上述要求,它由 kubernetes 控制器负责处理,例如:如果所需状态不匹配,pod 将自动为守护程序集或部署创建。
终于介绍完啦!小伙伴们,这篇关于《在 Kubernetes Operator 中,如何实现对特定 Kubernetes 资源实例的监视?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习