-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
77 lines (72 loc) · 1.88 KB
/
util.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
package main
import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PodOptions struct {
image string
namespace string
pvc corev1.PersistentVolumeClaim
cmd []string
}
// Finds if a pod that attached to a PVC
func findPodByPVC(podList corev1.PodList, pvc corev1.PersistentVolumeClaim) *corev1.Pod {
for _, pod := range podList.Items {
for _, volume := range pod.Spec.Volumes {
if volume.PersistentVolumeClaim != nil && volume.PersistentVolumeClaim.ClaimName == pvc.Name {
return &pod
}
}
}
return nil
}
// Returns a job for the get command.
// func buildPvcbGetJob(namespace string, image string, pvc corev1.PersistentVolumeClaim) *batchv1.Job {
func buildPvcbGetJob(options PodOptions) *batchv1.Job {
TTLSecondsAfterFinished := new(int32)
*TTLSecondsAfterFinished = 10
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "pvcb-edit-" + options.pvc.Name,
Namespace: options.namespace,
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: TTLSecondsAfterFinished,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "pvcb-edit",
},
Spec: corev1.PodSpec{
RestartPolicy: "Never",
Containers: []corev1.Container{
{
Name: "pvcb-edit",
Image: image,
//Command: []string{"/bin/bash", "-c", "--"},
Command: options.cmd,
Args: []string{"/entrypoint.sh"},
VolumeMounts: []corev1.VolumeMount{
{
Name: "target-pvc",
MountPath: "/mnt",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "target-pvc",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: options.pvc.Name,
},
},
},
},
},
},
},
}
return job
}