diff --git a/pkg/modules/generators/workload/workload_generator.go b/pkg/modules/generators/workload/workload_generator.go index 30eddc00..0f958fc5 100644 --- a/pkg/modules/generators/workload/workload_generator.go +++ b/pkg/modules/generators/workload/workload_generator.go @@ -137,6 +137,14 @@ func toOrderedContainers( } ctn.VolumeMounts = append(ctn.VolumeMounts, volumeMounts...) + // Append more volumes and volumeMounts + otherVolumes, otherVolumeMounts, err := handleDirCreation(c) + if err != nil { + return err + } + volumes = append(volumes, otherVolumes...) + ctn.VolumeMounts = append(ctn.VolumeMounts, otherVolumeMounts...) + // Append the container object to the containers slice. containers = append(containers, ctn) return nil @@ -341,7 +349,7 @@ func tcpSocketAction(urlstr string) (*corev1.TCPSocketAction, error) { }, nil } -// handleFileCreation handles the creation of the files declared in container.File +// handleFileCreation handles the creation of the files declared in container.Files // and returns the generated ConfigMap, Volume and VolumeMount. func handleFileCreation(c container.Container, uniqueAppName, containerName string) ( volumes []corev1.Volume, @@ -369,8 +377,8 @@ func handleFileCreation(c container.Container, uniqueAppName, containerName stri } if v.ContentFrom != "" { - sec, ok, err := parseSecretReference(v.ContentFrom) - if err != nil || !ok { + sec, ok, parseErr := parseSecretReference(v.ContentFrom) + if parseErr != nil || !ok { return fmt.Errorf("invalid content from str") } @@ -427,6 +435,33 @@ func handleFileCreation(c container.Container, uniqueAppName, containerName stri return } +// handleDirCreation handles the creation of folder declared in container.Dirs and returns +// the generated Volume and VolumeMount. +func handleDirCreation(c container.Container) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount, err error) { + err = modules.ForeachOrdered(c.Dirs, func(mountPath string, v string) error { + sec, ok, parseErr := parseSecretReference(v) + if parseErr != nil || !ok { + return fmt.Errorf("invalid dir configuration") + } + + volumes = append(volumes, corev1.Volume{ + Name: sec.Name, + VolumeSource: corev1.VolumeSource{ + Secret: &corev1.SecretVolumeSource{ + SecretName: sec.Name, + }, + }, + }) + + volumeMounts = append(volumeMounts, corev1.VolumeMount{ + Name: sec.Name, + MountPath: path.Join("/", mountPath), + }) + return nil + }) + return +} + // completeBaseWorkload uses config from workspace to complete the Workload base config. func completeBaseWorkload(base *workload.Base, config apiv1.GenericConfig) error { replicas, err := workspace.GetInt32PointerFromGenericConfig(config, workload.FieldReplicas) diff --git a/pkg/modules/generators/workload/workload_generator_test.go b/pkg/modules/generators/workload/workload_generator_test.go index 7995cfc6..535ee423 100644 --- a/pkg/modules/generators/workload/workload_generator_test.go +++ b/pkg/modules/generators/workload/workload_generator_test.go @@ -169,6 +169,36 @@ func TestGenerate(t *testing.T) { }, }, }, + { + name: "simple service workload with dirs", + project: "beep", + stack: "test", + application: "nginx", + workload: &workload.Workload{ + Header: workload.Header{ + Type: workload.TypeService, + }, + Service: &workload.Service{ + Base: workload.Base{ + Containers: map[string]container.Container{ + "main": { + Image: "nginx:latest", + Dirs: map[string]string{ + "/var/tmp-secret": "secret://other-sec-name", + }, + Files: map[string]container.FileSpec{ + "/run/secret/password": { + ContentFrom: "secret://sec-name/key?mode=0400", + Mode: "0644", + }, + }, + }, + }, + }, + Type: workload.Deployment, + }, + }, + }, } for _, tc := range testCases {