azure - K8S 提供 kubeconfig 作为字节数组 genericclioptions

标签 azure go kubernetes google-cloud-platform

有什么方法可以将 kubeconfig 的 bytearray 传递给 k8s 客户端的 &genericclioptions 吗?

https://pkg.go.dev/k8s.io/cli-runtime/pkg/genericclioptions

目前,genericclioptionsits 默认情况下从文件的 env/path 获取它,但我需要将其显式传递为 bytearray 作为参数,而不是引用文件或环境,有办法做到吗?

我尝试了以下方法,但不起作用:

package main

import (
    "bytes"
    "github.com/spf13/pflag"
    "k8s.io/cli-runtime/pkg/genericclioptions"
)

func main() {
    kubeconfigData := []byte("my_kubeconfig")

    // create a new flag set and bind the kubeconfig flag
    flags := pflag.NewFlagSet("default", pflag.ExitOnError)
    kubeconfigFlag := flags.String("kubeconfig", "", "absolute path to the kubeconfig file")
    flags.Parse([]string{})

    // set the kubeconfig flag to the data in the byte array
    *kubeconfigFlag = "-"
    configOverrides := &genericclioptions.ConfigOverrides{ClusterDefaults: genericclioptions.NewClusterDefaults()}
    configLoader := genericclioptions.NewConfigFlags(false).WithOverrideFlags(configOverrides)
    configLoader.KubeConfig = bytes.NewBuffer(kubeconfigData)

    // ...
}

有什么想法吗?

第二个TRY 我还尝试了以下版本,但出现错误:加载配置文件时出错,有什么想法吗?

func main() {
    // read the kubeconfig file into a byte array
    kubeconfig, err := ioutil.ReadFile("./kubeconfig--pln.yaml")
    if err != nil {
        panic(err)
    }

    // create a ConfigFlags object with the kubeconfig byte array
    flags := genericclioptions.NewConfigFlags(false)
    *flags.KubeConfig = string(kubeconfig)
    dc, err := flags.ToDiscoveryClient()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(dc)

    res, err := dc.ServerPreferredNamespacedResources()
    if err != nil {
        fmt.Println(res)
    }

    // use the flags object to create a clientset or other Kubernetes API object
    // ...
}

最佳答案

您发布的代码试图将 kubeconfig 标志设置为字符串值“-”而不是字节数组。要将 kubeconfig 作为字节数组传递,您需要将 ConfigLoader 的 KubeConfig 字段设置为从字节数组创建的 bytes.Buffer。

这是代码的更新版本:

package main

import (
    "bytes"
    "github.com/spf13/pflag"
    "k8s.io/cli-runtime/pkg/genericclioptions"
)

func main() {
    kubeconfigData := []byte("my_kubeconfig")

    configOverrides := &genericclioptions.ConfigOverrides{ClusterDefaults: genericclioptions.NewClusterDefaults()}
    configLoader := genericclioptions.NewConfigFlags(false).WithOverrideFlags(configOverrides)
    configLoader.KubeConfig = bytes.NewBuffer(kubeconfigData)

    // ...
}

尝试一次上面的代码,你也可以按照这些link1 Link2 Link3 Link4

关于azure - K8S 提供 kubeconfig 作为字节数组 genericclioptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75684722/

相关文章:

c# - 如何设置环境变量并在Azure 2.5的IIS应用程序中使用它们

windows - 如何在 Go 中管理 Windows 用户帐户?

go - 创建GPA计算器时遇到问题

go - Golang中如何导入KFServing客户端包

kubernetes - serviceaccount 中不会自动生成可挂载的 key 和 token

azure - 从 Azure 上的 Linux 容器运行 Google Cloud Vision

azure - 如何修复删除 vhd/磁盘时 Microsoft azure 中的内部服务错误?

azure - 入口规则无法解析 Azure AKS 上的后端服务器 IP 地址

Go: go run file.go 是如何工作的

php - virtctl 在通过命令行执行时有效,但在 php exec() 中无效