0

我正在尝试将环境变量替换为 .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)

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

谁能解释这种奇怪的行为?接收标准输入是 bash 问题还是 kubectl 问题?

破解解决方案

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

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

……但感觉很丑。

4

0 回答 0