首页 > 解决方案 > K8s operator 监听特定的 config map

问题描述

我有一个操作员为某些对象更改运行协调,现在我想添加在特定configmap更改时进行协调的能力,(我的操作员对此 CM负责,只需要听它并阅读更改... ) 从文档中我认为我需要使用Owns(&corev1.Configmap{})但不确定如何使用并提供特定的 configmap 名称以供观看,

我应该如何参考特定name: foo的configmapnamespace=bar

https://sdk.operatorframework.io/docs/building-operators/golang/references/event-filtering/#using-predicates

标签: gokubernetes

解决方案


这个具体的算子框架我没用过,但是概念很熟悉。创建一个这样的谓词函数,并在创建控制器时通过将其传递给 SDK 的WithEventFilter函数来使用它:

func specificConfigMap(name, namespace string) predicate.Predicate {
    return predicate.Funcs{
        UpdateFunc: func(e event.UpdateEvent) bool {
            configmap := e.NewObject.(*corev1.ConfigMap)
            if configmap.Name == name && configmap.Namespace == namespace {
                return true
            }
            return false
        },
    }
}

推荐阅读