【问题标题】:Listing unscheduleable kubernetes nodes by taints in operator-sdk/client-go通过 operator-sdk/client-go 中的 taints 列出不可调度的 kubernetes 节点
【发布时间】:2019-09-12 23:01:12
【问题描述】:

我正在尝试列出在 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,因为我不相信(除非我弄错了?)无论如何你都可以使用 fieldselector 和污点。

所以,我的问题是 - 如何有效地列出集群中所有受污染/不可调度的节点,特别是在使用 operator-sdk 时

更新:

我已经设法通过使用 v1meta.ListOptions 调用解决了字段选择器问题,如下所示:

nodes := &corev1.NodeList{}
opts := &client.ListOptions{
Raw: &metav1.ListOptions{
  FieldSelector: "spec.unschedulable=true",
  },
}

但是,我仍然不知道如何处理污点,因此我已编辑问题并将其保持打开状态

【问题讨论】:

    标签: kubernetes client-go


    【解决方案1】:

    根据控制器运行时文档,使用原始选项不能很好地处理缓存。

    相反,您应该在控制器初始化期间将所需字段添加到索引中:

    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。

    【讨论】:

      【解决方案2】:

      我正在使用 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
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-22
        • 2022-01-23
        • 2020-12-23
        • 2017-11-03
        • 2022-11-18
        • 1970-01-01
        • 2019-02-01
        • 2021-03-29
        • 2018-04-11
        相关资源
        最近更新 更多