From 87e26ca0214ecf2679c5316b9c68209b57f0f7bf Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 3 Dec 2021 11:49:32 +0100 Subject: [PATCH] Check dependencies before to crash... --- compose/parser.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ compose/types.go | 4 ++-- generator/main.go | 27 +++++---------------------- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/compose/parser.go b/compose/parser.go index 7ae32d6..85a3ab6 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -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 } diff --git a/compose/types.go b/compose/types.go index 38deff1..1c5f800 100644 --- a/compose/types.go +++ b/compose/types.go @@ -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 } diff --git a/generator/main.go b/generator/main.go index 7d0085a..7bf4955 100644 --- a/generator/main.go +++ b/generator/main.go @@ -46,31 +46,14 @@ 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) chan interface{} { - - // fetch label to specific exposed port, and add them in "ports" section - 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) - } - } - } - +func CreateReplicaObject(name string, s *compose.Service) chan interface{} { ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+1) go parseService(name, s, ret) return ret } // This function will try to yied deployment and services based on a service from the compose file structure. -func parseService(name string, s compose.Service, ret chan interface{}) { +func parseService(name string, s *compose.Service, ret chan interface{}) { Magenta(ICON_PACKAGE+" Generating deployment for ", name) o := helm.NewDeployment(name) @@ -317,7 +300,7 @@ func parseService(name string, s compose.Service, ret chan interface{}) { } // Create a service (k8s). -func createService(name string, s compose.Service) []interface{} { +func createService(name string, s *compose.Service) []interface{} { ret := make([]interface{}, 0) Magenta(ICON_SERVICE+" Generating service for ", name) @@ -363,7 +346,7 @@ func createService(name string, s compose.Service) []interface{} { } // Create an ingress. -func createIngress(name string, port int, s compose.Service) *helm.Ingress { +func createIngress(name string, port int, s *compose.Service) *helm.Ingress { ingress := helm.NewIngress(name) Values[name]["ingress"] = map[string]interface{}{ "class": "nginx", @@ -433,7 +416,7 @@ func waitPort(name string) chan int { return c } -func buildSelector(name string, s compose.Service) map[string]string { +func buildSelector(name string, s *compose.Service) map[string]string { return map[string]string{ "katenary.io/component": name, "katenary.io/release": "{{ .Release.Name }}",