From 333d051d4967ba9ecacffd1f65507aef082587cf Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Sun, 8 May 2022 09:11:31 +0200 Subject: [PATCH] Fix problems with relative/absolute path --- compose/parser.go | 3 ++- generator/main.go | 33 ++++++++++++--------------------- generator/utils.go | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 generator/utils.go diff --git a/compose/parser.go b/compose/parser.go index 81e54bf..86b0cb5 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -69,8 +69,9 @@ func (p *Parser) Parse(appname string) { cli.WithDefaultConfigPath, cli.WithNormalization(true), cli.WithInterpolation(true), - //cli.WithResolvedPaths(true), + cli.WithResolvedPaths(true), ) + if err != nil { log.Fatal(err) } diff --git a/generator/main.go b/generator/main.go index 9bec10b..4d2a252 100644 --- a/generator/main.go +++ b/generator/main.go @@ -10,7 +10,6 @@ import ( "net/url" "os" "path/filepath" - "regexp" "runtime" "strconv" "strings" @@ -239,8 +238,8 @@ func buildSelector(name string, s *types.ServiceConfig) map[string]string { } } -// buildCMFromPath generates a ConfigMap from a path. -func buildCMFromPath(name, path string) *helm.ConfigMap { +// buildConfigMapFromPath generates a ConfigMap from a path. +func buildConfigMapFromPath(name, path string) *helm.ConfigMap { stat, err := os.Stat(path) if err != nil { return nil @@ -267,7 +266,7 @@ func buildCMFromPath(name, path string) *helm.ConfigMap { } } - cm := helm.NewConfigMap(name, path) + cm := helm.NewConfigMap(name, GetRelPath(path)) cm.Data = files return cm } @@ -313,6 +312,9 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container * configMapsVolumes := make([]string, 0) if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok { configMapsVolumes = strings.Split(v, ",") + for i, cm := range configMapsVolumes { + configMapsVolumes[i] = strings.TrimSpace(cm) + } } for _, vol := range s.Volumes { @@ -329,15 +331,14 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container * isConfigMap := false for _, cmVol := range configMapsVolumes { - cmVol = strings.TrimSpace(cmVol) - if volname == cmVol { + if GetRelPath(volname) == cmVol { isConfigMap = true break } } + // local volume cannt be mounted if !isConfigMap && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) { - // local volume cannt be mounted logger.ActivateColors = true logger.Redf("You cannot, at this time, have local volume in %s deployment\n", name) logger.ActivateColors = false @@ -359,11 +360,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 := buildCMFromPath(name, volname) - volname = PathToName(volname) - cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + volname + cm := buildConfigMapFromPath(name, volname) + cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + PathToName(volname) - // build a configmap from the volume path + // build a configmapRef for this volume volumes = append(volumes, map[string]interface{}{ "name": volname, "configMap": map[string]string{ @@ -432,6 +432,7 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container * } } } + // add the volume in the container and return the volume definition to add in Deployment container.VolumeMounts = append(container.VolumeMounts, mountPoints...) return volumes } @@ -889,13 +890,3 @@ 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 = regexp.MustCompile(replaceChars).ReplaceAllString(path, "-") - return path -} diff --git a/generator/utils.go b/generator/utils.go new file mode 100644 index 0000000..a862035 --- /dev/null +++ b/generator/utils.go @@ -0,0 +1,22 @@ +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 +}