我想使用 Kubuilder 的 List(ctx context.Context, list ObjectList, opts ...ListOption) 列出 Kubernetes 集群中资源
方法。 X
拥有的 podListOptions
包含用于限制或过滤结果的选项。这是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/