List Traefik ingress controllers using Go client for Kubernetes

9/6/2018

We have a little internal tool listing our Kubernetes services using Go client for Kubernetes.

Now we have added Traefik to map our services to meaningful dns names using ingress controllers. We see that there is an ingress lister extension in Go client for Kubernetes that can be used to list ingress controllers. However, we could not make it work.

Are there any Go client code snippets listing ingress controllers?

-- Maksim Sorokin
go
kubernetes
kubernetes-go-client
kubernetes-ingress
traefik-ingress

1 Answer

9/6/2018

What have you tried so far? Posting examples helps. Try the following arbitrary example to get you started:

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

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
            panic(err.Error())
    }
    for {
            ingressList, err := clientset.ExtensionsV1beta1().Ingresses("").List(metav1.ListOptions{})
            if err != nil {
                    // handle err
            }
            ingressCtrls := ingressList.Items
            if len(ingressCtrls) > 0 {
                    for _, ingress := range ingressCtrls {
                            fmt.Printf("ingress %s exists in namespace %s\n", ingress.Name, ingress.Namespace)
                    }
            } else {
                    fmt.Println("no ingress found")
            }
            time.Sleep(10 * time.Second)
    }
-- PhilipGough
Source: StackOverflow