如何找到 kubectl 支持的给定资源类型的字段选择器列表?

How can I find the list of field selectors supported by kubectl for a given resource type?

我最近了解了 kubectl --field-selector 标志,但是 运行 在尝试将它用于各种对象时出错。

例如:

$ kubectl delete jobs.batch --field-selector status.succeeded==1
Error from server (BadRequest): Unable to find "batch/v1, Resource=jobs" that match label selector "", field selector "status.succeeded==1": field label "status.succeeded" not supported for batchv1.Job

根据 the documentationSupported field selectors vary by Kubernetes resource type.,所以我想这种行为是意料之中的。

烦人的部分是我必须单独尝试每个字段才能知道我是否可以使用它们。

有没有办法获取给定资源类型/资源版本/kubectl 版本支持的所有字段?

郑重声明,虽然这没有回答问题,但可以使用 jsonPath 解决此限制。

比如上面的例子可以这样来做:

kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')

(解决方案灵感来自

你的问题是你错误地使用了status.succeeded而不是status.successful,所以正确的命令是

kubectl delete jobs.batch --field-selector status.successful==1
No resources found

关于您关于所有字段的问题:我的建议是深入代码并在 conversion.go 中为每个 API.

搜索合适的资源类型

示例: Batch Jobs conversion.go

    return scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("Job"),
        func(label, value string) (string, string, error) {
            switch label {
            case "metadata.name", "metadata.namespace", "status.successful":
                return label, value, nil
            default:
                return "", "", fmt.Errorf("field label %q not supported for batchv1.Job", label)
            }
        },
    )
}

以下是如何列出所有命名空间中的资源(例如“机器”类型),状态阶段不是 运行:

kubectl get Machine -A -o jsonpath='{.items[?(@.status.phase!="Running")].metadata.name}'

打印失败资源的错误消息

kubectl get Machine -A -o jsonpath='{.items[?(@.status.phase=="Failed")].status.errorMessage}'

输出示例:

nmanos-cluster-a-v5jcx-submariner-gw-us-west-1b-pwwrk: reconciler failed to Create machine: failed to launch instance: error launching instance: The requested configuration is currently not supported. Please check the documentation for supported configurations.

关于如何知道给定资源类型支持的字段选择器的问题。这可以通过使用以下命令为资源类型转储 yaml 或 json 配置来完成。

kubectl get pods -o yaml

以上命令给出了给定资源的所有字段。我们可以以此为参考,想出一个参考滤镜。