如何在Amazon EKS服务上使用Kubernetes Go客户端?

huangapple go评论89阅读模式
英文:

How to use kubernetes go-client on amazon eks service?

问题

我已经很长时间在寻找文档,但仍然找不到清晰的连接过程。我想到了这个代码示例:

package aws

import (
	"fmt"
	"net/http"

	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/eks"
	"github.com/joho/godotenv"
)

func Connect() {
	godotenv.Load(".env")
	session := session.Must(session.NewSession())
	svc := eks.New(session)
	clusters, err := svc.ListClusters(&eks.ListClustersInput{})
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(clusters)
}

我的意思是,由于环境变量混乱,这仍然返回403禁止错误,但我认为代码是有效的。我的问题是,一旦建立了这个连接,如何将svc变量转换为go驱动程序中的*kubernetes.Clientset变量?

英文:

I've been looking for documentation for a long time and still couldn't find any clear connection procedure.
I came up with this code sample :

package aws

import (
	"fmt"
	"net/http"

	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/eks"
	"github.com/joho/godotenv"
)
func Connect() {
	godotenv.Load(".env")
	session := session.Must(session.NewSession())
	svc := eks.New(session)
	clusters, err := svc.ListClusters(&eks.ListClustersInput{})
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(clusters)
}

i mean, this still returns a 403 forbidden error because of env variable mess, but the code is valid i guess. My question is, having this connection established : how to convert this svc variable into the *kubernetes.Clientset one from the go driver ?

答案1

得分: 3

你是否查看了关于如何在集群中进行身份验证的client-go示例

通常,用于对Kubernetes API进行身份验证的代码会像这样开始:

	// 创建集群内配置
	config, err := rest.InClusterConfig()
	if err != nil {
		panic(err.Error())
	}
	// 创建客户端集
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}
英文:

Have you had a look at the client-go example on how to authenticate in-cluster?

Code that authenticate to the Kubernetes API typically start like this:

	// creates the in-cluster config
	config, err := rest.InClusterConfig()
	if err != nil {
		panic(err.Error())
	}
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

答案2

得分: 0

我使用以下代码来自动检测它是在本地机器上运行还是在任何 Kubernetes 集群上运行。

var config *rest.Config
if _, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token"); err == nil {
    config, err = rest.InClusterConfig()
    if err != nil {
        log.Fatal(err)
    }
} else if os.IsNotExist(err) {
    config, err = clientcmd.BuildConfigFromFlags("", *kubeConfig)
    if err != nil {
        log.Fatal("未挂载服务帐户或未传递 -kubeconfig 标志或 .kube/config 文件\n", err)
    }
}
// 创建一个不针对特定 API 版本的 REST 客户端
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
    log.Fatal(err)
}

请注意,这是代码的翻译版本,不包括任何其他内容。

英文:

I use the following code to automatically detect where its running from local machine or any kubernetes cluster.

var config *rest.Config
	if _, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token"); err == nil {
		config, err = rest.InClusterConfig()
		if err != nil {
			log.Fatal(err)
		}
	} else if os.IsNotExist(err) {
		config, err = clientcmd.BuildConfigFromFlags("", *kubeConfig)
		if err != nil {
			log.Fatal("No serviceaccount mounted or -kubeconfig flag passed or .kube/config file \n " ,err)
		}
	}
	// Create an rest client not targeting specific API version
	clientSet, err := kubernetes.NewForConfig(config)
	if err != nil {
		log.Fatal(err)
	}

答案3

得分: 0

这篇帖子对于你如果有一个工作流程来从aws eks update-kubeconfig cluster_name --kubeconfig=/my/path/file获取kubeconfig,然后将kubeconfig传递给kubectl --kubeconfig=/my/path/file,现在想要使用aws sdk和go k8s客户端库来完成相同的操作会很有用。

英文:

This post will be useful if you had a workflow to get kubeconfig from aws eks update-kubeconfig cluster_name --kubeconfig=/my/path/file and then pass the kubeconfig to kubectl --kubeconfig=/my/path/file and now want to do the same using aws sdk and go k8s client library.

huangapple
  • 本文由 发表于 2021年8月30日 13:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/68979323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定