diff --git a/generator/main.go b/generator/main.go index 4ae44af..9bec10b 100644 --- a/generator/main.go +++ b/generator/main.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "runtime" "strconv" "strings" @@ -889,9 +890,12 @@ func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *ty } } +// replaceChars replaces some chars in a string. +const replaceChars = `[^a-zA-Z0-9._-]` + +// PathToName transform a path to a yaml name. func PathToName(path string) string { path = strings.TrimPrefix(path, "./") - path = strings.ReplaceAll(path, ".", "-") - path = strings.ReplaceAll(path, "/", "-") + path = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-") return path } diff --git a/generator/writer.go b/generator/writer.go index 6ed7dc3..ec0c372 100644 --- a/generator/writer.go +++ b/generator/writer.go @@ -16,10 +16,14 @@ import ( "gopkg.in/yaml.v3" ) +// HelmFile represents a helm file from helm package that has got some necessary methods +// to generate a helm file. type HelmFile interface { GetType() string GetPathRessource() string } + +// HelmFileGenerator is a chanel of HelmFile. type HelmFileGenerator chan HelmFile var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`) @@ -34,6 +38,7 @@ func portExists(port int, ports []types.ServicePortConfig) bool { return false } +// Generate get a parsed compose file, and generate the helm files. func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVersion, composeFile, dirName string) { // make the appname global (yes... ugly but easy) @@ -55,6 +60,7 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers // Manage services to not export skips := make(map[string]bool) + // remove ignored services for _, s := range p.Data.Services { if s.Labels[helm.LABEL_IGNORE] == "true" { skips[s.Name] = true @@ -185,12 +191,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() - if suffix != "" { - charToRemove := []string{"/", ".", " "} - for _, char := range charToRemove { - suffix = strings.Replace(suffix, char, "-", -1) - } - } + suffix = PathToName(suffix) name += suffix name = PrefixRE.ReplaceAllString(name, "") writers.BuildConfigMap(c, kind, n, name, templatesDir)