Files
katenary/main.go

96 lines
2.2 KiB
Go
Raw Normal View History

2021-11-30 12:04:28 +01:00
package main
import (
"flag"
"helm-compose/compose"
"helm-compose/generator"
"helm-compose/helm"
"os"
"path/filepath"
2021-11-30 12:09:07 +01:00
"strings"
2021-11-30 12:04:28 +01:00
"sync"
"gopkg.in/yaml.v3"
)
var ComposeFile = "docker-compose.yaml"
var AppName = "MyApp"
2021-11-30 15:35:32 +01:00
var AppVersion = "0.0.1"
2021-11-30 12:04:28 +01:00
func main() {
flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse")
flag.StringVar(&AppName, "appname", AppName, "Give the helm chart app name")
2021-11-30 15:35:32 +01:00
flag.StringVar(&AppVersion, "appversion", AppVersion, "Set the chart appVersion")
2021-11-30 12:04:28 +01:00
flag.Parse()
p := compose.NewParser(ComposeFile)
p.Parse(AppName)
wait := sync.WaitGroup{}
files := make(map[string][]interface{})
for name, s := range p.Data.Services {
wait.Add(1)
go func(name string, s compose.Service) {
o := generator.CreateReplicaObject(name, s)
files[name] = o
wait.Done()
}(name, s)
}
wait.Wait()
dirname := filepath.Join("chart", AppName)
2021-11-30 15:35:32 +01:00
os.RemoveAll(dirname)
2021-11-30 12:04:28 +01:00
templatesDir := filepath.Join(dirname, "templates")
os.MkdirAll(templatesDir, 0755)
for n, f := range files {
for _, c := range f {
kind := c.(helm.Kinded).Get()
2021-11-30 12:09:07 +01:00
kind = strings.ToLower(kind)
2021-11-30 12:04:28 +01:00
fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
enc.Encode(c)
fp.Close()
}
}
2021-11-30 12:09:07 +01:00
for name, ing := range generator.Ingresses {
fname := filepath.Join(templatesDir, name+".ingress.yaml")
fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp)
2021-11-30 12:04:28 +01:00
enc.SetIndent(2)
2021-11-30 12:17:38 +01:00
fp.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n")
2021-11-30 12:04:28 +01:00
enc.Encode(ing)
2021-11-30 12:17:38 +01:00
fp.WriteString("\n{{- end -}}")
2021-11-30 12:09:07 +01:00
fp.Close()
2021-11-30 12:04:28 +01:00
}
2021-11-30 12:17:38 +01:00
fp, _ := os.Create(filepath.Join(dirname, "values.yaml"))
enc := yaml.NewEncoder(fp)
2021-11-30 12:04:28 +01:00
enc.SetIndent(2)
enc.Encode(generator.Values)
2021-11-30 12:17:38 +01:00
fp.Close()
2021-11-30 12:04:28 +01:00
2021-11-30 12:17:38 +01:00
fp, _ = os.Create(filepath.Join(dirname, "Chart.yaml"))
enc = yaml.NewEncoder(fp)
enc.SetIndent(2)
enc.Encode(map[string]interface{}{
"apiVersion": "v2",
"name": AppName,
"description": "A helm chart for " + AppName,
"type": "application",
"version": "0.1.0",
2021-11-30 15:35:32 +01:00
"appVersion": AppVersion,
2021-11-30 12:17:38 +01:00
})
fp.Close()
2021-11-30 15:35:32 +01:00
fp, _ = os.Create(filepath.Join(templatesDir, "NOTES.txt"))
fp.WriteString(helm.GenNotes(generator.Ingresses))
fp.Close()
2021-11-30 12:04:28 +01:00
}