2021-11-30 12:04:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-30 17:29:42 +01:00
|
|
|
"bytes"
|
2021-11-30 12:04:28 +01:00
|
|
|
"flag"
|
2021-12-01 08:31:51 +01:00
|
|
|
"fmt"
|
|
|
|
"katenary/compose"
|
|
|
|
"katenary/generator"
|
|
|
|
"katenary/helm"
|
2021-11-30 12:04:28 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-12-02 16:07:15 +01:00
|
|
|
"regexp"
|
2021-11-30 12:09:07 +01:00
|
|
|
"strings"
|
2021-12-05 09:05:48 +01:00
|
|
|
"time"
|
2021-11-30 12:04:28 +01:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ComposeFile = "docker-compose.yaml"
|
|
|
|
var AppName = "MyApp"
|
2021-12-01 09:27:12 +01:00
|
|
|
var AppVersion = "0.0.1"
|
|
|
|
var Version = "master"
|
2021-12-01 10:36:43 +01:00
|
|
|
var ChartsDir = "chart"
|
2021-11-30 12:04:28 +01:00
|
|
|
|
2021-12-02 16:07:15 +01:00
|
|
|
var PrefixRE = regexp.MustCompile(`\{\{.*\}\}-?`)
|
|
|
|
|
2021-11-30 12:04:28 +01:00
|
|
|
func main() {
|
2021-12-01 10:36:43 +01:00
|
|
|
flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory")
|
2021-11-30 12:04:28 +01:00
|
|
|
flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse")
|
2021-12-02 14:59:08 +01:00
|
|
|
flag.StringVar(&AppName, "appname", helm.GetProjectName(), "set the helm chart app name")
|
2021-12-01 10:36:43 +01:00
|
|
|
flag.StringVar(&AppVersion, "appversion", AppVersion, "set the chart appVersion")
|
|
|
|
version := flag.Bool("version", false, "Show version and exit")
|
|
|
|
force := flag.Bool("force", false, "force the removal of the chart-dir")
|
2021-11-30 12:04:28 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// Only display the version
|
2021-12-01 08:31:51 +01:00
|
|
|
if *version {
|
2021-12-01 09:27:12 +01:00
|
|
|
fmt.Println(Version)
|
2021-12-01 08:31:51 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// make the appname global (yes... ugly but easy)
|
2021-12-01 15:17:34 +01:00
|
|
|
helm.Appname = AppName
|
2021-12-05 09:05:48 +01:00
|
|
|
helm.Version = Version
|
2021-12-01 10:36:43 +01:00
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
dirname := filepath.Join(ChartsDir, AppName)
|
2021-12-01 10:36:43 +01:00
|
|
|
if _, err := os.Stat(dirname); err == nil && !*force {
|
|
|
|
response := ""
|
|
|
|
for response != "y" && response != "n" {
|
|
|
|
response = "n"
|
2021-12-05 09:05:48 +01:00
|
|
|
fmt.Printf(""+
|
|
|
|
"The %s directory already exists, it will be \x1b[31;1mremoved\x1b[0m!\n"+
|
|
|
|
"Do you really want to continue ? [y/N]: ", dirname)
|
2021-12-01 10:36:43 +01:00
|
|
|
fmt.Scanf("%s", &response)
|
|
|
|
response = strings.ToLower(response)
|
|
|
|
}
|
|
|
|
if response == "n" {
|
2021-12-01 10:47:20 +01:00
|
|
|
fmt.Println("Cancelled")
|
2021-12-01 10:36:43 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// cleanup and create the chart directory (until "templates")
|
2021-12-01 10:36:43 +01:00
|
|
|
os.RemoveAll(dirname)
|
|
|
|
templatesDir := filepath.Join(dirname, "templates")
|
|
|
|
os.MkdirAll(templatesDir, 0755)
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// Parse the compose file now
|
2021-11-30 12:04:28 +01:00
|
|
|
p := compose.NewParser(ComposeFile)
|
|
|
|
p.Parse(AppName)
|
|
|
|
|
2021-12-02 10:21:05 +00:00
|
|
|
files := make(map[string]chan interface{})
|
2021-11-30 12:04:28 +01:00
|
|
|
|
|
|
|
for name, s := range p.Data.Services {
|
2021-12-02 10:21:05 +00:00
|
|
|
o := generator.CreateReplicaObject(name, s)
|
|
|
|
files[name] = o
|
|
|
|
//}(name, s)
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 08:31:51 +01:00
|
|
|
// to generate notes, we need to keep an Ingresses list
|
|
|
|
ingresses := make(map[string]*helm.Ingress)
|
|
|
|
|
2021-11-30 12:04:28 +01:00
|
|
|
for n, f := range files {
|
2021-12-02 10:21:05 +00:00
|
|
|
for c := range f {
|
|
|
|
if c == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-11-30 12:04:28 +01:00
|
|
|
kind := c.(helm.Kinded).Get()
|
2021-11-30 12:09:07 +01:00
|
|
|
kind = strings.ToLower(kind)
|
2021-12-05 09:05:48 +01:00
|
|
|
|
|
|
|
// Add a SHA inside the generated file, it's only
|
|
|
|
// to make it easy to check it the compose file corresponds to the
|
|
|
|
// generated helm chart
|
2021-12-01 15:17:34 +01:00
|
|
|
c.(helm.Signable).BuildSHA(ComposeFile)
|
2021-12-05 09:05:48 +01:00
|
|
|
|
|
|
|
// Some types need special fixes in yaml generation
|
2021-11-30 17:29:42 +01:00
|
|
|
switch c := c.(type) {
|
|
|
|
case *helm.Storage:
|
2021-12-05 09:05:48 +01:00
|
|
|
// For storage, we need to add a "condition" to activate it
|
2021-12-01 16:50:32 +01:00
|
|
|
fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
|
|
|
|
fp, _ := os.Create(fname)
|
2021-11-30 17:29:42 +01:00
|
|
|
volname := c.K8sBase.Metadata.Labels[helm.K+"/pvc-name"]
|
|
|
|
fp.WriteString("{{ if .Values." + n + ".persistence." + volname + ".enabled }}\n")
|
|
|
|
enc := yaml.NewEncoder(fp)
|
|
|
|
enc.SetIndent(2)
|
|
|
|
enc.Encode(c)
|
|
|
|
fp.WriteString("{{- end -}}")
|
|
|
|
case *helm.Deployment:
|
2021-12-05 09:05:48 +01:00
|
|
|
// for the deployment, we need to fix persitence volumes to be activated
|
|
|
|
// only when the storage is "enabled", either we use an "emptyDir"
|
2021-12-01 16:50:32 +01:00
|
|
|
fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
|
|
|
|
fp, _ := os.Create(fname)
|
2021-11-30 17:32:14 +01:00
|
|
|
buffer := bytes.NewBuffer(nil)
|
2021-11-30 17:29:42 +01:00
|
|
|
enc := yaml.NewEncoder(buffer)
|
|
|
|
enc.SetIndent(2)
|
|
|
|
enc.Encode(c)
|
|
|
|
_content := string(buffer.Bytes())
|
|
|
|
content := strings.Split(string(_content), "\n")
|
|
|
|
dataname := ""
|
|
|
|
component := c.Spec.Selector["matchLabels"].(map[string]string)[helm.K+"/component"]
|
|
|
|
for _, line := range content {
|
|
|
|
if strings.Contains(line, "name:") {
|
|
|
|
dataname = strings.Split(line, ":")[1]
|
|
|
|
dataname = strings.TrimSpace(dataname)
|
|
|
|
} else if strings.Contains(line, "persistentVolumeClaim") {
|
|
|
|
line = " {{- if .Values." + component + ".persistence." + dataname + ".enabled }}\n" + line
|
|
|
|
} else if strings.Contains(line, "claimName") {
|
|
|
|
line += "\n {{ else }}"
|
|
|
|
line += "\n emptyDir: {}"
|
|
|
|
line += "\n {{- end }}"
|
|
|
|
}
|
|
|
|
fp.WriteString(line + "\n")
|
|
|
|
}
|
2021-12-01 16:50:32 +01:00
|
|
|
fp.Close()
|
2021-12-02 10:21:05 +00:00
|
|
|
|
2021-12-01 16:50:32 +01:00
|
|
|
case *helm.Service:
|
2021-12-05 09:05:48 +01:00
|
|
|
// Change the type for service if it's an "exposed" port
|
2021-12-01 16:50:32 +01:00
|
|
|
suffix := ""
|
|
|
|
if c.Spec.Type == "NodePort" {
|
|
|
|
suffix = "-external"
|
|
|
|
}
|
|
|
|
fname := filepath.Join(templatesDir, n+suffix+"."+kind+".yaml")
|
|
|
|
fp, _ := os.Create(fname)
|
|
|
|
enc := yaml.NewEncoder(fp)
|
2021-12-02 10:21:05 +00:00
|
|
|
enc.SetIndent(2)
|
2021-12-01 16:50:32 +01:00
|
|
|
enc.Encode(c)
|
|
|
|
fp.Close()
|
|
|
|
|
2021-12-01 08:31:51 +01:00
|
|
|
case *helm.Ingress:
|
2021-12-05 09:05:48 +01:00
|
|
|
// we need to make ingresses "activable" from values
|
2021-12-02 10:21:05 +00:00
|
|
|
buffer := bytes.NewBuffer(nil)
|
2021-12-01 16:50:32 +01:00
|
|
|
fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
|
2021-12-01 08:31:51 +01:00
|
|
|
ingresses[n] = c // keep it to generate notes
|
2021-12-02 10:21:05 +00:00
|
|
|
enc := yaml.NewEncoder(buffer)
|
2021-12-01 08:31:51 +01:00
|
|
|
enc.SetIndent(2)
|
2021-12-02 10:21:05 +00:00
|
|
|
buffer.WriteString("{{- if .Values." + n + ".ingress.enabled -}}\n")
|
2021-12-01 08:31:51 +01:00
|
|
|
enc.Encode(c)
|
2021-12-02 10:21:05 +00:00
|
|
|
buffer.WriteString("{{- end -}}")
|
|
|
|
|
|
|
|
fp, _ := os.Create(fname)
|
|
|
|
content := string(buffer.Bytes())
|
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
for _, l := range lines {
|
|
|
|
if strings.Contains(l, "ingressClassName") {
|
|
|
|
p := strings.Split(l, ":")
|
|
|
|
condition := p[1]
|
|
|
|
condition = strings.ReplaceAll(condition, "'", "")
|
|
|
|
condition = strings.ReplaceAll(condition, "{{", "")
|
|
|
|
condition = strings.ReplaceAll(condition, "}}", "")
|
|
|
|
condition = strings.TrimSpace(condition)
|
|
|
|
condition = "{{- if " + condition + " }}"
|
|
|
|
l = " " + condition + "\n" + l + "\n {{- end }}"
|
|
|
|
}
|
|
|
|
fp.WriteString(l + "\n")
|
|
|
|
}
|
2021-12-01 16:50:32 +01:00
|
|
|
fp.Close()
|
2021-12-01 08:31:51 +01:00
|
|
|
|
2021-12-02 16:07:15 +01:00
|
|
|
case *helm.ConfigMap, *helm.Secret:
|
|
|
|
// there could be several files, so let's force the filename
|
|
|
|
name := c.(helm.Named).Name()
|
|
|
|
name = PrefixRE.ReplaceAllString(name, "")
|
|
|
|
fname := filepath.Join(templatesDir, n+"."+name+"."+kind+".yaml")
|
|
|
|
fp, _ := os.Create(fname)
|
|
|
|
enc := yaml.NewEncoder(fp)
|
|
|
|
enc.SetIndent(2)
|
|
|
|
enc.Encode(c)
|
|
|
|
fp.Close()
|
|
|
|
|
2021-11-30 17:29:42 +01:00
|
|
|
default:
|
2021-12-01 16:50:32 +01:00
|
|
|
fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
|
|
|
|
fp, _ := os.Create(fname)
|
2021-11-30 17:29:42 +01:00
|
|
|
enc := yaml.NewEncoder(fp)
|
|
|
|
enc.SetIndent(2)
|
|
|
|
enc.Encode(c)
|
2021-12-01 16:50:32 +01:00
|
|
|
fp.Close()
|
2021-11-30 17:29:42 +01:00
|
|
|
}
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// Create the values.yaml file
|
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-12-05 09:05:48 +01:00
|
|
|
// Create tht Chart.yaml file
|
2021-11-30 12:17:38 +01:00
|
|
|
fp, _ = os.Create(filepath.Join(dirname, "Chart.yaml"))
|
2021-12-05 09:05:48 +01:00
|
|
|
fp.WriteString(`# Create on ` + time.Now().Format(time.RFC3339) + "\n")
|
|
|
|
fp.WriteString(`# Katenary command line: ` + strings.Join(os.Args, " ") + "\n")
|
2021-11-30 12:17:38 +01:00
|
|
|
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
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
// And finally, create a NOTE.txt file
|
2021-11-30 15:35:32 +01:00
|
|
|
fp, _ = os.Create(filepath.Join(templatesDir, "NOTES.txt"))
|
2021-12-05 09:05:48 +01:00
|
|
|
fp.WriteString(helm.GenerateNotesFile(ingresses))
|
2021-11-30 15:35:32 +01:00
|
|
|
fp.Close()
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|