Merge pull request #154 from Katenary/develop

fix(configmap): File from the root of project ends by a dash
This commit is contained in:
2025-08-03 13:16:21 +02:00
committed by GitHub
2 changed files with 45 additions and 1 deletions

View File

@@ -2,8 +2,10 @@ package generator
import ( import (
"fmt" "fmt"
"io"
"katenary/generator/labels" "katenary/generator/labels"
"os" "os"
"regexp"
"testing" "testing"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
@@ -90,3 +92,41 @@ func TestAppendBadDir(t *testing.T) {
t.Errorf("Expected error, got nil") t.Errorf("Expected error, got nil")
} }
} }
func TestRootConfigmapfile(t *testing.T) {
composeFile := `
services:
web:
image: nginx
volumes:
- ./foo.txt:/etc/foo.txt
labels:
%[1]s/configmap-files: |-
- ./foo.txt
`
composeFile = fmt.Sprintf(composeFile, labels.KatenaryLabelPrefix)
tmpDir := setup(composeFile)
defer teardown(tmpDir)
currentDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(currentDir)
fooTxt := "foo content"
fooFp, _ := os.Create("foo.txt")
io.WriteString(fooFp, fooTxt)
fooFp.Close()
output := internalCompileTest(t, "-s", "templates/web/statics/configmap.yaml")
configMap := v1.ConfigMap{}
if err := yaml.Unmarshal([]byte(output), &configMap); err != nil {
t.Errorf(unmarshalError, err)
}
if configMap.Data == nil {
t.Error("Expected configmap data to not be nil")
}
// if the configmap.Name ends by anything that is not alphanumeric, there is a problem
valid := regexp.MustCompile(`.*[a-zA-Z0-9]+$`)
if !valid.MatchString(configMap.Name) {
t.Errorf("ConfigMap name %s is not valid", configMap.Name)
}
}

View File

@@ -635,12 +635,16 @@ func (d *Deployment) appendFileToConfigMap(service types.ServiceConfig, appName
// in generate.go // in generate.go
dirname := filepath.Dir(volume.Source) dirname := filepath.Dir(volume.Source)
pathname := utils.PathToName(dirname) pathname := utils.PathToName(dirname)
pathname = strings.TrimSpace(pathname)
if len(pathname) != 0 {
pathname += "-" + pathname
}
var cm *ConfigMap var cm *ConfigMap
if v, ok := d.configMaps[pathname]; !ok { if v, ok := d.configMaps[pathname]; !ok {
cm = NewConfigMap(*d.service, appName, true) cm = NewConfigMap(*d.service, appName, true)
cm.usage = FileMapUsageFiles cm.usage = FileMapUsageFiles
cm.path = dirname cm.path = dirname
cm.Name = utils.TplName(service.Name, appName) + "-" + pathname cm.Name = utils.TplName(service.Name, appName) + pathname
d.configMaps[pathname] = &ConfigMapMount{ d.configMaps[pathname] = &ConfigMapMount{
configMap: cm, configMap: cm,
mountPath: []mountPathConfig{{ mountPath: []mountPathConfig{{