K8s操作员监听特定的配置映射。

huangapple go评论82阅读模式
英文:

K8s operator listen to specific config map

问题

我有一个运算符,用于对一些对象的更改进行调和,现在我想在特定的configmap更改时添加调和的能力(我的运算符不负责这个CM,只需要监听它并读取更改...)根据文档,我认为我需要使用Owns(&corev1.Configmap{}),但不确定如何做以及如何提供要监视的特定configmap名称。

namespace=bar中,我应该如何引用特定的configmap名称name: foo

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

英文:

I've an operator which run a reconcile for some object changes, now I want to add ability to reconcile when specific configmap is changing, (my operator doesn't responsible on this CM just needs to listen to it and read on changes...) from the docs I think I need to use the Owns(&corev1.Configmap{}) but not sure how to do it and provide specific configmap name to watch,

How should I refer to specific configmap name: foo in namespace=bar

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

答案1

得分: 3

我还没有使用过这个特定的操作符框架,但是这些概念我很熟悉。创建一个像这样的谓词函数,并在创建控制器时使用它,通过将其传递给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
		},
	}
}

希望对你有帮助!

英文:

I haven't used this specific operator framework, but the concepts are familiar. Create a predicate function like this and use it when you are creating a controller by passing it into the SDK's WithEventFilter function:

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
		},
	}
}

huangapple
  • 本文由 发表于 2021年6月4日 23:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/67840081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定