forked from munnerz/kube-plex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (143 loc) · 4.29 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/chrispyduck/kube-plex/pkg/signals"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
var namespace = os.Getenv("KUBE_NAMESPACE")
var currentPodName = os.Getenv("KUBE_POD_NAME")
var pmsInternalAddress = os.Getenv("PMS_INTERNAL_ADDRESS")
func main() {
args := os.Args
rewriteArgs(args)
cfg, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
log.Fatalf("Error building kubeconfig: %s", err)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatalf("Error building kubernetes clientset: %s", err)
}
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting working directory: %s", err)
}
currentPod, err := kubeClient.CoreV1().Pods(namespace).Get(context.TODO(), currentPodName, metav1.GetOptions{})
if err != nil {
log.Fatalf("Error getting current pod: %s", err)
}
pod := generatePod(currentPod, cwd, args)
createdPod, err := kubeClient.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
if err != nil {
log.Fatalf("Error creating pod: %s", err)
}
log.Printf("Created pod: %s", createdPod.GetName())
stopCh := signals.SetupSignalHandler()
waitFn := func() <-chan error {
stopCh := make(chan error)
go func() {
stopCh <- waitForPodCompletion(kubeClient, pod)
}()
return stopCh
}
select {
case err := <-waitFn():
if err != nil {
log.Printf("Error waiting for pod to complete: %s", err)
}
case <-stopCh:
log.Printf("Exit requested.")
}
log.Printf("Cleaning up pod...")
err = kubeClient.CoreV1().Pods(namespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{})
if err != nil {
log.Fatalf("Error cleaning up pod: %s", err)
}
}
func rewriteArgs(in []string) {
for i, v := range in {
switch v {
case "-progressurl", "-manifest_name", "-segment_list":
in[i+1] = strings.Replace(in[i+1], "http://127.0.0.1:32400", pmsInternalAddress, 1)
case "-loglevel", "-loglevel_plex":
in[i+1] = "debug"
}
}
}
func generatePod(currentPod *corev1.Pod, cwd string, args []string) *corev1.Pod {
//envVars := toCoreV1EnvVar(env)
log.Printf("Creating pod to run command: %s", strings.Join(args, " "))
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: currentPod.ObjectMeta.Labels["app"] + "-transcoder-",
},
Spec: corev1.PodSpec{
NodeSelector: map[string]string{
"beta.kubernetes.io/arch": "amd64",
},
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "plex",
Command: args,
Image: currentPod.Spec.Containers[0].Image,
Env: currentPod.Spec.Containers[0].Env,
WorkingDir: cwd,
VolumeMounts: []corev1.VolumeMount{},
},
},
Volumes: []corev1.Volume{},
},
}
for _, currentPodVolumeMount := range currentPod.Spec.Containers[0].VolumeMounts {
if currentPodVolumeMount.Name != "shared" && !strings.HasPrefix(currentPodVolumeMount.Name, "kube") {
log.Printf("Adding volume mount for %s", currentPodVolumeMount.Name)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, currentPodVolumeMount)
}
}
for _, currentPodVolume := range currentPod.Spec.Volumes {
if currentPodVolume.Name != "shared" && !strings.HasPrefix(currentPodVolume.Name, "kube") {
log.Printf("Adding volume for %s", currentPodVolume.Name)
pod.Spec.Volumes = append(pod.Spec.Volumes, currentPodVolume)
}
}
return pod
}
func toCoreV1EnvVar(in []string) []corev1.EnvVar {
out := make([]corev1.EnvVar, len(in))
for i, v := range in {
splitvar := strings.SplitN(v, "=", 2)
out[i] = corev1.EnvVar{
Name: splitvar[0],
Value: splitvar[1],
}
}
return out
}
func waitForPodCompletion(cl kubernetes.Interface, pod *corev1.Pod) error {
for {
pod, err := cl.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if err != nil {
return err
}
switch pod.Status.Phase {
case corev1.PodPending:
case corev1.PodRunning:
case corev1.PodUnknown:
log.Printf("Warning: pod %q is in an unknown state", pod.Name)
case corev1.PodFailed:
return fmt.Errorf("pod %q failed", pod.Name)
case corev1.PodSucceeded:
return nil
}
time.Sleep(1 * time.Second)
}
}