我正在尝试列出在operator-sdk操作符中设置为不可调度的所有节点。通常(1.12之前),这意味着他们设置了spec.unscheduleable。所以我试了一下:
nodes := &corev1.NodeList{}
opts := &client.ListOptions{}
if err := opts.SetFieldSelector("spec.unschedulable=true"); err != nil {
reqLogger.Info("Failed to set field selector")
}这是错误的:
2019-04-23T10:19:39.761-0700 ERROR kubebuilder.controller Reconciler error {"controller": "node-controller", "request": "/nodename", "error": "Index with name field:spec.unschedulable does not exist"}我对此感到困惑,因为字段选择器在kubectl中工作:
kubectl get nodes --field-selector="spec.unschedulable=true"除了这个问题,我还注意到在v1.12之后,spec.unscheduleable字段已经被deprecated替换为TaintNodeByCondition。这使事情变得更加复杂,因为现在我真的不认为我可以使用字段选择器,因为我不相信(除非我错了?)你可以对污点使用fieldselector。
因此,我的问题是-如何有效地列出集群中所有受污染/无法调度的节点,特别是在使用operator-sdk时
更新:
我已经设法通过使用v1meta.ListOptions调用解决了字段选择器问题,如下所示:
nodes := &corev1.NodeList{}
opts := &client.ListOptions{
Raw: &metav1.ListOptions{
FieldSelector: "spec.unschedulable=true",
},
}然而,我仍然不知道如何处理污染,所以我已经编辑了这个问题,并将其保留为开放状态
发布于 2019-12-22 04:09:31
根据Controller Runtime文档,使用raw选项不能很好地处理缓存。
相反,您应该在控制器初始化期间将所需的字段添加到索引中:
idx := myManager.GetFieldIndexer()
idx.IndexField(&corev1.Node{}, "spec.unschedulable", func(o runtime.Object) []string {
return []string{fmt.Sprintf("%v", o.(*corev1.Node).Spec.Unschedulable)}
})不过,我不知道这是否能帮助您使用Operator SDK。
发布于 2021-02-02 07:33:52
我正在使用client-go API,我也一直在努力解决这个问题,所以我做了一个变通方法,我不确定这是否是最好的选择。尽管如此,还是会分享我的代码。
它从获取所有节点开始,然后过滤没有污染的节点。
func GetNodes(withoutTaints bool, clientset kubernetes.Interface, ctx
*context.Context, options *metav1.ListOptions) *v1.NodeList {
nodes, err := clientset.CoreV1().Nodes().List(*ctx, *options)
if err != nil {
panic(err)
}
if withoutTaints {
nodesWithoutTaints := v1.NodeList{}
for _, node := range nodes.Items {
if len(node.Spec.Taints) == 0 {
nodesWithoutTaints.Items = append(nodesWithoutTaints.Items, node)
}
}
return &nodesWithoutTaints
}
return nodes
}https://stackoverflow.com/questions/55816640
复制相似问题