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
}

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).
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 }}",