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 package compose
import ( import (
"fmt"
"katenary/helm"
"log" "log"
"os" "os"
"strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -27,6 +30,48 @@ func NewParser(filename string) *Parser {
p := &Parser{Data: c} 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 return p
} }

View File

@@ -3,14 +3,14 @@ package compose
// Compose is a complete docker-compse representation. // Compose is a complete docker-compse representation.
type Compose struct { type Compose struct {
Version string `yaml:"version"` Version string `yaml:"version"`
Services map[string]Service `yaml:"services"` Services map[string]*Service `yaml:"services"`
Volumes map[string]interface{} `yaml:"volumes"` Volumes map[string]interface{} `yaml:"volumes"`
} }
// NewCompose resturs a Compose object. // NewCompose resturs a Compose object.
func NewCompose() *Compose { func NewCompose() *Compose {
c := &Compose{} c := &Compose{}
c.Services = make(map[string]Service) c.Services = make(map[string]*Service)
c.Volumes = make(map[string]interface{}) c.Volumes = make(map[string]interface{})
return c return c
} }

View File

@@ -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). // 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{} { 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)
}
}
}
ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+1) ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+1)
go parseService(name, s, ret) go parseService(name, s, ret)
return ret return ret
} }
// This function will try to yied deployment and services based on a service from the compose file structure. // 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) Magenta(ICON_PACKAGE+" Generating deployment for ", name)
o := helm.NewDeployment(name) o := helm.NewDeployment(name)
@@ -317,7 +300,7 @@ func parseService(name string, s compose.Service, ret chan interface{}) {
} }
// Create a service (k8s). // Create a service (k8s).
func createService(name string, s compose.Service) []interface{} { func createService(name string, s *compose.Service) []interface{} {
ret := make([]interface{}, 0) ret := make([]interface{}, 0)
Magenta(ICON_SERVICE+" Generating service for ", name) Magenta(ICON_SERVICE+" Generating service for ", name)
@@ -363,7 +346,7 @@ func createService(name string, s compose.Service) []interface{} {
} }
// Create an ingress. // 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) ingress := helm.NewIngress(name)
Values[name]["ingress"] = map[string]interface{}{ Values[name]["ingress"] = map[string]interface{}{
"class": "nginx", "class": "nginx",
@@ -433,7 +416,7 @@ func waitPort(name string) chan int {
return c 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{ return map[string]string{
"katenary.io/component": name, "katenary.io/component": name,
"katenary.io/release": "{{ .Release.Name }}", "katenary.io/release": "{{ .Release.Name }}",