在 golang 示例中执行 kubectl get po -o yaml 时出错

Error while exec kubectl get po -o yaml in golang example

如何在golang中获取kubectl get po -o yaml

它到运行这个代码 Go client example

预期输出是

./app
There are 3 pods in the cluster
There are 3 pods in the cluster
There are 3 pods in the cluster

但是当我 运行 我得到了

 go build -o app .
    ./app
panic: exec plugin: invalid apiVersion "client.authentication.k8s.io/v1alpha1"

goroutine 1 [running]:
main.main()
    /Users/padmanabanpr/Documents/client-go/examples/out-of-cluster-client-configuration/main.go:61 +0x5b6

这可能是因为您的 k8s client/server 版本高于 1.24+ https://github.com/aws/aws-cli/issues/6920 请参阅此以进一步说明

以下步骤解决了问题

  • 安装Kubectl客户端1.23.0-00(1.24.x有问题)
$ cd client-go/examples/out-of-cluster-client-configuration
$ go mod init example.com/m
$ go mod tidy
$ go build -o app .
$ ./app