fix(volume) File from project root failed
All checks were successful
Go-Tests / tests (pull_request) Successful in 1m47s
Go-Tests / sonar (pull_request) Successful in 48s
Go-Tests / tests (push) Successful in 1m37s
Go-Tests / sonar (push) Successful in 46s

When mounting one file from the root of the project (e.g. ./file.txt),
the volume name is empty. In this case, we generate the volume name from
the subpath.
This commit is contained in:
2025-09-14 23:23:11 +02:00
parent 8358b068b3
commit 03b4d5c714
3 changed files with 32 additions and 3 deletions

View File

@@ -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-<configmap name>"
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,
)
}
}

View File

@@ -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,

View File

@@ -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