A big commit to fix more things, see details

- ingress can now have annotations
- configmaps are better named and there is a label to know from where
  the content is taken
- fixed bad "nil" pointer checks
- we force to not resolve paths from compose file, this should be a
  problem when we call the compose file from outside. So, TODO: check
  this!
- the "project" label is now the Chart Name, not a static string
- minor fixes
This commit is contained in:
2022-05-06 20:18:10 +02:00
parent baeb8a612a
commit b1b96d8318
8 changed files with 68 additions and 22 deletions

View File

@@ -69,7 +69,7 @@ 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

@@ -195,11 +195,15 @@ func generateServicesAndIngresses(name string, s *types.ServiceConfig) []HelmFil
func createIngress(name string, port int, s *types.ServiceConfig) *helm.Ingress { func createIngress(name string, port int, s *types.ServiceConfig) *helm.Ingress {
ingress := helm.NewIngress(name) ingress := helm.NewIngress(name)
annotations := map[string]string{}
ingressVal := map[string]interface{}{ ingressVal := map[string]interface{}{
"class": "nginx", "class": "nginx",
"host": name + "." + helm.Appname + ".tld", "host": name + "." + helm.Appname + ".tld",
"enabled": false, "enabled": false,
"annotations": annotations,
} }
// add Annotations in values
AddValues(name, map[string]EnvVal{"ingress": ingressVal}) AddValues(name, map[string]EnvVal{"ingress": ingressVal})
ingress.Spec.Rules = []helm.IngressRule{ ingress.Spec.Rules = []helm.IngressRule{
@@ -235,7 +239,7 @@ func buildSelector(name string, s *types.ServiceConfig) map[string]string {
} }
// buildCMFromPath generates a ConfigMap from a path. // buildCMFromPath generates a ConfigMap from a path.
func buildCMFromPath(path string) *helm.ConfigMap { func buildCMFromPath(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
@@ -262,7 +266,7 @@ func buildCMFromPath(path string) *helm.ConfigMap {
} }
} }
cm := helm.NewConfigMap("") cm := helm.NewConfigMap(name, path)
cm.Data = files cm.Data = files
return cm return cm
} }
@@ -354,11 +358,11 @@ 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(volname) cm := buildCMFromPath(name, volname)
volname = strings.Replace(volname, "./", "", 1) volname = strings.Replace(volname, "./", "", 1)
volname = strings.ReplaceAll(volname, "/", "-") volname = strings.ReplaceAll(volname, "/", "-")
volname = strings.ReplaceAll(volname, ".", "-") volname = strings.ReplaceAll(volname, ".", "-")
cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + volname + "-" + name cm.K8sBase.Metadata.Name = helm.ReleaseNameTpl + "-" + name + "-" + volname
// build a configmap from the volume path // build a configmap from the volume path
volumes = append(volumes, map[string]interface{}{ volumes = append(volumes, map[string]interface{}{
@@ -570,7 +574,6 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
f = strings.ReplaceAll(f, ".env", "") f = strings.ReplaceAll(f, ".env", "")
f = strings.ReplaceAll(f, ".", "") f = strings.ReplaceAll(f, ".", "")
f = strings.ReplaceAll(f, "/", "") f = strings.ReplaceAll(f, "/", "")
cf := f + "-" + name
isSecret := false isSecret := false
for _, s := range secretsFiles { for _, s := range secretsFiles {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
@@ -580,11 +583,11 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
} }
var store helm.InlineConfig var store helm.InlineConfig
if !isSecret { if !isSecret {
logger.Bluef(ICON_CONF+" Generating configMap %s\n", cf) logger.Bluef(ICON_CONF+" Generating configMap from %s\n", envfile)
store = helm.NewConfigMap(cf) store = helm.NewConfigMap(name, envfile)
} else { } else {
logger.Bluef(ICON_SECRET+" Generating secret %s\n", cf) logger.Bluef(ICON_SECRET+" Generating secret from %s\n", envfile)
store = helm.NewSecret(cf) store = helm.NewSecret(name, envfile)
} }
envfile = filepath.Join(compose.GetCurrentDir(), envfile) envfile = filepath.Join(compose.GetCurrentDir(), envfile)
@@ -620,7 +623,7 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
} }
if store != nil { if store != nil {
fileGeneratorChan <- store fileGeneratorChan <- store.(HelmFile)
} }
} }
} }
@@ -695,6 +698,9 @@ func applyEnvMapLabel(s *types.ServiceConfig, c *helm.Container) {
vstring := fmt.Sprintf("%v", v) vstring := fmt.Sprintf("%v", v)
s.Environment[k] = &vstring s.Environment[k] = &vstring
touched := false touched := false
if c.Env != nil {
c.Env = make([]*helm.Value, 0)
}
for _, env := range c.Env { for _, env := range c.Env {
if env.Name == k { if env.Name == k {
env.Value = v env.Value = v
@@ -745,7 +751,7 @@ func setSecretVar(name string, s *types.ServiceConfig, c *helm.Container) *helm.
return nil return nil
} }
store := helm.NewSecret(name + "-secrets") store := helm.NewSecret(name, "")
for _, secretvar := range strings.Split(secretvars, ",") { for _, secretvar := range strings.Split(secretvars, ",") {
secretvar = strings.TrimSpace(secretvar) secretvar = strings.TrimSpace(secretvar)
// get the value from env // get the value from env

View File

@@ -16,7 +16,10 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
type HelmFile interface{} type HelmFile interface {
GetType() string
GetPathRessource() string
}
type HelmFileGenerator chan HelmFile type HelmFileGenerator chan HelmFile
var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`) var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`)
@@ -179,7 +182,15 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, chartVers
case *helm.ConfigMap, *helm.Secret: case *helm.ConfigMap, *helm.Secret:
// there could be several files, so let's force the filename // there could be several files, so let's force the filename
name := c.(helm.Named).Name() name := c.(helm.Named).Name() + "-" + c.GetType()
suffix := c.GetPathRessource()
if suffix != "" {
charToRemove := []string{"/", ".", " "}
for _, char := range charToRemove {
suffix = strings.Replace(suffix, char, "-", -1)
}
}
name += suffix
name = PrefixRE.ReplaceAllString(name, "") name = PrefixRE.ReplaceAllString(name, "")
writers.BuildConfigMap(c, kind, n, name, templatesDir) writers.BuildConfigMap(c, kind, n, name, templatesDir)

View File

@@ -9,7 +9,7 @@ import (
// BuildConfigMap writes the configMap. // BuildConfigMap writes the configMap.
func BuildConfigMap(c interface{}, kind, servicename, name, templatesDir string) { func BuildConfigMap(c interface{}, kind, servicename, name, templatesDir string) {
fname := filepath.Join(templatesDir, servicename+"."+name+"."+kind+".yaml") fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
fp, _ := os.Create(fname) fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp) enc := yaml.NewEncoder(fp)
enc.SetIndent(IndentSize) enc.SetIndent(IndentSize)

View File

@@ -59,6 +59,15 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
l = apiVersion l = apiVersion
} }
// add annotations linked to the Values
if strings.Contains(l, "annotations:") {
n := CountSpaces(l) + IndentSize
l += "\n" + strings.Repeat(" ", n) + "{{- range $k, $v := .Values.__name__.ingress.annotations }}\n"
l += strings.Repeat(" ", n) + "{{ $k }}: {{ $v }}\n"
l += strings.Repeat(" ", n) + "{{- end }}"
l = strings.ReplaceAll(l, "__name__", name)
}
// pathTyype is ony for 1.19+ // pathTyype is ony for 1.19+
if strings.Contains(l, "pathType:") { if strings.Contains(l, "pathType:") {
n := CountSpaces(l) n := CountSpaces(l)

View File

@@ -21,12 +21,15 @@ type ConfigMap struct {
} }
// NewConfigMap returns a new initialzed ConfigMap. // NewConfigMap returns a new initialzed ConfigMap.
func NewConfigMap(name string) *ConfigMap { func NewConfigMap(name, path string) *ConfigMap {
base := NewBase() base := NewBase()
base.ApiVersion = "v1" base.ApiVersion = "v1"
base.Kind = "ConfigMap" base.Kind = "ConfigMap"
base.Metadata.Name = ReleaseNameTpl + "-" + name base.Metadata.Name = ReleaseNameTpl + "-" + name
base.Metadata.Labels[K+"/component"] = name base.Metadata.Labels[K+"/component"] = name
if path != "" {
base.Metadata.Labels[K+"/path"] = path
}
return &ConfigMap{ return &ConfigMap{
K8sBase: base, K8sBase: base,
Data: make(map[string]string), Data: make(map[string]string),
@@ -72,12 +75,15 @@ type Secret struct {
} }
// NewSecret returns a new initialzed Secret. // NewSecret returns a new initialzed Secret.
func NewSecret(name string) *Secret { func NewSecret(name, path string) *Secret {
base := NewBase() base := NewBase()
base.ApiVersion = "v1" base.ApiVersion = "v1"
base.Kind = "Secret" base.Kind = "Secret"
base.Metadata.Name = ReleaseNameTpl + "-" + name base.Metadata.Name = ReleaseNameTpl + "-" + name
base.Metadata.Labels[K+"/component"] = name base.Metadata.Labels[K+"/component"] = name
if path != "" {
base.Metadata.Labels[K+"/path"] = path
}
return &Secret{ return &Secret{
K8sBase: base, K8sBase: base,
Data: make(map[string]string), Data: make(map[string]string),

View File

@@ -38,7 +38,6 @@ func NewContainer(name, image string, environment types.MappingWithEquals, label
container := &Container{ container := &Container{
Image: image, Image: image,
Name: name, Name: name,
Env: make([]*Value, len(environment)),
EnvFrom: make([]map[string]map[string]string, 0), EnvFrom: make([]map[string]map[string]string, 0),
} }

View File

@@ -4,6 +4,7 @@ import (
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
) )
// Metadata is the metadata for a kubernetes object. // Metadata is the metadata for a kubernetes object.
@@ -34,7 +35,7 @@ func NewBase() *K8sBase {
Metadata: NewMetadata(), Metadata: NewMetadata(),
} }
// add some information of the build // add some information of the build
b.Metadata.Labels[K+"/project"] = GetProjectName() b.Metadata.Labels[K+"/project"] = "{{ .Chart.Name }}"
b.Metadata.Labels[K+"/release"] = ReleaseNameTpl b.Metadata.Labels[K+"/release"] = ReleaseNameTpl
b.Metadata.Annotations[K+"/version"] = Version b.Metadata.Annotations[K+"/version"] = Version
return b return b
@@ -56,3 +57,17 @@ func (k *K8sBase) Get() string {
func (k *K8sBase) Name() string { func (k *K8sBase) Name() string {
return k.Metadata.Name return k.Metadata.Name
} }
func (k *K8sBase) GetType() string {
if n, ok := k.Metadata.Labels[K+"/type"]; ok {
return n
}
return strings.ToLower(k.Kind)
}
func (k *K8sBase) GetPathRessource() string {
if p, ok := k.Metadata.Labels[K+"/path"]; ok {
return p
}
return ""
}