Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions charts/kube-plex/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ spec:
# TODO: move this to a secret?
- name: PLEX_CLAIM
value: "{{ .Values.claimToken }}"
{{- if .Values.plexUser }}
- name: PLEX_UID
value: "{{ .Values.plexUser }}"
{{ end }}
{{- if .Values.plexGroup}}
- name: PLEX_GID
value: "{{ .Values.plexGroup }}"
{{ end }}
# kube-plex env vars
- name: PMS_INTERNAL_ADDRESS
value: http://{{ template "fullname" . }}:32400
Expand Down
6 changes: 6 additions & 0 deletions charts/kube-plex/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ claimToken: ""
# Set the timezone of the plex server
timezone: Europe/London

# Set the UID/GID of the plex user (For NFS deployments)
#plexUser: "1005"
#plexGroup: "8675309"

service:
type: ClusterIP
port: 32400
Expand Down Expand Up @@ -65,6 +69,8 @@ nodeSelector:

persistence:
transcode:
# Enable a persistent transcode directory
## enabled: true
# Optionally specify claimName to manually override the PVC to be used for
# the transcode directory. If claimName is specified, storageClass and size
# are ignored.
Expand Down
62 changes: 56 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/munnerz/kube-plex/pkg/signals"
"gopkg.in/alessio/shellescape.v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -31,16 +33,26 @@ var namespace = os.Getenv("KUBE_NAMESPACE")
var pmsImage = os.Getenv("PMS_IMAGE")
var pmsInternalAddress = os.Getenv("PMS_INTERNAL_ADDRESS")

// Whether or not to cleanup the generated pod (for debugging).
// Defaults to TRUE.
var cleanUpPod = os.Getenv("KUBE_PLEX_CLEANUP_POD")

// UID/GID settings, if configured
var plexUID = os.Getenv("PLEX_UID")
var plexGID = os.Getenv("PLEX_GID")

func main() {
env := os.Environ()
args := os.Args

rewriteEnv(env)
rewriteArgs(args)
args = rewriteArgs(args)

cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting working directory: %s", err)
}
log.Printf("Creating pod with transcode args: %s", args)
pod := generatePod(cwd, env, args)

cfg, err := clientcmd.BuildConfigFromFlags("", "")
Expand All @@ -56,6 +68,8 @@ func main() {
pod, err = kubeClient.CoreV1().Pods(namespace).Create(pod)
if err != nil {
log.Fatalf("Error creating pod: %s", err)
} else {
log.Printf("Pod '%s' created.", pod.Name)
}

stopCh := signals.SetupSignalHandler()
Expand All @@ -76,10 +90,12 @@ func main() {
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)
if cu, err := strconv.ParseBool(cleanUpPod); cu && err != nil {
log.Printf("Cleaning up pod...")
err = kubeClient.CoreV1().Pods(namespace).Delete(pod.Name, nil)
if err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to ignore IsNotFoundError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that. I'll change the logic to handle it.

log.Fatalf("Error cleaning up pod: %s", err)
}
}
}

Expand All @@ -88,7 +104,7 @@ func rewriteEnv(in []string) {
// no changes needed
}

func rewriteArgs(in []string) {
func rewriteArgs(in []string) []string {
for i, v := range in {
switch v {
case "-progressurl", "-manifest_name", "-segment_list":
Expand All @@ -97,6 +113,40 @@ func rewriteArgs(in []string) {
in[i+1] = "debug"
}
}

var args []string

// If UID/GID is set, we have to modify the local groups before the transcode is called.
if plexUID != "" {
log.Printf("PLEX_UID set. Adding usermod command to transcode command.")
args = append([]string{fmt.Sprintf("/usr/sbin/usermod -o -u %s plex", plexUID)}, args...)
}
if plexGID != "" {
log.Printf("PLEX_GID set. Adding groupmod command to transcode command.")
args = append([]string{fmt.Sprintf("/usr/sbin/groupmod -o -g %s plex", plexGID)}, args...)
}

// Change entrypoint to be bash if we need it for the permissions operations.
if plexUID != "" || plexGID != "" {
log.Printf("PLEX_UID || PLEX_GID set. Prefixing transcode command with bash.")

//Replace the space in "Plex Transcoder" path
in[0] = strings.Replace(in[0], " ", "\\ ", 1)

var escaped_in []string
escaped_in = append(escaped_in, in[0])
for _, v := range in[1:] {
escaped_in = append(escaped_in, shellescape.Quote(v))
}

args = append([]string{"/bin/bash", "-c"},
fmt.Sprintf("%s && su plex -s /bin/bash -c %s",
strings.Join(args, " && "),
shellescape.Quote(strings.Join(escaped_in, " "))))
} else {
return in
}
return args
}

func generatePod(cwd string, env []string, args []string) *corev1.Pod {
Expand Down