从管道输入到`kubectl`的奇怪行为

时间:2020-11-12 09:49:52

标签: bash environment-variables pipe kubectl envsubst

我正在尝试将环境变量替换为.yaml文件,并将结果文件传递给kubectl

> cat template.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: $K8S_NAMESPACE

> cat .env
K8S_NAMESPACE=leafsheets-staging-pi

> ( set -a; source .env;  envsubst < template.yaml | kubectl apply -f - )
error: You must be logged in to the server (the server has asked for the client to provide credentials)

为什么会出现此错误?

我从https://skofgar.ch/dev/2020/08/how-to-quickly-replace-environment-variables-in-a-file/那里获得了语法

调试

kubectl可以像这样正常工作:

> cat pure.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: leafsheets-staging-pi

> kubectl apply -f pure.yaml 
namespace/leafsheets-staging-pi created

> kubectl apply -f pure.yaml 
namespace/leafsheets-staging-pi unchanged

替换操作正常:

> envsubst < template.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: leafsheets-staging-pi

(这与pure.yaml相同)

但是命令失败:

> ( set -a; source .env;  envsubst < template.yaml | kubectl apply -f - )
error: You must be logged in to the server (the server has asked for the client to provide credentials)

现在原始的直接命令(以前起作用)也失败了:

> kubectl apply -f pure.yaml 
error: You must be logged in to the server (the server has asked for the client to provide credentials)

创建一个新的外壳,它将再次起作用。

有人可以解释这种奇怪的行为吗?接收stdin是bash问题还是kubectl问题?

破解方案

有什么好的解决方法?我目前正在做:

> cat template.yaml | envsubst > /tmp/foo.yaml
> kubectl apply -f /tmp/foo.yaml 

...但是感觉很丑。

0 个答案:

没有答案
相关问题