当从 Python subprocess.run 调用 kubectl 并通过 env 参数传递 KUBECONFIG 变量时,我在 Windows 环境中收到以下错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 526, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'kubectl get ns' returned non-zero exit status 1.
我正在运行以下代码。它在 Linux 终端上工作得很好,但在 Windows 上却不行。我在想这是否是因为操作系统路径在 Windows 中的编写方式不同,但我无法让它在 Windows 中工作。我得到的错误也粘贴在下面。有人可以帮忙吗?
import subprocess
env_var = {'KUBECONFIG': '/home/myhome/.kube/config'} #For Linux
env_var = {'KUBECONFIG': 'C:\\Users\\myhome\\.kube\\config'} #For Windows
output = subprocess.run('kubectl get ns', shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env_var)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 526, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'kubectl get ns' returned non-zero exit status 1.
如果我将附加环境变量(即:KUBECONFIG)附加到现有环境变量中,它就会起作用。问题是,如果没有设置某些环境变量,KUBECONFIG 文件中的主机名就无法解决。