/
k8sexp.go
107 lines (96 loc) · 2.67 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
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"k8s.io/client-go/kubernetes"
apiUnver "k8s.io/client-go/pkg/api/unversioned"
api "k8s.io/client-go/pkg/api/v1"
batchapi "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(api.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 := &batchapi.Job{
TypeMeta: apiUnver.TypeMeta{
Kind: "Job",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: "k8sexp-testjob",
Labels: make(map[string]string),
},
Spec: batchapi.JobSpec{
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Name: "k8sexp-testpod",
Labels: make(map[string]string),
},
Spec: api.PodSpec{
InitContainers: []api.Container{}, // Doesn't seem obligatory(?)...
Containers: []api.Container{
{
Name: "k8sexp-testimg",
Image: "perl",
Command: []string{"sh", "-c", "echo hej > /k8sexp-data/hej.txt"},
SecurityContext: &api.SecurityContext{
Privileged: &falseVal,
},
ImagePullPolicy: api.PullPolicy(api.PullIfNotPresent),
Env: []api.EnvVar{},
VolumeMounts: []api.VolumeMount{
api.VolumeMount{
Name: "k8sexp-testvol",
MountPath: "/k8sexp-data",
},
},
},
},
RestartPolicy: api.RestartPolicyOnFailure,
ImagePullSecrets: []api.LocalObjectReference{},
Volumes: []api.Volume{
api.Volume{
Name: "k8sexp-testvol",
VolumeSource: api.VolumeSource{
HostPath: &api.HostPathVolumeSource{
Path: "/data",
},
},
},
},
},
},
},
}
newJob, err := jobsClient.Create(batchJob)
check(err)
fmt.Println("New job name: ", newJob.Name)
}
func check(err error) {
if err != nil {
panic(err)
}
}