Skip to content

Commit

Permalink
feat: workload generator support create contentFrom based file (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Mar 9, 2024
1 parent 45d9f2c commit 6cacc77
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
52 changes: 49 additions & 3 deletions pkg/modules/generators/workload/workload_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workload
import (
"fmt"
"net/url"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -350,7 +351,6 @@ func handleFileCreation(c container.Container, uniqueAppName, containerName stri
) {
var idx int
err = modules.ForeachOrdered(c.Files, func(k string, v container.FileSpec) error {
// for k, v := range c.Files {
// The declared file path needs to include the file name.
if filepath.Base(k) == "." || filepath.Base(k) == "/" {
return fmt.Errorf("the declared file path needs to include the file name")
Expand All @@ -369,8 +369,26 @@ func handleFileCreation(c container.Container, uniqueAppName, containerName stri
}

if v.ContentFrom != "" {
// TODO: support the creation of the file content from a reference source.
panic("not supported the creation the file content from a reference source")
sec, ok, err := parseSecretReference(v.ContentFrom)
if err != nil || !ok {
return fmt.Errorf("invalid content from str")
}

volumes = append(volumes, corev1.Volume{
Name: sec.Name,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: sec.Name,
DefaultMode: &modeInt32,
},
},
})

volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: sec.Name,
MountPath: path.Join("/", k),
SubPath: sec.Key,
})
} else if v.Content != "" {
// Create the file content with configMap.
data := make(map[string]string)
Expand Down Expand Up @@ -440,3 +458,31 @@ func completeBaseWorkload(base *workload.Base, config apiv1.GenericConfig) error
}
return nil
}

type secretReference struct {
Name string
Key string
}

// parseSecretReference takes secret reference string as parameter and returns secretReference obj.
// Parameter `ref` is expected in following format: secret://sec-name/key, if the provided ref str
// is not in valid format, this function will return false or err.
func parseSecretReference(ref string) (result secretReference, _ bool, _ error) {
if strings.HasPrefix(ref, "${secret://") && strings.HasSuffix(ref, "}") {
ref = ref[2 : len(ref)-1]
}

if !strings.HasPrefix(ref, "secret://") {
return result, false, nil
}

u, err := url.Parse(ref)
if err != nil {
return result, false, err
}

result.Name = u.Host
result.Key, _, _ = strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")

return result, true, nil
}
52 changes: 52 additions & 0 deletions pkg/modules/generators/workload/workload_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,58 @@ func TestWorkloadGenerator_Generate(t *testing.T) {
}
}

func TestGenerate(t *testing.T) {
testCases := []struct {
name string
project string
stack string
application string
workload *workload.Workload
}{
{
name: "simple service workload",
project: "helloworld",
stack: "dev",
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",
Files: map[string]container.FileSpec{
"/run/secret/password": {
ContentFrom: "secret://sec-name/key?mode=0400",
Mode: "0644",
},
},
},
},
},
Type: workload.Deployment,
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := &Generator{
Project: tc.project,
Stack: tc.stack,
App: tc.application,
Workload: tc.workload,
}
spec := &apiv1.Intent{}
err := g.Generate(spec)
assert.NoError(t, err, "Error should be nil")
})
}
}

func TestToOrderedContainers(t *testing.T) {
t.Run("toOrderedContainers should convert app containers to ordered containers", func(t *testing.T) {
appContainers := make(map[string]container.Container)
Expand Down

0 comments on commit 6cacc77

Please sign in to comment.