-
Notifications
You must be signed in to change notification settings - Fork 191
/
main.go
199 lines (178 loc) · 4.43 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/munnerz/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"
)
// data pvc name
var dataPVC = os.Getenv("DATA_PVC")
// config pvc name
var configPVC = os.Getenv("CONFIG_PVC")
// transcode pvc name
var transcodePVC = os.Getenv("TRANSCODE_PVC")
// pms namespace
var namespace = os.Getenv("KUBE_NAMESPACE")
// image for the plexmediaserver container containing the transcoder. This
// should be set to the same as the 'master' pms server
var pmsImage = os.Getenv("PMS_IMAGE")
var pmsInternalAddress = os.Getenv("PMS_INTERNAL_ADDRESS")
func main() {
env := os.Environ()
args := os.Args
rewriteEnv(env)
rewriteArgs(args)
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting working directory: %s", err)
}
pod := generatePod(cwd, env, 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)
}
pod, err = kubeClient.CoreV1().Pods(namespace).Create(pod)
if err != nil {
log.Fatalf("Error creating pod: %s", err)
}
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(pod.Name, nil)
if err != nil {
log.Fatalf("Error cleaning up pod: %s", err)
}
}
// rewriteEnv rewrites environment variables to be passed to the transcoder
func rewriteEnv(in []string) {
// no changes needed
}
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(cwd string, env []string, args []string) *corev1.Pod {
envVars := toCoreV1EnvVar(env)
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "pms-elastic-transcoder-",
},
Spec: corev1.PodSpec{
NodeSelector: map[string]string{
"beta.kubernetes.io/arch": "amd64",
},
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "plex",
Command: args,
Image: pmsImage,
Env: envVars,
WorkingDir: cwd,
VolumeMounts: []corev1.VolumeMount{
{
Name: "data",
MountPath: "/data",
ReadOnly: true,
},
{
Name: "config",
MountPath: "/config",
ReadOnly: true,
},
{
Name: "transcode",
MountPath: "/transcode",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "data",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: dataPVC,
},
},
},
{
Name: "config",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: configPVC,
},
},
},
{
Name: "transcode",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: transcodePVC,
},
},
},
},
},
}
}
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(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)
}
}