diff --git a/internal/generator/configMap_test.go b/internal/generator/configMap_test.go index 48e3ae0..0e83ef8 100644 --- a/internal/generator/configMap_test.go +++ b/internal/generator/configMap_test.go @@ -10,6 +10,7 @@ import ( "katenary.io/internal/generator/labels" "github.com/compose-spec/compose-go/types" + appv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" "sigs.k8s.io/yaml" ) @@ -117,9 +118,15 @@ services: io.WriteString(fooFp, fooTxt) fooFp.Close() - output := internalCompileTest(t, "-s", "templates/web/statics/configmap.yaml") + cmOutput := internalCompileTestForce(t, "-s", "templates/web/statics/configmap.yaml") + depOutput := internalCompileTestForce(t, "-s", "templates/web/deployment.yaml") + configMap := v1.ConfigMap{} - if err := yaml.Unmarshal([]byte(output), &configMap); err != nil { + if err := yaml.Unmarshal([]byte(cmOutput), &configMap); err != nil { + t.Errorf(unmarshalError, err) + } + deployment := appv1.Deployment{} + if err := yaml.Unmarshal([]byte(depOutput), &deployment); err != nil { t.Errorf(unmarshalError, err) } if configMap.Data == nil { @@ -130,4 +137,12 @@ services: if !valid.MatchString(configMap.Name) { t.Errorf("ConfigMap name %s is not valid", configMap.Name) } + + // the volume mount should be named "configmap-" + if deployment.Spec.Template.Spec.Volumes[0].Name != deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name { + t.Errorf("Expected volume name to be %s, got %s", + deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name, + deployment.Spec.Template.Spec.Volumes[0].Name, + ) + } } diff --git a/internal/generator/generator.go b/internal/generator/generator.go index 7fa5b9e..6c9f48f 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -278,6 +278,7 @@ func addStaticVolumes(deployments map[string]*Deployment, service types.ServiceC if y, err = config.configMap.Yaml(); err != nil { log.Fatal(err) } + // add the configmap to the chart d.chart.Templates[config.configMap.Filename()] = &ChartTemplate{ Content: y, @@ -285,6 +286,10 @@ func addStaticVolumes(deployments map[string]*Deployment, service types.ServiceC } // add the moint path to the container for _, m := range config.mountPath { + // volumeName can be empty, in this case we generate a name + if volumeName == "" { + volumeName = utils.PathToName(m.subPath) + } container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ Name: utils.PathToName(volumeName), MountPath: m.mountPath, diff --git a/internal/generator/tools_test.go b/internal/generator/tools_test.go index fa22caa..aeb6730 100644 --- a/internal/generator/tools_test.go +++ b/internal/generator/tools_test.go @@ -29,13 +29,22 @@ func teardown(tmpDir string) { } } +// internalCompileTestForce is like internalCompileTest but with the force option enabled to rewrite the chart. +func internalCompileTestForce(t *testing.T, options ...string) string { + return compileTest(t, true, options...) +} + +// internalCompileTest is like compileTest but without the force option enabled to rewrite the chart. func internalCompileTest(t *testing.T, options ...string) string { + return compileTest(t, false, options...) +} + +func compileTest(t *testing.T, force bool, options ...string) string { _, err := parser.Parse(nil, nil, "compose.yml") if err != nil { t.Fatalf("Failed to parse the project: %s", err) } - force := false outputDir := "./chart" profiles := make([]string, 0) helmdepUpdate := true