如何向 KubeBuilder 的 List 方法的 `ListOptions` 提供 Owner Reference?

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

How to provide Owner Reference to the `ListOptions` of KubeBuilder's List method?

问题

我将为您翻译以下内容:

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

type ListOptions struct {
	// LabelSelector通过标签筛选结果。使用labels.Parse()将原始字符串设置为标签选择器。
	LabelSelector labels.Selector
	// FieldSelector通过特定字段筛选结果。为了在基于缓存的实现中使用此功能,请将使用限制为已添加到索引器的单个字段-值对。
	FieldSelector fields.Selector

	// Namespace表示要列出的命名空间,对于非命名空间对象为空,或者跨所有命名空间进行列出。
	Namespace string

	// Limit指定从服务器返回的最大结果数。服务器可能不支持此字段的所有资源类型,但如果支持并且还有更多结果,则会在返回的列表对象上设置继续字段。如果在原始ListOptions中watch为true,则不支持此字段。
	Limit int64
	// Continue是服务器返回的令牌,它允许客户端通过指定限制来从服务器检索结果的块。服务器可能会拒绝不识别的继续令牌的请求,并且如果令牌因过期而无法再使用,则返回410错误。如果在原始ListOptions中watch为true,则不支持此字段。
	Continue string

	// Raw表示原始的ListOptions,如传递给API服务器。请注意,并非所有接口实现都会遵守这些选项,而且将忽略LabelSelector、FieldSelector、Limit和Continue字段。
	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)
英文:

I want to list the pods that are owned by the resource X from the Kubernetes cluster using Kubuilder's List(ctx context.Context, list ObjectList, opts ...ListOption) method. ListOptions contains options for limiting or filtering results. Here is the the structure of the 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
}

Now, How can I provide the owner information to this ListOptions so the List method will only list the pods that are owned by X?

Here is an example from the KubeBuilder book that shows how to filter results by a particular field,

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

答案1

得分: 1

很抱歉,对于资源的每个字段都无法使用字段选择器。例如,在您的情况下,您只能使用这些字段作为字段选择器。这也在这个线程中有说明。

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

英文:

Unfortunately it's not possible to use field selector for every field of a resource. In your case for example, you can only use these fields as field selector. It's also stated in this thread.

Alternatively, you can put labels to pods that is owned by a custom resource and use label selectors. Or you can get all pods and apply programmatic filter to get necessary pods. (I recommend the first approach since metadata.ownerReferences is an array and the cost is O(n^2))

huangapple
  • 本文由 发表于 2023年1月13日 03:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75101179.html
匿名

发表评论

匿名网友

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

确定