raphael.oester
raphael.oester

Reputation: 445

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

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 ?

Upvotes: 3

Views: 3829

Answers (3)

Gopi Palamalai
Gopi Palamalai

Reputation: 416

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.

Upvotes: 0

Amjad Hussain Syed
Amjad Hussain Syed

Reputation: 1040

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)
    }

Upvotes: 1

Jonas
Jonas

Reputation: 128807

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())
    }

Upvotes: 3

Related Questions