如何基于注释“筛选”和“格式化”命名空间。

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

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.

使用kubectlJSONPath实现时,我认为您不能使用否定形式。

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的解释:

  1. 返回一个JSON数组,它被传送到@tsv(制表符格式的输出)
  2. 第一个数组条目是一个列标题数组
  3. .items被传送到一个select,用于过滤不包含annotation的命名空间
  4. 作为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:

  1. Returns a JSON array that is piped into @tsv (tab-formatted output)
  2. The first array entry is a an array (!) of column titles
  3. .items is piped through a select that filters Namespaces that don't contain the annotation
  4. 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.

huangapple
  • 本文由 发表于 2023年5月21日 22:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300525.html
匿名

发表评论

匿名网友

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

确定