-
Notifications
You must be signed in to change notification settings - Fork 2
/
k8sexp.go
99 lines (88 loc) · 2.6 KB
/
k8sexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"k8s.io/client-go/kubernetes"
k8sapiUnver "k8s.io/client-go/pkg/api/unversioned"
k8sapi "k8s.io/client-go/pkg/api/v1"
batchv1 "k8s.io/client-go/pkg/apis/batch/v1"
"k8s.io/client-go/tools/clientcmd"
)
var (
kubeconfig = flag.String("kubeconfig", "/home/samuel/.kube/config", "absolute path to the kubeconfig file")
trueVal = true
falseVal = false
)
func main() {
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
check(err)
clientset, err := kubernetes.NewForConfig(config)
check(err)
// Access jobs. We can't do it all in one line, since we need to receive the
// errors and manage thgem appropriately
batchClient := clientset.BatchV1Client
jobsClient := batchClient.Jobs("default")
piJob, err := jobsClient.Get("pi")
check(err)
fmt.Printf("piJob Name: %v\n", piJob.Name)
jobsList, err := jobsClient.List(k8sapi.ListOptions{})
check(err)
// Loop over all jobs and print their name
for i, job := range jobsList.Items {
fmt.Printf("Job %d: %s\n", i, job.Name)
}
// For an example of how to create jobs, see this file:
// https://github.com/pachyderm/pachyderm/blob/805e63/src/server/pps/server/api_server.go#L2320-L2345
batchJob := &batchv1.Job{
TypeMeta: k8sapiUnver.TypeMeta{
Kind: "Job",
APIVersion: "v1",
},
ObjectMeta: k8sapi.ObjectMeta{
Name: "k8sexp-testjob",
Labels: make(map[string]string),
},
Spec: batchv1.JobSpec{
// Optional: Parallelism:,
// Optional: Completions:,
// Optional: ActiveDeadlineSeconds:,
// Optional: Selector:,
// Optional: ManualSelector:,
Template: k8sapi.PodTemplateSpec{
ObjectMeta: k8sapi.ObjectMeta{
Name: "k8sexp-testpod",
Labels: make(map[string]string),
},
Spec: k8sapi.PodSpec{
InitContainers: []k8sapi.Container{}, // Doesn't seem obligatory(?)...
Containers: []k8sapi.Container{
{
Name: "k8sexp-testimg",
Image: "perl",
Command: []string{"sleep", "10"},
SecurityContext: &k8sapi.SecurityContext{
Privileged: &falseVal,
},
ImagePullPolicy: k8sapi.PullPolicy(k8sapi.PullIfNotPresent),
Env: []k8sapi.EnvVar{},
VolumeMounts: []k8sapi.VolumeMount{},
},
},
RestartPolicy: k8sapi.RestartPolicyOnFailure,
Volumes: []k8sapi.Volume{},
ImagePullSecrets: []k8sapi.LocalObjectReference{},
},
},
},
// Optional, not used by pach: JobStatus:,
}
newJob, err := jobsClient.Create(batchJob)
check(err)
fmt.Println("New job name: ", newJob.Name)
}
func check(err error) {
if err != nil {
panic(err)
}
}