fix: honor storage attachment process types at deploy time

An attachment's process type was recorded, reported and used to scope `storage:mount --replace` and `storage:unmount --all`, but nothing read it when the volume was actually mounted, so `storage:mount <app> <entry> --container-dir <path> --process-type web` bound the volume into every process rather than only `web`. Both schedulers now scope a mount to the process type it was declared for, and containers that belong to no process type - a `dokku run` one-off, an `app.json` deploy task, a k3s cron job - receive default-scoped mounts only. A container path can now be claimed by a single mount per process type, and `storage:migrate` drains process-scoped `-v` lines out of `docker-options` rather than only the default-scoped ones.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-09-21 18:08:42 -04:00
1 parent e00e939f25
commit b612285392
20 files changed
+1075 -114

No files matched your search

+102 -13
View File
@@ -6,7 +6,7 @@ import (
"github.com/dokku/dokku/plugins/storage"
)
func TestToProcessVolumesRejectsDockerLocalEntry(t *testing.T) {
func TestValidateMountPairsRejectsDockerLocalEntry(t *testing.T) {
pairs := []AppMountPair{
{
Entry: &storage.Entry{
@@ -19,7 +19,7 @@ func TestToProcessVolumesRejectsDockerLocalEntry(t *testing.T) {
},
}
_, err := ToProcessVolumes(pairs)
err := ValidateMountPairs(pairs)
if err == nil {
t.Fatal("expected error for docker-local entry, got nil")
}
@@ -29,7 +29,7 @@ func TestToProcessVolumesRejectsDockerLocalEntry(t *testing.T) {
}
}
func TestToProcessVolumesRejectsNonK3sEntry(t *testing.T) {
func TestValidateMountPairsRejectsNonK3sEntry(t *testing.T) {
pairs := []AppMountPair{
{
Entry: &storage.Entry{
@@ -42,7 +42,7 @@ func TestToProcessVolumesRejectsNonK3sEntry(t *testing.T) {
},
}
_, err := ToProcessVolumes(pairs)
err := ValidateMountPairs(pairs)
if err == nil {
t.Fatal("expected error for non-k3s entry, got nil")
}
@@ -52,6 +52,28 @@ func TestToProcessVolumesRejectsNonK3sEntry(t *testing.T) {
}
}
// TestValidateMountPairsRejectsOutOfScopeEntry pins the reason validation is a
// separate pass: a mismatched entry scoped to a process type the caller is not
// currently building still has to fail the deploy.
func TestValidateMountPairsRejectsOutOfScopeEntry(t *testing.T) {
pairs := []AppMountPair{
{
Entry: &storage.Entry{
Name: "demo-data",
Scheduler: storage.SchedulerDockerLocal,
},
Attachment: &storage.Attachment{
ContainerPath: "/data",
ProcessType: "worker",
},
},
}
if err := ValidateMountPairs(pairs); err == nil {
t.Fatal("expected error for docker-local entry scoped to another process, got nil")
}
}
func TestToProcessVolumesAcceptsK3sEntry(t *testing.T) {
pairs := []AppMountPair{
{
@@ -67,10 +89,7 @@ func TestToProcessVolumesAcceptsK3sEntry(t *testing.T) {
},
}
volumes, err := ToProcessVolumes(pairs)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
volumes := ToProcessVolumes(pairs, "web")
if len(volumes) != 1 {
t.Fatalf("expected 1 volume, got %d", len(volumes))
}
@@ -99,11 +118,81 @@ func TestToProcessVolumesSkipsNilPairs(t *testing.T) {
{Entry: nil, Attachment: &storage.Attachment{ContainerPath: "/data"}},
}
volumes, err := ToProcessVolumes(pairs)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(volumes) != 0 {
if volumes := ToProcessVolumes(pairs, "web"); len(volumes) != 0 {
t.Fatalf("expected 0 volumes, got %d", len(volumes))
}
}
// TestToProcessVolumesScopesToProcessType is the k3s half of #9059: a mount
// scoped to a named process type reaches that process and no other, while a
// default-scoped mount reaches all of them.
func TestToProcessVolumesScopesToProcessType(t *testing.T) {
pairs := []AppMountPair{
{
Entry: &storage.Entry{Name: "shared", Scheduler: storage.SchedulerK3s},
Attachment: &storage.Attachment{ContainerPath: "/shared", ProcessType: storage.DefaultProcessType},
},
{
Entry: &storage.Entry{Name: "web-only", Scheduler: storage.SchedulerK3s},
Attachment: &storage.Attachment{ContainerPath: "/web", ProcessType: "web"},
},
{
Entry: &storage.Entry{Name: "legacy", Scheduler: storage.SchedulerK3s},
Attachment: &storage.Attachment{ContainerPath: "/legacy"},
},
}
for _, tc := range []struct {
processType string
expected []string
}{
{processType: "web", expected: []string{"shared", "web-only", "legacy"}},
{processType: "worker", expected: []string{"shared", "legacy"}},
{processType: "", expected: []string{"shared", "legacy"}},
} {
volumes := ToProcessVolumes(pairs, tc.processType)
names := []string{}
for _, volume := range volumes {
names = append(names, volume.Name)
}
if len(names) != len(tc.expected) {
t.Fatalf("process type %q: expected %v, got %v", tc.processType, tc.expected, names)
}
for i, name := range tc.expected {
if names[i] != name {
t.Fatalf("process type %q: expected %v, got %v", tc.processType, tc.expected, names)
}
}
}
}
// TestProcessVolumesForDoesNotAliasBase pins that each process type gets its
// own slice: appending onto a shared backing array would leak one process's
// storage mounts into another's pod spec.
func TestProcessVolumesForDoesNotAliasBase(t *testing.T) {
base := make([]ProcessVolume, 0, 8)
base = append(base, ProcessVolume{Name: "shmem", MountPath: "/dev/shm"})
pairs := []AppMountPair{
{
Entry: &storage.Entry{Name: "web-only", Scheduler: storage.SchedulerK3s},
Attachment: &storage.Attachment{ContainerPath: "/web", ProcessType: "web"},
},
}
web := processVolumesFor(base, pairs, "web")
worker := processVolumesFor(base, pairs, "worker")
if len(web) != 2 {
t.Fatalf("expected web to get 2 volumes, got %d", len(web))
}
if len(worker) != 1 {
t.Fatalf("expected worker to get 1 volume, got %d", len(worker))
}
if worker[0].Name != "shmem" {
t.Fatalf("expected worker to keep only the base volume, got %s", worker[0].Name)
}
if len(base) != 1 {
t.Fatalf("expected base to be left untouched, got %d volumes", len(base))
}
}