【发布时间】:2021-02-24 21:08:31
【问题描述】:
我想在 Kubernetes 中打印当前上下文的命名空间。
oc config view -o json 返回以下输出(为便于阅读而缩短)
{
"kind": "Config",
"apiVersion": "v1",
"contexts": [
{
"name": "pp2-review/master-ocp-internal-company-com:443/Philippe",
"context": {
"cluster": "master-ocp-internal-company-com:443",
"user": "Philippe",
"namespace": "pp2-review"
}
},
{
"name": "pp2-master/master-ocp-internal-company-com:443/Philippe",
"context": {
"cluster": "master-ocp-internal-company-com:443",
"user": "Philippe",
"namespace": "review"
}
}
],
"current-context": "pp2-review/master-ocp-internal-company-com:443/Philippe"
}
我想返回pp2-review。当前上下文的命名空间。
oc config view -o "jsonpath={\$.current-context}" 返回pp2-review/master-ocp-internal-company-com:443/Philippe。
oc config view -o "jsonpath={.contexts[?(@.name==\"pp2-review/master-ocp-internal-company-com:443/Philippe\")].context.namespace}" 返回pp2-review。
将它们组合成 oc config view -o "jsonpath={\$.contexts[?(@.name==\$.current-context)]}" 不会返回任何内容。
除了执行第一个命令并将其返回值放在第二个之外,还有其他方法吗?
【问题讨论】:
-
我不能尝试这个,但我认为这个路径快递应该可以工作:
$..[?(@.name==$.current-context)].context.namespace -
执行返回
error: error executing jsonpath "{$..[?(@.name==$.current-context)].context.namespace}": Error executing template: … is not array or slice and cannot be filtered. -
Welp,看来 kubectl 的 Golang JSONPath 实现不支持这个。
-
太糟糕了。感谢您的尝试。