Skip to content

Commit ac0a896

Browse files
authored
fix: repair worker pull PVC ownership (#84)
1 parent ce6e500 commit ac0a896

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

internal/runtime/kubernetes/resources.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,42 @@ const (
4141
registryConfigEnvName = "DRUID_RUNTIME_REGISTRY_CONFIG_JSON"
4242
registryConfigSecretKey = "config.json"
4343
registryConfigScript = `printf '%s' "$DRUID_RUNTIME_REGISTRY_CONFIG_JSON" > /tmp/druid-registry.json && exec druid --config /tmp/druid-registry.json "$@"`
44+
workerPullRootEnvName = "DRUID_WORKER_ROOT"
45+
workerPullScript = `set -eu
46+
if [ -n "${DRUID_RUNTIME_REGISTRY_CONFIG_JSON:-}" ]; then
47+
printf '%s' "$DRUID_RUNTIME_REGISTRY_CONFIG_JSON" > /tmp/druid-registry.json
48+
druid --config /tmp/druid-registry.json "$@"
49+
else
50+
druid "$@"
51+
fi
52+
chown -R 1000:1000 "$DRUID_WORKER_ROOT"`
4453
)
4554

4655
func workerPullJobSpec(namespace string, jobName string, pvc string, image string, action ports.RuntimeWorkerAction, imagePullSecret string, registryConfigSecret string, registryPlainHTTP bool) *batchv1.Job {
4756
command := []string{
48-
"druid", "worker", "pull",
57+
"sh", "-c", workerPullScript, "druid-worker-pull",
58+
"worker", "pull",
4959
"--artifact", action.Artifact,
5060
"--runtime-id", action.RuntimeID,
5161
"--mode", string(action.Mode),
5262
"--root", action.MountPath,
5363
"--callback-url", action.CallbackURL,
5464
}
55-
if registryConfigSecret != "" {
56-
command = append([]string{"sh", "-c", registryConfigScript, "sh"}, command[1:]...)
57-
}
5865
job := helperJobSpec(namespace, jobName, pvc, image, command, imagePullSecret, map[string]string{
5966
labelComponent: "worker-pull",
6067
})
6168
container := &job.Spec.Template.Spec.Containers[0]
62-
container.Env = append(container.Env, corev1.EnvVar{Name: "DRUID_WORKER_TOKEN", Value: action.CallbackToken})
69+
runAsRoot := int64(0)
70+
runAsNonRoot := false
71+
container.SecurityContext = &corev1.SecurityContext{
72+
RunAsUser: &runAsRoot,
73+
RunAsGroup: &runAsRoot,
74+
RunAsNonRoot: &runAsNonRoot,
75+
}
76+
container.Env = append(container.Env,
77+
corev1.EnvVar{Name: "DRUID_WORKER_TOKEN", Value: action.CallbackToken},
78+
corev1.EnvVar{Name: workerPullRootEnvName, Value: action.MountPath},
79+
)
6380
if registryConfigSecret != "" {
6481
container.Env = append(container.Env, corev1.EnvVar{
6582
Name: registryConfigEnvName,

internal/runtime/kubernetes/resources_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func TestWorkerPullJobSpecRunsDruidWorkerPull(t *testing.T) {
213213
job := workerPullJobSpec("druid", "worker-pull", "runtime-pvc", "druid-cli:test", action, "pull-secret", "runtime-registry", true)
214214
container := job.Spec.Template.Spec.Containers[0]
215215
command := strings.Join(container.Command, " ")
216-
for _, want := range []string{"druid --config /tmp/druid-registry.json", "worker pull", "--mode update", "--runtime-id deployment-123", "--callback-url"} {
216+
for _, want := range []string{"druid --config /tmp/druid-registry.json", "worker pull", "--mode update", "--runtime-id deployment-123", "--callback-url", "chown -R 1000:1000"} {
217217
if !strings.Contains(command, want) {
218218
t.Fatalf("command = %#v, want %s", container.Command, want)
219219
}
@@ -228,6 +228,18 @@ func TestWorkerPullJobSpecRunsDruidWorkerPull(t *testing.T) {
228228
if env["DRUID_WORKER_TOKEN"] != "secret-token" || env["DRUID_REGISTRY_PLAIN_HTTP"] != "true" {
229229
t.Fatalf("env = %#v", container.Env)
230230
}
231+
if env[workerPullRootEnvName] != "/scroll" {
232+
t.Fatalf("%s = %q, want /scroll", workerPullRootEnvName, env[workerPullRootEnvName])
233+
}
234+
if container.SecurityContext == nil || container.SecurityContext.RunAsUser == nil || *container.SecurityContext.RunAsUser != 0 {
235+
t.Fatalf("worker pull must run as root to repair PVC ownership, securityContext = %#v", container.SecurityContext)
236+
}
237+
if container.SecurityContext.RunAsGroup == nil || *container.SecurityContext.RunAsGroup != 0 {
238+
t.Fatalf("worker pull runAsGroup = %#v, want 0", container.SecurityContext.RunAsGroup)
239+
}
240+
if container.SecurityContext.RunAsNonRoot == nil || *container.SecurityContext.RunAsNonRoot {
241+
t.Fatalf("worker pull runAsNonRoot = %#v, want false", container.SecurityContext.RunAsNonRoot)
242+
}
231243
if len(job.Spec.Template.Spec.ImagePullSecrets) != 1 || job.Spec.Template.Spec.ImagePullSecrets[0].Name != "pull-secret" {
232244
t.Fatalf("image pull secrets = %#v", job.Spec.Template.Spec.ImagePullSecrets)
233245
}

0 commit comments

Comments
 (0)