From b586b1eac866f1feca87cab92284a5c3d35a2eae Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Tue, 30 Nov 2021 15:45:36 +0100 Subject: [PATCH] More documentation --- compose/parser.go | 2 ++ compose/types.go | 29 ++++++++++++++++------------- generator/main.go | 13 ++++++++++--- helm/deployment.go | 1 + main.go | 2 ++ 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/compose/parser.go b/compose/parser.go index 0a34aa5..7ae32d6 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -7,12 +7,14 @@ import ( "gopkg.in/yaml.v3" ) +// Parser is a docker-compose parser. type Parser struct { Data *Compose } var Appname = "" +// NewParser create a Parser and parse the file given in filename. func NewParser(filename string) *Parser { f, err := os.Open(filename) diff --git a/compose/types.go b/compose/types.go index 9cca9d6..b84fc17 100644 --- a/compose/types.go +++ b/compose/types.go @@ -1,5 +1,21 @@ package compose +// Compose is a complete docker-compse representation. +type Compose struct { + Version string `yaml:"version"` + Services map[string]Service `yaml:"services"` + Volumes map[string]interface{} `yaml:"volumes"` +} + +// NewCompose resturs a Compose object. +func NewCompose() *Compose { + c := &Compose{} + c.Services = make(map[string]Service) + c.Volumes = make(map[string]interface{}) + return c +} + +// Service represent a "service" in a docker-compose file. type Service struct { Image string `yaml:"image"` Ports []string `yaml:"ports"` @@ -9,16 +25,3 @@ type Service struct { Volumes []string `yaml:"volumes"` Expose []int `yaml:"expose"` } - -type Compose struct { - Version string `yaml:"version"` - Services map[string]Service `yaml:"services"` - Volumes map[string]interface{} `yaml:"volumes"` -} - -func NewCompose() *Compose { - c := &Compose{} - c.Services = make(map[string]Service) - c.Volumes = make(map[string]interface{}) - return c -} diff --git a/generator/main.go b/generator/main.go index f859957..54229be 100644 --- a/generator/main.go +++ b/generator/main.go @@ -17,10 +17,13 @@ var serviceWaiters = make(map[string][]chan int) 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. var Values = make(map[string]map[string]interface{}) -var DependScript = ` +var dependScript = ` OK=0 echo "Checking __service__ port" while [ $OK != 1 ]; do @@ -32,6 +35,7 @@ echo echo "Done" ` +// Create a Deployment for a given compose.Service. It returns a list of objects: a Deployment and a possible Service (kubernetes represnetation as maps). func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) { Magenta("Generating deployment for ", name) @@ -86,7 +90,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) { os.Exit(1) } c := helm.NewContainer("check-"+dp, "busybox", nil, s.Labels) - command := strings.ReplaceAll(strings.TrimSpace(DependScript), "__service__", dp) + command := strings.ReplaceAll(strings.TrimSpace(dependScript), "__service__", dp) wait.Add(1) go func(dp string) { @@ -120,6 +124,7 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) { return } +// Create a service (k8s). func createService(name string, s compose.Service) *helm.Service { Magenta("Generating service for ", name) @@ -163,6 +168,7 @@ func createService(name string, s compose.Service) *helm.Service { return ks } +// Create an ingress. func createIngress(name string, port int, s compose.Service) { ingress := helm.NewIngress(name) Values[name]["ingress"] = map[string]interface{}{ @@ -196,6 +202,7 @@ func createIngress(name string, port int, s compose.Service) { 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. func detected(name string, port int) { locker.Lock() servicesMap[name] = port @@ -217,6 +224,7 @@ func getPort(name string) (int, error) { return -1, errors.New("Not found") } +// Waits for a service to be discovered. Sometimes, a deployment depends on another one. See the detected() function. func waitPort(name string) chan int { locker.Lock() c := make(chan int, 0) @@ -231,7 +239,6 @@ func waitPort(name string) chan int { } func buildSelector(name string, s compose.Service) map[string]string { - return map[string]string{ "katenary.io/component": name, "katenary.io/release": "{{ .Release.Name }}", diff --git a/helm/deployment.go b/helm/deployment.go index cb892f4..57cef9c 100644 --- a/helm/deployment.go +++ b/helm/deployment.go @@ -2,6 +2,7 @@ package helm import "strings" +// Deployment is a k8s deployment. type Deployment struct { *K8sBase `yaml:",inline"` Spec *DepSpec `yaml:"spec"` diff --git a/main.go b/main.go index b257cf1..0014fac 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,8 @@ func main() { for name, s := range p.Data.Services { wait.Add(1) + // it's mandatory to make the build in goroutines because some dependencies can + // wait for a port number. So the entire services are built in parallel. go func(name string, s compose.Service) { o := generator.CreateReplicaObject(name, s) files[name] = o