英文:
Piping into kubectl.exe
问题
I am trying to run a command like this:
kubectl get namespaces -o custom-columns=:metadata.name --no-headers
| Where-Object{$_.StartsWith("test") -or $_.StartsWith("dev")}
| kubectl get namespaces
(NOTE: the line breaks are just for readability, there are no line breaks in my actual command.)
The end goal is to apply a label to a subset of namespaces, but for now, I am just trying to feed the filtered piped list of spaces back in to kubectl
so it will get
them.
But I can't seem to get kubectl
to accept the piped list as the list of namespaces to return. (this just returns all the namespaces on my cluster).
How can I get kubectl
to take the pipe as input? (So I can filter the list of namespaces.)
英文:
I am trying to run a command like this:
kubectl get namespaces -o custom-columns=:metadata.name --no-headers
| Where-Object{$_.StartsWith("test") -or $_.StartsWith("dev")}
| kubectl get namespaces
(NOTE: the line breaks are just for readability, there are no line breaks in my actual command.)
The end goal is to apply a label to a subset of namespaces, but for now, I am just trying to feed the filtered piped list of spaces back in to kubectl
so it will get
them.
But I can't seem to get kubectl
to accept the piped list as the list of namespaces to return. (this just returns all the namespaces on my cluster).
How can I get kubectl
to take the pipe as input? (So I can filter the list of namespaces.)
答案1
得分: 1
The kubectl
docs没有提到管道(stdin)输入,所以可能不受支持。
您提到,通过将名称列表以空格分隔的方式追加到 kubectl get namespaces
可以实现传递多个名称 作为参数。
因此,您可以简单地将名称收集到一个 数组 中 - 当您将外部程序调用的输出收集到变量中时,PowerShell 会自动执行此操作,每个输出 行 都成为其自己的数组元素<sup>[1]</sup> - 并将其用作参数:
$namespaces =
kubectl get namespaces -o custom-columns=:metadata.name --no-headers |
Where-Object { $_.StartsWith("test") -or $_.StartsWith("dev") }
kubectl get namespaces $namespaces
PowerShell 自动将数组的元素作为 单独的(字符串化的)参数传递给 外部程序。
<sup>[1] 如果恰好只有 一行 输出,它将按原样收集,即作为 [string]
而不是包含该字符串的单一元素数组。但是,在这种情况下该方法仍然有效。如果您明确需要一个数组,请使用类似 [array] $namespaces = ...
的方法。</sup>
英文:
<!-- language-all: sh -->
The kubectl
docs make no mention of pipeline (stdin) input, so it may not be supported.
You state that passing multiple names as arguments can be achieved with appending a space-separated list of names to kubectl get namespaces
Therefore, you can simply collect the names in an array - which PowerShell automatically does when you collect an external-program call's output in a variable, with each output line becoming its own array element<sup>[1]</sup> - and use it as an argument:
$namespaces =
kubectl get namespaces -o custom-columns=:metadata.name --no-headers |
Where-Object { $_.StartsWith("test") -or $_.StartsWith("dev") }
kubectl get namespaces $namespaces
PowerShell automatically passes an array's elements as individual, (stringified) arguments to external programs.
<sup>[1] If there happens to be just one line of output, it is collected as-is, i.e., as a [string]
rather than a single-element array containing that string. However, the approach still works in that case. If you explicitly do need an array, use something like [array] $namespaces = ...
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论