Fix problems with relative/absolute path

This commit is contained in:
2022-05-08 09:11:31 +02:00
parent 7e861618bb
commit 333d051d49
3 changed files with 36 additions and 22 deletions

View File

@@ -69,8 +69,9 @@ func (p *Parser) Parse(appname string) {
cli.WithDefaultConfigPath, cli.WithDefaultConfigPath,
cli.WithNormalization(true), cli.WithNormalization(true),
cli.WithInterpolation(true), cli.WithInterpolation(true),
//cli.WithResolvedPaths(true), cli.WithResolvedPaths(true),
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@@ -10,7 +10,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -239,8 +238,8 @@ func buildSelector(name string, s *types.ServiceConfig) map[string]string {
} }
} }
// buildCMFromPath generates a ConfigMap from a path. // buildConfigMapFromPath generates a ConfigMap from a path.
func buildCMFromPath(name, path string) *helm.ConfigMap { func buildConfigMapFromPath(name, path string) *helm.ConfigMap {
stat, err := os.Stat(path) stat, err := os.Stat(path)
if err != nil { if err != nil {
return 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 cm.Data = files
return cm return cm
} }
@@ -313,6 +312,9 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
configMapsVolumes := make([]string, 0) configMapsVolumes := make([]string, 0)
if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok { if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok {
configMapsVolumes = strings.Split(v, ",") configMapsVolumes = strings.Split(v, ",")
for i, cm := range configMapsVolumes {
configMapsVolumes[i] = strings.TrimSpace(cm)
}
} }
for _, vol := range s.Volumes { for _, vol := range s.Volumes {
@@ -329,15 +331,14 @@ func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *
isConfigMap := false isConfigMap := false
for _, cmVol := range configMapsVolumes { for _, cmVol := range configMapsVolumes {
cmVol = strings.TrimSpace(cmVol) if GetRelPath(volname) == cmVol {
if volname == cmVol {
isConfigMap = true isConfigMap = true
break break
} }
} }
// local volume cannt be mounted
if !isConfigMap && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) { if !isConfigMap && (strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/")) {
// local volume cannt be mounted
logger.ActivateColors = true logger.ActivateColors = true
logger.Redf("You cannot, at this time, have local volume in %s deployment\n", name) logger.Redf("You cannot, at this time, have local volume in %s deployment\n", name)
logger.ActivateColors = false 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 // the volume is a path and it's explicitally asked to be a configmap in labels
cm := buildCMFromPath(name, volname) cm := buildConfigMapFromPath(name, volname)
volname = PathToName(volname) cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + PathToName(volname)
cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + volname
// build a configmap from the volume path // build a configmapRef for this volume
volumes = append(volumes, map[string]interface{}{ volumes = append(volumes, map[string]interface{}{
"name": volname, "name": volname,
"configMap": map[string]string{ "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...) container.VolumeMounts = append(container.VolumeMounts, mountPoints...)
return volumes 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
}

22
generator/utils.go Normal file
View File

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