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:
20 files changed
+1075
-114
No files matched your search
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
"github.com/dokku/dokku/plugins/cron"
|
||||
"github.com/dokku/dokku/plugins/registry"
|
||||
"github.com/dokku/dokku/plugins/storage"
|
||||
"github.com/kballard/go-shellquote"
|
||||
)
|
||||
|
||||
@@ -335,9 +336,12 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
}
|
||||
}
|
||||
|
||||
processVolumes := []ProcessVolume{}
|
||||
// Volumes every workload in the app receives, whichever process type it
|
||||
// belongs to. Storage attachments are layered on top per process type
|
||||
// inside the loops below.
|
||||
baseVolumes := []ProcessVolume{}
|
||||
if shmSize := getComputedShmSize(appName); shmSize != "" {
|
||||
processVolumes = append(processVolumes, ProcessVolume{
|
||||
baseVolumes = append(baseVolumes, ProcessVolume{
|
||||
Name: "shmem",
|
||||
MountPath: "/dev/shm",
|
||||
EmptyDir: &ProcessVolumeEmptyDir{
|
||||
@@ -351,11 +355,9 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
if err != nil {
|
||||
return cleanup(fmt.Errorf("error loading storage app mounts: %w", err))
|
||||
}
|
||||
deployVolumes, err := ToProcessVolumes(deployMountPairs)
|
||||
if err != nil {
|
||||
if err := ValidateMountPairs(deployMountPairs); err != nil {
|
||||
return cleanup(err)
|
||||
}
|
||||
processVolumes = append(processVolumes, deployVolumes...)
|
||||
|
||||
for processType, processCount := range processes {
|
||||
healthchecks, ok := appJSON.Healthchecks[processType]
|
||||
@@ -411,7 +413,7 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
ProcessType: ProcessType_Worker,
|
||||
Replicas: int32(processCount),
|
||||
Resources: processResources,
|
||||
Volumes: processVolumes,
|
||||
Volumes: processVolumesFor(baseVolumes, deployMountPairs, processType),
|
||||
}
|
||||
|
||||
if processType == "web" {
|
||||
@@ -589,7 +591,11 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
ProcessType: ProcessType_Cron,
|
||||
Replicas: 1,
|
||||
Resources: processResources,
|
||||
Volumes: processVolumes,
|
||||
// A cron ID is a hash of the app name, command and schedule, so it
|
||||
// is not something an operator can name in --process-type and it
|
||||
// churns whenever the task changes. Cron jobs therefore sit with
|
||||
// `dokku run` and app.json tasks: default-scoped mounts only.
|
||||
Volumes: processVolumesFor(baseVolumes, deployMountPairs, storage.DefaultProcessType),
|
||||
}
|
||||
values.Processes[cronTask.ID] = processValues
|
||||
}
|
||||
|
||||
@@ -275,18 +275,37 @@ func LoadAppMounts(appName string, phase string) ([]AppMountPair, error) {
|
||||
return pairs, nil
|
||||
}
|
||||
|
||||
// ToProcessVolumes converts each AppMountPair into a ProcessVolume. K3s
|
||||
// app deployments reference the PVC by name; the PVC itself is owned by
|
||||
// the storage entry's separate helm release. Any docker-local entries
|
||||
// found here are an error - they cannot be mounted on a k3s app.
|
||||
func ToProcessVolumes(pairs []AppMountPair) ([]ProcessVolume, error) {
|
||||
volumes := []ProcessVolume{}
|
||||
// ValidateMountPairs rejects any pair whose entry belongs to another
|
||||
// scheduler. Kept separate from ToProcessVolumes so the chart builder can run
|
||||
// it once over the whole set: folding it into the per-process conversion would
|
||||
// make it reachable only for apps that have at least one scaled process or
|
||||
// cron task, and an app scaled to zero would stop reporting the mismatch.
|
||||
func ValidateMountPairs(pairs []AppMountPair) error {
|
||||
for _, pair := range pairs {
|
||||
if pair.Entry == nil || pair.Attachment == nil {
|
||||
continue
|
||||
}
|
||||
if pair.Entry.Scheduler != storage.SchedulerK3s {
|
||||
return nil, fmt.Errorf("storage entry %q is scheduler=%s but is mounted on a k3s app; recreate it with --scheduler k3s", pair.Entry.Name, pair.Entry.Scheduler)
|
||||
return fmt.Errorf("storage entry %q is scheduler=%s but is mounted on a k3s app; recreate it with --scheduler k3s", pair.Entry.Name, pair.Entry.Scheduler)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToProcessVolumes converts the AppMountPairs that apply to processType into
|
||||
// ProcessVolumes. K3s app deployments reference the PVC by name; the PVC
|
||||
// itself is owned by the storage entry's separate helm release.
|
||||
//
|
||||
// An empty processType names a workload that belongs to no process type - a
|
||||
// cron job - and so receives default-scoped attachments only.
|
||||
func ToProcessVolumes(pairs []AppMountPair, processType string) []ProcessVolume {
|
||||
volumes := []ProcessVolume{}
|
||||
for _, pair := range pairs {
|
||||
if pair.Entry == nil || pair.Attachment == nil {
|
||||
continue
|
||||
}
|
||||
if !pair.Attachment.AppliesToProcessType(processType) {
|
||||
continue
|
||||
}
|
||||
volumes = append(volumes, ProcessVolume{
|
||||
Name: pair.Entry.Name,
|
||||
@@ -298,7 +317,19 @@ func ToProcessVolumes(pairs []AppMountPair) ([]ProcessVolume, error) {
|
||||
},
|
||||
})
|
||||
}
|
||||
return volumes, nil
|
||||
return volumes
|
||||
}
|
||||
|
||||
// processVolumesFor returns the volumes one process type's pods receive: the
|
||||
// volumes every workload in the app gets, plus the storage attachments scoped
|
||||
// to that process type. The slice is freshly allocated rather than appended
|
||||
// onto base, so one workload's volumes can never alias another's.
|
||||
func processVolumesFor(base []ProcessVolume, pairs []AppMountPair, processType string) []ProcessVolume {
|
||||
scoped := ToProcessVolumes(pairs, processType)
|
||||
volumes := make([]ProcessVolume, 0, len(base)+len(scoped))
|
||||
volumes = append(volumes, base...)
|
||||
volumes = append(volumes, scoped...)
|
||||
return volumes
|
||||
}
|
||||
|
||||
// asK8sVolumeMount is a small helper used by tests / other callers that
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user