go - 如何为 KubeBuilder 的 List 方法的 `ListOptions` 提供 Owner 引用?

标签 go kubernetes kubebuilder

我想使用 Kubuilder 的 List(ctx context.Context, list ObjectList, opts ...ListOption) 列出 Kubernetes 集群中资源 X 拥有的 pod方法。 ListOptions 包含用于限制或过滤结果的选项。这是ListOptions

的结构
type ListOptions struct {
    // LabelSelector filters results by label. Use labels.Parse() to
    // set from raw string form.
    LabelSelector labels.Selector
    // FieldSelector filters results by a particular field.  In order
    // to use this with cache-based implementations, restrict usage to
    // a single field-value pair that's been added to the indexers.
    FieldSelector fields.Selector

    // Namespace represents the namespace to list for, or empty for
    // non-namespaced objects, or to list across all namespaces.
    Namespace string

    // Limit specifies the maximum number of results to return from the server. The server may
    // not support this field on all resource types, but if it does and more results remain it
    // will set the continue field on the returned list object. This field is not supported if watch
    // is true in the Raw ListOptions.
    Limit int64
    // Continue is a token returned by the server that lets a client retrieve chunks of results
    // from the server by specifying limit. The server may reject requests for continuation tokens
    // it does not recognize and will return a 410 error if the token can no longer be used because
    // it has expired. This field is not supported if watch is true in the Raw ListOptions.
    Continue string

    // Raw represents raw ListOptions, as passed to the API server.  Note
    // that these may not be respected by all implementations of interface,
    // and the LabelSelector, FieldSelector, Limit and Continue fields are ignored.
    Raw *metav1.ListOptions
}

现在,我如何向此 ListOptions 提供所有者信息,以便 List 方法仅列出 X 拥有的 pod ?

这是 KubeBuilder 书中的一个示例,展示了如何按特定字段过滤结果,

  listOps := &client.ListOptions{
        FieldSelector: fields.OneTermEqualSelector(configMapField, configMap.GetName()),
        Namespace:     configMap.GetNamespace(),
  }
  err := r.List(context.TODO(), attachedConfigDeployments, listOps)

最佳答案

不幸的是,不可能对资源的每个字段使用字段选择器。例如,在您的情况下,您只能使用 these fields作为字段选择器。 this thread中也有说明。 .

或者,您可以将标签添加到自定义资源拥有的 Pod 中并使用标签选择器。或者,您可以获取所有 pod 并应用编程过滤器来获取必要的 pod。 (我推荐第一种方法,因为 metadata.ownerReferences 是一个数组,成本为 O(n^2))

关于go - 如何为 KubeBuilder 的 List 方法的 `ListOptions` 提供 Owner 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75101179/

相关文章:

kubernetes - kubernetes pod 的写时复制式内存重用?使 pod 生成速度更快、内存效率更高

kubebuilder - kubebuilder脚手架中的 "domain"是什么?

linux - 有的可执行文件在docker中运行后显示 "no such file or directory",有的则不然

c - 在 C90 中实现无溢出系统堆栈

multithreading - 内存映射文件的原子操作

kubernetes - Kubernetes集群从Google Container Engine中消失了

kubernetes - Istio VirtualService 和 Kubernetes Service 有什么区别?

go - 使用旧表中的架构和数据创建表

memory - Go 1.3 垃圾收集器没有将服务器内存释放回系统

go - 将 url.URL 与controller-gen一起使用