Fix path label

This commit is contained in:
2022-06-01 16:51:45 +02:00
parent 75500c0830
commit 403e7fb7e5
5 changed files with 14 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import (
"katenary/compose"
"katenary/helm"
"katenary/logger"
"katenary/tools"
"log"
"net/url"
"os"
@@ -272,7 +273,7 @@ func buildConfigMapFromPath(name, path string) *helm.ConfigMap {
files[filename] = string(c)
}
cm := helm.NewConfigMap(name, GetRelPath(path))
cm := helm.NewConfigMap(name, tools.GetRelPath(path))
cm.Data = files
return cm
}
@@ -337,7 +338,7 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
isConfigMap := false
for _, cmVol := range configMapsVolumes {
if GetRelPath(volname) == cmVol {
if tools.GetRelPath(volname) == cmVol {
isConfigMap = true
break
}
@@ -366,10 +367,10 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
// the volume is a path and it's explicitally asked to be a configmap in labels
cm := buildConfigMapFromPath(name, volname)
cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + PathToName(volname)
cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + tools.PathToName(volname)
// build a configmapRef for this volume
volname := PathToName(volname)
volname := tools.PathToName(volname)
volumes = append(volumes, map[string]interface{}{
"name": volname,
"configMap": map[string]string{
@@ -586,7 +587,7 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
// manage environment files (env_file in compose)
for _, envfile := range s.EnvFile {
f := PathToName(envfile)
f := tools.PathToName(envfile)
f = strings.ReplaceAll(f, ".env", "")
isSecret := false
for _, s := range secretsFiles {
@@ -876,7 +877,7 @@ func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *ty
for name, volumes := range volumesFrom {
for volumeName := range volumes {
initianame := volumeName
volumeName = PathToName(volumeName)
volumeName = tools.PathToName(volumeName)
// get the volume from the deployment container "name"
var ctn *helm.Container
for _, c := range deployment.Spec.Template.Spec.Containers {

View File

@@ -1,22 +0,0 @@
package generator
import (
"katenary/compose"
"regexp"
"strings"
)
// replaceChars replaces some chars in a string.
const replaceChars = `[^a-zA-Z0-9_]+`
// GetRelPath return the relative path from the root of the project.
func GetRelPath(path string) string {
return strings.Replace(path, compose.GetCurrentDir(), ".", 1)
}
// PathToName transform a path to a yaml name.
func PathToName(path string) string {
path = strings.TrimPrefix(GetRelPath(path), "./")
path = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-")
return path
}

View File

@@ -1,14 +0,0 @@
package generator
import (
"katenary/compose"
"testing"
)
func Test_PathToName(t *testing.T) {
path := compose.GetCurrentDir() + "/envéfoo.file"
name := PathToName(path)
if name != "env-foo-file" {
t.Error("Expected env-foo-file, got ", name)
}
}

View File

@@ -4,6 +4,7 @@ import (
"katenary/compose"
"katenary/generator/writers"
"katenary/helm"
"katenary/tools"
"log"
"os"
"path/filepath"
@@ -175,7 +176,7 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers
// there could be several files, so let's force the filename
name := c.(helm.Named).Name() + "-" + c.GetType()
suffix := c.GetPathRessource()
suffix = PathToName(suffix)
suffix = tools.PathToName(suffix)
name += suffix
name = PrefixRE.ReplaceAllString(name, "")
writers.BuildConfigMap(c, kind, n, name, templatesDir)