如何为 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)

不幸的是,不可能对资源的每个字段使用字段选择器。例如,在您的示例中,您只能将这些字段用作字段选择器。在这篇文章中也有说明。

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

最新更新