-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (77 loc) · 1.87 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
func run(ctx context.Context) error {
scheme := runtime.NewScheme()
batchv1.AddToScheme(scheme)
c, err := client.New(config.GetConfigOrDie(), client.Options{
Scheme: scheme,
})
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}
if err := c.Create(ctx, &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Name: "rebalancer-storage-job",
Namespace: "default",
Labels: map[string]string{
"app": "rebalancer-storage-job",
"app.kubernetes.io/rebalancer-reason": "recovery",
},
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "rebalancer-storage-job",
},
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "rebalancer-storage-job",
Image: "ubuntu:14.04",
Command: []string{
"/bin/sh",
"-c",
"printenv; echo $REASON; sleep 10s",
},
Env: []corev1.EnvVar{
{
Name: "REASON",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.labels['app.kubernetes.io/rebalancer-reason']",
},
},
},
},
},
},
},
},
},
}); err != nil {
return fmt.Errorf("failed to create object: %w", err)
}
return nil
}
func main() {
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}