运行 kubectl patch --local 由于缺少配置而失败

Running kubectl patch --local fails due to missing config

我有一个 GitHub Actions 工作流程,可以替换部署清单中的值。我使用 kubectl patch --local=true 来更新图像。到目前为止,这曾经完美无缺地工作。今天,工作流程开始失败,出现 Missing or incomplete configuration info 错误。

我是 运行 kubectl,带有 --local 标志,因此不需要配置。有谁知道 kubectl 突然开始需要配置的原因是什么?我在 Kubernetes 中找不到任何有用的信息 GitHub 问题和谷歌搜索时间没有帮助。

GitHub 操作工作流程中失败步骤的输出:

Run: kubectl patch --local=true -f authserver-deployment.yaml -p '{"spec":{"template":{"spec":{"containers":[{"name":"authserver","image":"test.azurecr.io/authserver:20201230-1712-d3a2ae4"}]}}}}' -o yaml > temp.yaml && mv temp.yaml authserver-deployment.yaml

error: Missing or incomplete configuration info.  Please point to an existing, complete config file:


  1. Via the command-line flag --kubeconfig
  2. Via the KUBECONFIG environment variable
  3. In your home directory as ~/.kube/config

To view or setup config directly use the 'config' command.
Error: Process completed with exit code 1.

kubectl version 的输出:

Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", 
GitCommit:"ffd68360997854d442e2ad2f40b099f5198b6471", GitTreeState:"clean", 
BuildDate:"2020-11-18T13:35:49Z", GoVersion:"go1.15.0", Compiler:"gc", 
Platform:"linux/amd64"}

您还需要设置config to access kubernetes cluster。即使您在本地修改文件,您仍在执行 kubectl 命令,该命令必须对集群执行 运行。默认情况下,kubectl 在 $HOME/.kube 目录中查找名为 config 的文件。

error: current-context is not set 表示没有为集群设置当前上下文,无法对集群执行 kubectl。您可以使用 this tutorial.

为服务帐户创建上下文

作为一种解决方法,我安装了 kind(完成工作确实需要更长的时间,但至少它在工作,并且可以在以后用于 e2e 测试)。

添加了这一步:

- name: Setup kind
        run: kubectl version
        uses: engineerd/setup-kind@v0.5.0

同时使用 --dry-run=client 作为 kubectl 命令的选项。

我意识到这不是正确的解决方案。

导出 KUBERNETES_MASTER 环境变量应该可以解决问题:

$ export KUBERNETES_MASTER=localhost:8081  # 8081 port, just to ensure it works 

$ kubectl version

Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.8", GitCommit:"9f2892aab98fe339f3bd70e3c470144299398ace", GitTreeState:"clean", BuildDate
:"2020-08-13T16:12:48Z", GoVersion:"go1.13.15", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8081 was refused - did you specify the right host or port?

# Notice the port 8081 in the error message ^^^^^^

现在补丁也应该像往常一样工作了:

$ kubectl patch --local=true -f testnode.yaml -p '{"metadata":{"managedFields":[]}}'    # to get the file content use  -o yaml 
node/k8s-w1 patched

或者,您可以将 kubectl 更新到更高版本。 (即使没有技巧,v1.18.8 也能正常工作)

解释部分:

PR #86173 stop defaulting kubeconfig to http://localhost:8080

可能会引入更改

更改已在 Revert "stop defaulting kubeconfig to http://localhost:8080" #90243 for the later 18.x versions, see the issue kubectl --local requires a valid kubeconfig file #90074 中还原以获取详细信息

我最终使用 sed 将字符串替换为图像

  - name: Update manifests with new images
    working-directory: test/cloud
    run: |
      sed -i "s~image:.*$~image: ${{ steps.image_tags.outputs.your_new_tag }}~g" your-deployment.yaml

现在很有魅力。