gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.47k stars 1.32k forks source link

Error loading api client config #1400

Closed JCzz closed 5 months ago

JCzz commented 5 months ago

Describe the bug I am trying to run example code.

To Reproduce Just run: kubernetes_basic_example_test.go

Expected behavior I was hoping for the example to work.

Nice to have


**Versions**
- Terratest version: v0.46.13

MacOS
go version
go version go1.21.7 darwin/arm64

Same On Windows 11 WSL
go version go1.21.3 linux/amd64

**Additional context**
Add any other context about the problem here.

I can use "client-go" with my current-context in "~/.kube/config"

package main

import ( "context" "fmt" "os" "path/filepath"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

)

func main() { userHomeDir, err := os.UserHomeDir() if err != nil { fmt.Printf("error getting user home dir: %v\n", err) os.Exit(1) } kubeConfigPath := filepath.Join(userHomeDir, ".kube", "config") fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath)

kubeConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
    fmt.Printf("Error getting kubernetes config: %v\n", err)
    os.Exit(1)
}

clientset, err := kubernetes.NewForConfig(kubeConfig)

if err != nil {
    fmt.Printf("error getting kubernetes config: %v\n", err)
    os.Exit(1)
}
// An empty string returns all namespaces
namespace := "kube-system"
pods, err := ListPods(namespace, clientset)
if err != nil {
    fmt.Println(err.Error)
    os.Exit(1)
}
for _, pod := range pods.Items {
    fmt.Printf("Pod name: %v\n", pod.Name)
}
var message string
if namespace == "" {
    message = "Total Pods in all namespaces"
} else {
    message = fmt.Sprintf("Total Pods in namespace `%s`", namespace)
}
fmt.Printf("%s %d\n", message, len(pods.Items))

//ListNamespaces function call returns a list of namespaces in the kubernetes cluster
namespaces, err := ListNamespaces(clientset)
if err != nil {
    fmt.Println(err.Error)
    os.Exit(1)
}
for _, namespace := range namespaces.Items {
    fmt.Println(namespace.Name)
}
fmt.Printf("Total namespaces: %d\n", len(namespaces.Items))

}

func ListPods(namespace string, client kubernetes.Interface) (*v1.PodList, error) { fmt.Println("Get Kubernetes Pods") pods, err := client.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{}) if err != nil { err = fmt.Errorf("error getting pods: %v\n", err) return nil, err } return pods, nil }

func ListNamespaces(client kubernetes.Interface) (*v1.NamespaceList, error) { fmt.Println("Get Kubernetes Namespaces") namespaces, err := client.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{}) if err != nil { err = fmt.Errorf("error getting namespaces: %v\n", err) return nil, err } return namespaces, nil }


go mod tidy
go run ./main.go
denis256 commented 5 months ago

Hi, I suspect that path to config file contains ":"

log message:

client.go:45: Configuring Kubernetes client using config file /Users/<name>/.kube/config: with context 

contains ":", and it is not added from logs:

logger.Logf(t, "Configuring Kubernetes client using config file %s with context %s", kubeConfigPath, options.ContextName)

https://github.com/gruntwork-io/terratest/blob/master/modules/k8s/client.go#L45

JCzz commented 5 months ago

Thanks @denis256 That was indeed the case. Way back I created this:

export KUBECONFIG=~/.kube/config:$(find ~/.kube/custom -type f | tr '\n' ':')

Trying to make it easier to add new config files.