通过kubernetes命令循环

pod7payv  于 2023-04-20  发布在  Kubernetes
关注(0)|答案(2)|浏览(127)

我有一个test.txt文件,其中包含一堆kubernetes命令。它看起来像:

kubectl get cm -o yaml | grep "test"
kubectl get pods
kubectl describe pod xxx
.
.

bash脚本读取test.txt并循环通过每个应该执行的命令。然而,当我运行脚本时,它给出了以下错误。如何解决这个问题?
来自服务器的错误(NotFound):配置Map"|“没有找到
来自服务器的错误(NotFound):未找到configmaps "grep"
来自服务器的错误(NotFound):未找到configmaps """
以下是脚本:

in_file=test.txt

# Loop over each line
while read -r test_case; do
    "kubectl -n ${namespace} ${test_case}" 
    
done < "$in_file"
zf2sa74q

zf2sa74q1#

将bash脚本改为:

#!/bin/bash

cat test.txt | xargs -I {} bash -c {}

工作起来就像一种享受:

{"apiVersion":"v1","data":{"api-key":"abc123","database-url":"postgresql://localhost/mydatabase"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"example-config","namespace":"default"}}
    name: example-config
{"apiVersion":"v1","data":{"api-key":"abc123","database-url":"postgresql://localhost/mydatabase"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"example-config2","namespace":"default"}}
    name: example-config2
{"apiVersion":"v1","data":{"api-key":"abc123","database-url":"postgresql://localhost/mydatabase"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"example-config3","namespace":"default"}}
    name: example-config3

here得到的想法。我不确定它是否有效,但它确实有效:)
希望能帮上忙。

5uzkadbs

5uzkadbs2#

kubectl get cm -o yaml | grep "test"

应改为

kubectl get cm <your_cm_name> -o yaml | grep "test"

您缺少name参数,这就是为什么kubectl认为管道字符是configmap的名称。

相关问题