More documentation

This commit is contained in:
2021-11-30 15:45:36 +01:00
parent e18a797504
commit b586b1eac8
5 changed files with 31 additions and 16 deletions

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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 }}",

View File

@@ -2,6 +2,7 @@ package helm
import "strings"
// Deployment is a k8s deployment.
type Deployment struct {
*K8sBase `yaml:",inline"`
Spec *DepSpec `yaml:"spec"`

View File

@@ -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