Last active
March 24, 2019 23:05
-
-
Save pracps/1dd154facf08c44294e1066086d43997 to your computer and use it in GitHub Desktop.
Create Job on kubernetes cluster with go-client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"flag" | |
"path/filepath" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/util/homedir" | |
"k8s.io/client-go/tools/clientcmd" | |
corev1 "k8s.io/api/core/v1" | |
batchv1 "k8s.io/api/batch/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
) | |
func PrepareJob(jobName string) *batchv1.Job { | |
job := &batchv1.Job { | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: jobName, | |
}, | |
Spec: batchv1.JobSpec { | |
//Completions: &int32(3), | |
//Parallelism: &int32(3), | |
Template: corev1.PodTemplateSpec { | |
Spec: corev1.PodSpec { | |
Containers: []corev1.Container { | |
{ | |
Name: "pi", | |
Image: "perl", | |
Command: []string {"perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"}, | |
Env: []corev1.EnvVar { | |
{ | |
Name: "EVENT_NAME", | |
Value: "BLASTPRO SERIES", | |
}, | |
} | |
}, | |
}, | |
RestartPolicy: "Never", | |
}, | |
}, | |
}, | |
} | |
return job | |
} | |
func CreateJob(jobName string) { | |
var kubeconfig *string | |
if home := homedir.HomeDir(); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.Parse() | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err) | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err) | |
} | |
jobClient := clientset.BatchV1().Jobs(corev1.NamespaceDefault) | |
job := PrepareJob(jobName) | |
fmt.Println("Creating job...") | |
result, err := jobClient.Create(job) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Created job %q.\n", result.GetObjectMeta().GetName()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment