Examples of accessing the Jobs API via the Go client?

2,142 views
Skip to first unread message
Assigned to samuel...@gmail.com by me

Samuel Lampa

unread,
Feb 11, 2017, 3:20:23 AM2/11/17
to Kubernetes user discussion and Q&A
Hi folks,

Are there any examples on, specifically, how to use the Go client to access the Jobs API? (from outside the cluster).

I found the basic example [1], and managed to list running pods in my minikube cluster, after I figured out where to find the cluster config file in minikube (~/.kube/config, for the record). I also found the code for the Jobs API [2], which definitely helps too. But I'm still pretty bewildered about how to use it to create new jobs, start them, list running jobs, poll for their finish etc etc.

Are there any code examples of accessing the jobs API via the go client ... even just some undocumented usage of the code inside some other app?

Just remember that it is the Jobs API specifically I'm interested in, as I got the basic example to work already (a lot of folks sent me examples which did not meet this requirement).

Samuel Lampa

unread,
Feb 11, 2017, 3:49:06 AM2/11/17
to Kubernetes user discussion and Q&A
Well, I forgot that I actually got a bit forward, and realized (thanks fatih's vim-go for the autocompletion), that I should be able to use clientset.Jobs() to access jobs ... but I still get an error.

So I get this code:

package main

import (
    "flag"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
)

var (
    kubeconfig = flag.String("kubeconfig", "/home/samuel/.kube/config", "absolute path to the kubeconfig file")
)

func main() {
    flag.Parse()

    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    check(err)

    clientset, err := kubernetes.NewForConfig(config)
    check(err)

    clientset.Jobs(clientset.Namespaces().Get("default"))
}

func check(err error) {
    if err != nil {
        panic(err)
    }
}

... but when I try to run it, I get:

$ go run k8sexp.go 
# command-line-arguments
./k8sexp.go:22: ambiguous selector clientset.Jobs

Any hints?

Best
// Samuel

Samuel Lampa

unread,
Feb 13, 2017, 3:25:44 AM2/13/17
to Kubernetes user discussion and Q&A
On Saturday, February 11, 2017 at 9:49:06 AM UTC+1, Samuel Lampa wrote:
Well, I forgot that I actually got a bit forward, and realized <snip>

And now I finally found an example which put me on the right track: https://github.com/gaia-docker/tugbot-kubernetes/blob/master/main.go#L107-L109

Based on pointers in this code, I have now managed to list some kind of Job info with this code: https://gist.github.com/samuell/5a1107577186c7c3f5db239ae1b2a5f7

So, I'm now progressing, and this issue and thread can be seen as solved.

Best
// Samuel

Samuel Lampa

unread,
Feb 14, 2017, 8:30:43 AM2/14/17
to Kubernetes user discussion and Q&A
On Monday, February 13, 2017 at 9:25:44 AM UTC+1, Samuel Lampa wrote:
And now I finally found an example which put me on the right track: https://github.com/gaia-docker/tugbot-kubernetes/blob/master/main.go#L107-L109

For the record, I found an even more to the point example, in the Pachyderm codebase now:


// Samuel 
Reply all
Reply to author
Forward
0 new messages