英文:
How to "filter" and "format" namespaces based on the annotation
问题
I want to filter namespaces based on the annotation. I do not want namespace which has annotation as components\.gke\.io/layer=="addon"
.
我想根据注释来过滤命名空间。我不想要具有注释components\.gke\.io/layer=="addon"
的命名空间。
I tried kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="addon")].metadata.name}'
command and at least it gives me the result but it is not coming in normal table format, the way it comes when you execute kubectl get ns
.
我尝试了kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="addon")].metadata.name}'
命令,至少它给了我结果,但它不是以正常的表格格式呈现,就像执行kubectl get ns
时一样。
However, this is not what I want, I want the inverse of this, which means filtering out these GKE rows. I tried kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="")].metadata.name}'
but it doesn't work, it gives empty result.
然而,这不是我想要的,我想要的是这个的反向操作,也就是过滤掉这些GKE行。我尝试了kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="")].metadata.name}'
,但它不起作用,它返回空结果。
I tried to format but unable to create the desired command. Anyone has tried this before - (1.) filter out namespaces which has certain annotation (2.) format that output in table format.
我尝试进行格式化,但无法创建所需的命令。有人以前尝试过这个吗 - (1.)过滤具有特定注释的命名空间(2.)将输出格式化为表格格式。
英文:
I want to filter namespaces based on the annotation. I do not want namespace which has annotation as components\.gke\.io/layer=="addon"
.
I tried kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="addon")].metadata.name}'
command and at least it gives me the result but it is not coming in normal table format, the way it comes when you execute kubectl get ns
.
However, this is not what I want, I want the inverse of this, which means filtering out these GKE rows. I tried kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer=="")].metadata.name}'
but it doesn't work, it give empty result.
I tried to format but unable to create the desired command. Anyone has tried this before - (1.) filter out namespaces which has certain annotation (2.) format that output in table format.
答案1
得分: 3
I think (!?) you can't use negation with kubectl
's implementation of JSONPath.
使用kubectl
的JSONPath实现时,我认为您不能使用否定形式。
Once you use the --output
flag with kubectl
, you lose the ability to render the command's output using the default table format. With e.g. Bash column
can provide similar out.
一旦您在kubectl
中使用--output
标志,您就会失去使用默认表格格式渲染命令输出的能力。例如,使用Bash的 column
可以提供类似的输出。
Consider using a generic JSON processing tool such as jq
.
考虑使用通用的JSON处理工具,例如 jq
。
An advantage of using a standalone tool is that, once you're familiar with the tool, you can use it with any command that emits JSON (not just with kubectl
).
使用独立工具的优点是,一旦您熟悉了该工具,您可以将其与发出JSON的任何命令一起使用(不仅限于kubectl
)。
FILTER='
["NAME","STATUS"],
(.items[]|
[
select(.metadata.annotations["components.gke.io/layer"]!="addon")
|.metadata.name,.status.phase
]
)
|@tsv'
kubectl get namespaces --output=json \
| jq -r "${FILTER}" \
| column --table --separator $'\t'
FILTER
的解释:
- 返回一个JSON数组,它被传送到
@tsv
(制表符格式的输出) - 第一个数组条目是一个列标题数组
.items
被传送到一个select
,用于过滤不包含annotation
的命名空间- 作为JSON数组项发出未经过滤的命名空间名称和状态
由jq
发出的制表符格式(不再是JSON)然后被传送到column
,该工具将结果格式化为表格。
英文:
I think (!?) you can't use negation with kubectl
's implementation of JSONPath.
Once you use the --output
flag with kubectl
, you lose the ability to render the command's output using the default table format. With e.g. Bash column
can provide similar out.
Consider using a generic JSON processing tool such as jq
.
An advantage of using a standalone tool is that, once you're familiar with the tool, you can use it with any command that emits JSON (not just with kubectl
.
FILTER='
["NAME","STATUS"],
(.items[]|
[
select(.metadata.annotations["components.gke.io/layer"]!="addon")
|.metadata.name,.status.phase
]
)
|@tsv'
kubectl get namespaces --output=json \
| jq -r "${FILTER}" \
| column --table --separator $'\t'
Explanation of FILTER
:
- Returns a JSON array that is piped into
@tsv
(tab-formatted output) - The first array entry is a an array (!) of column titles
.items
is piped through aselect
that filters Namespaces that don't contain theannotation
- Emit not filtered Namespace name and status as JSON array items
The tab-formatted (no longer JSON) that's emitted by jq
is then piped into column
which formats the results as a table.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论