Better contruction, changed mod name

This commit is contained in:
2021-12-01 08:31:51 +01:00
parent bee8b48f16
commit a36811020d
3 changed files with 38 additions and 34 deletions

View File

@@ -2,8 +2,8 @@ package generator
import ( import (
"fmt" "fmt"
"helm-compose/compose" "katenary/compose"
"helm-compose/helm" "katenary/helm"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@@ -15,10 +15,6 @@ import (
var servicesMap = make(map[string]int) var servicesMap = make(map[string]int)
var serviceWaiters = make(map[string][]chan int) var serviceWaiters = make(map[string][]chan int)
var locker = &sync.Mutex{} var locker = &sync.Mutex{}
var serviceTick = make(chan int, 0)
// Ingresses is kept in memory to create ingresses.
var Ingresses = make(map[string]*helm.Ingress, 0)
// Values is kept in memory to create a values.yaml file. // Values is kept in memory to create a values.yaml file.
var Values = make(map[string]map[string]interface{}) var Values = make(map[string]map[string]interface{})
@@ -156,7 +152,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
if len(s.Ports) > 0 || len(s.Expose) > 0 { if len(s.Ports) > 0 || len(s.Expose) > 0 {
ks := createService(name, s) ks := createService(name, s)
ret = append(ret, ks) ret = append(ret, ks...)
} }
if len(VolumeValues[name]) > 0 { if len(VolumeValues[name]) > 0 {
@@ -169,7 +165,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
} }
// Create a service (k8s). // Create a service (k8s).
func createService(name string, s compose.Service) *helm.Service { func createService(name string, s compose.Service) []interface{} {
Magenta("Generating service for ", name) Magenta("Generating service for ", name)
ks := helm.NewService() ks := helm.NewService()
@@ -204,16 +200,19 @@ func createService(name string, s compose.Service) *helm.Service {
ks.Spec.Selector = buildSelector(name, s) ks.Spec.Selector = buildSelector(name, s)
ret := make([]interface{}, 0)
ret = append(ret, ks)
if v, ok := s.Labels[helm.K+"/expose-ingress"]; ok && v == "true" { if v, ok := s.Labels[helm.K+"/expose-ingress"]; ok && v == "true" {
createIngress(name, defaultPort, s) ing := createIngress(name, defaultPort, s)
ret = append(ret, ing)
} }
Green("Done service ", name) Green("Done service ", name)
return ks return ret
} }
// Create an ingress. // Create an ingress.
func createIngress(name string, port int, s compose.Service) { func createIngress(name string, port int, s compose.Service) *helm.Ingress {
ingress := helm.NewIngress(name) ingress := helm.NewIngress(name)
Values[name]["ingress"] = map[string]interface{}{ Values[name]["ingress"] = map[string]interface{}{
"class": "nginx", "class": "nginx",
@@ -241,9 +240,7 @@ func createIngress(name string, port int, s compose.Service) {
} }
ingress.SetIngressClass(name) ingress.SetIngressClass(name)
locker.Lock() return ingress
Ingresses[name] = ingress
locker.Unlock()
} }
// This function is called when a possible service is detected, it append the port in a map to make others to be able to get the service name. It also try to send the data to any "waiter" for this service. // This function is called when a possible service is detected, it append the port in a map to make others to be able to get the service name. It also try to send the data to any "waiter" for this service.

2
go.mod
View File

@@ -1,4 +1,4 @@
module helm-compose module katenary
go 1.16 go 1.16

45
main.go
View File

@@ -3,9 +3,10 @@ package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"helm-compose/compose" "fmt"
"helm-compose/generator" "katenary/compose"
"helm-compose/helm" "katenary/generator"
"katenary/helm"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -16,15 +17,21 @@ import (
var ComposeFile = "docker-compose.yaml" var ComposeFile = "docker-compose.yaml"
var AppName = "MyApp" var AppName = "MyApp"
var AppVersion = "0.0.1" var AppVersion = "master"
func main() { func main() {
flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse") flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse")
flag.StringVar(&AppName, "appname", AppName, "Give the helm chart app name") flag.StringVar(&AppName, "appname", AppName, "Give the helm chart app name")
flag.StringVar(&AppVersion, "appversion", AppVersion, "Set the chart appVersion") flag.StringVar(&AppVersion, "appversion", AppVersion, "Set the chart appVersion")
version := flag.Bool("version", false, "Show version and exist")
flag.Parse() flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
p := compose.NewParser(ComposeFile) p := compose.NewParser(ComposeFile)
p.Parse(AppName) p.Parse(AppName)
wait := sync.WaitGroup{} wait := sync.WaitGroup{}
@@ -33,8 +40,9 @@ func main() {
for name, s := range p.Data.Services { for name, s := range p.Data.Services {
wait.Add(1) wait.Add(1)
// it's mandatory to make the build in goroutines because some dependencies can // it's mandatory to build in goroutines because some dependencies can
// wait for a port number. So the entire services are built in parallel. // wait for a port number discovery.
// So the entire services are built in parallel.
go func(name string, s compose.Service) { go func(name string, s compose.Service) {
o := generator.CreateReplicaObject(name, s) o := generator.CreateReplicaObject(name, s)
files[name] = o files[name] = o
@@ -48,6 +56,9 @@ func main() {
templatesDir := filepath.Join(dirname, "templates") templatesDir := filepath.Join(dirname, "templates")
os.MkdirAll(templatesDir, 0755) os.MkdirAll(templatesDir, 0755)
// to generate notes, we need to keep an Ingresses list
ingresses := make(map[string]*helm.Ingress)
for n, f := range files { for n, f := range files {
for _, c := range f { for _, c := range f {
kind := c.(helm.Kinded).Get() kind := c.(helm.Kinded).Get()
@@ -84,6 +95,14 @@ func main() {
} }
fp.WriteString(line + "\n") fp.WriteString(line + "\n")
} }
case *helm.Ingress:
ingresses[n] = c // keep it to generate notes
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
fp.WriteString("{{- if .Values." + n + ".ingress.enabled -}}\n")
enc.Encode(c)
fp.WriteString("{{- end -}}")
default: default:
enc := yaml.NewEncoder(fp) enc := yaml.NewEncoder(fp)
enc.SetIndent(2) enc.SetIndent(2)
@@ -93,17 +112,6 @@ func main() {
} }
} }
for name, ing := range generator.Ingresses {
fname := filepath.Join(templatesDir, name+".ingress.yaml")
fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp)
enc.SetIndent(2)
fp.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n")
enc.Encode(ing)
fp.WriteString("{{- end -}}")
fp.Close()
}
fp, _ := os.Create(filepath.Join(dirname, "values.yaml")) fp, _ := os.Create(filepath.Join(dirname, "values.yaml"))
enc := yaml.NewEncoder(fp) enc := yaml.NewEncoder(fp)
enc.SetIndent(2) enc.SetIndent(2)
@@ -124,7 +132,6 @@ func main() {
fp.Close() fp.Close()
fp, _ = os.Create(filepath.Join(templatesDir, "NOTES.txt")) fp, _ = os.Create(filepath.Join(templatesDir, "NOTES.txt"))
fp.WriteString(helm.GenNotes(generator.Ingresses)) fp.WriteString(helm.GenNotes(ingresses))
fp.Close() fp.Close()
} }