Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: workload generator add container dirs implementation #899

Merged
merged 1 commit into from
Mar 12, 2024
Merged
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
41 changes: 38 additions & 3 deletions pkg/modules/generators/workload/workload_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions pkg/modules/generators/workload/workload_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading