如何遍历多文档 YAML 文件以通过管道传输到命令?

时间:2021-06-06 21:36:06

标签: bash kubernetes yaml

我有一个命令可以生成一个包含多个由 yaml 分隔的 --- 文档的机密文件,其输出如下所示:

## THE COMMAND THAT RUNS
kubectl kustomize .

## SENDS THE FOLLOWING TO STDOUT
data:
  firebase-dev-credentials.json: |
    MY_BASE_64_CREDENTIALS
kind: Secret
metadata:
  name: nodejs-firebase-credentials-989t6dg7m2
type: Opaque
---
apiVersion: v1
data:
  AGENDA_DATABASE_URL: |
    MY_BASE_64_URL
kind: Secret
metadata:
  name: nodejs-secrets-c229fkh579
type: Opaque

现在,我必须将 yaml 文档通过管道传输到另一个命令进行处理,例如:

kubectl kustomize . | kubeseal

但是,kubeseal 命令不接受多文档 yaml。所以我想我需要用 yaml 分割 --- 并将每个单独的文档发送到 kubeseal 命令?

使用 bash,完成此任务的首选方法是什么?

例如,我想我不能做这样简单的事情,因为 stdout 是多行的?

export IFS=";"
sentence="one;two;three"
for word in $sentence; do
  echo "$word"
done

3 个答案:

答案 0 :(得分:2)

您可以通过 export default function PostsContainer() { let {categoryid} = useParams<any>(); const [posts, setPosts] = useState<any[]>([]); const [page, setPage] = useState<number>(1); const [totPages, setTotPages] = useState<number>(1); const [title, setTitle] = useState<string>("Recent posts"); const baseUrl = BASE_URL + "wp-json/wp/v2"; const [loadingPosts, setLoadingPosts] = useState<boolean>(false); useEffect(() => { async function loadPosts() { setLoadingPosts(false) let url = baseUrl + "/posts?status=publish&page=" + page; if (categoryid !== undefined) { url = url + "&categories=" + categoryid; getCategoryName(categoryid); } const response = await fetch(url); if (!response.ok) { return; } const totalPages = await response.headers.get("x-wp-totalpages"); const postsTemp = await response.json(); setPosts(postsTemp); setTotPages(Number(totalPages)); setLoadingPosts(true); console.log(posts) } loadPosts(); }, [page, categoryid]); function handleClickNextPage() { setPage(page + 1); } async function getCategoryName(id: number) { let url = baseUrl + "/categories/" + id; const response = await fetch(url); if (!response.ok) { return; } const category = await response.json(); setTitle(category.name); } if (loadingPosts) { return ( <IonPage> <IonHeader translucent={true}> </IonHeader> <IonContent> {page === 1 ? <Slider listOfPosts={posts}/> : <div/>} <Posts listOfPosts={posts} totPages={totPages} handleClickNextPage={handleClickNextPage} pageNumber={page} /> </IonContent> </IonPage> ); } else { return <IonLoading isOpen={!loadingPosts} message={ION_LOADING_DIALOG} duration={ION_LOADING_DURATION} /> } } 管道选择文档 N。但实际上,您不会这样做。您将在 Kustomize 配置中包含已经密封的数据,这不是您想要自动运行的内容。

答案 1 :(得分:0)

如果只用 bash 解析,

x="$(kubectl kustomize .)"
while [[ -n "$x" ]]
do ary+=("${x%%---*}")
if [[ "$x" =~ --- ]]; then x="${x#*---}"; else x=''; fi
done
for yml in "${ary[@]}"; do kubeseal<<<"$yml"; done

答案 2 :(得分:-1)

每隔一段时间,可能会有不同的解决方案,但一种方法是:

IFS="" # retain front tabs
buf=""
while read line
do
    if [ "$line" == "---" ]
    then
        echo -e $buf | kubeseal
        buf=""
    else
        if [ "$buf" == "" ] # prevent empty first newline
        then
            buf="$line"
        else
            buf=$(echo "$buf\\n$line")
        fi
    fi
done < yml

# get last section
echo -e $buf | kubeseal
相关问题