Check dependencies before to crash...

This commit is contained in:
2021-12-03 11:49:32 +01:00
parent f145a27a3d
commit 87e26ca021
3 changed files with 52 additions and 24 deletions

View File

@@ -1,8 +1,11 @@
package compose
import (
"fmt"
"katenary/helm"
"log"
"os"
"strings"
"gopkg.in/yaml.v3"
)
@@ -27,6 +30,48 @@ func NewParser(filename string) *Parser {
p := &Parser{Data: c}
services := make(map[string][]string)
// get the service list, to be sure that everything is ok
for name, s := range c.Services {
if portlabel, ok := s.Labels[helm.LABEL_PORT]; ok {
services := strings.Split(portlabel, ",")
for _, serviceport := range services {
portexists := false
for _, found := range s.Ports {
if found == serviceport {
portexists = true
}
}
if !portexists {
s.Ports = append(s.Ports, serviceport)
}
}
}
if len(s.Ports) > 0 {
services[name] = s.Ports
}
}
// check if dependencies are resolved
missing := []string{}
for name, s := range c.Services {
for _, dep := range s.DependsOn {
if _, ok := services[dep]; !ok {
missing = append(missing, fmt.Sprintf(
"The service \"%s\" hasn't got "+
"declared port for dependency from \"%s\" - please "+
"append a %s label or a \"ports\" section in the docker-compose file",
dep, name, helm.LABEL_PORT),
)
}
}
}
if len(missing) > 0 {
log.Fatal(strings.Join(missing, "\n"))
}
return p
}

View File

@@ -3,14 +3,14 @@ package compose
// Compose is a complete docker-compse representation.
type Compose struct {
Version string `yaml:"version"`
Services map[string]Service `yaml:"services"`
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.Services = make(map[string]*Service)
c.Volumes = make(map[string]interface{})
return c
}