登录
首页 >  Golang >  Go问答

在 Kubernetes Operator 中,如何实现对特定 Kubernetes 资源实例的监视?

来源:stackoverflow

时间:2024-02-07 13:45:22 445浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《在 Kubernetes Operator 中,如何实现对特定 Kubernetes 资源实例的监视?》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我有一个操作员,它创建了许多守护进程集,这些守护进程集又创建了 pod。可以说operator -> daemonset1 -> pod1_1operator -> 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相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>