diff --git a/compose/parser.go b/compose/parser.go index 7538d7f..427176a 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -40,7 +40,7 @@ func NewParser(filename string, content ...string) *Parser { tmpfile.Close() filename = tmpfile.Name() p.temporary = &tmp - cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...) + cli.DefaultFileNames = []string{filename} } // if filename is not in cli Default files, add it if len(filename) > 0 { @@ -56,7 +56,6 @@ func NewParser(filename string, content ...string) *Parser { cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...) } } - log.Println(cli.DefaultFileNames) return p } diff --git a/generator/main.go b/generator/main.go index a013df6..1afb380 100644 --- a/generator/main.go +++ b/generator/main.go @@ -15,10 +15,6 @@ import ( "github.com/compose-spec/compose-go/types" ) -var servicesMap = make(map[string]int) -var serviceWaiters = make(map[string][]chan int) -var locker = &sync.Mutex{} - const ( ICON_PACKAGE = "📦" ICON_SERVICE = "🔌" @@ -33,11 +29,15 @@ const ( ) // Values is kept in memory to create a values.yaml file. -var Values = make(map[string]map[string]interface{}) -var VolumeValues = make(map[string]map[string]map[string]interface{}) -var EmptyDirs = []string{} +var ( + Values = make(map[string]map[string]interface{}) + VolumeValues = make(map[string]map[string]map[string]interface{}) + EmptyDirs = []string{} + servicesMap = make(map[string]int) + serviceWaiters = make(map[string][]chan int) + locker = &sync.Mutex{} -var dependScript = ` + dependScript = ` OK=0 echo "Checking __service__ port" while [ $OK != 1 ]; do @@ -48,11 +48,12 @@ echo echo "Done" ` -var madeDeployments = make(map[string]helm.Deployment, 0) + madeDeployments = make(map[string]helm.Deployment, 0) +) // 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 types.ServiceConfig, linked map[string]types.ServiceConfig) chan interface{} { - ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+1) + ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+2) go parseService(name, s, linked, ret) return ret } @@ -136,17 +137,17 @@ func parseService(name string, s types.ServiceConfig, linked map[string]types.Se // prepareContainer assigns image, command, env, and labels to a container. func prepareContainer(container *helm.Container, service types.ServiceConfig, servicename string) { - locker.Lock() - defer locker.Unlock() // if there is no image name, this should fail! if service.Image == "" { log.Fatal(ICON_PACKAGE+" No image name for service ", servicename) } container.Image = "{{ .Values." + servicename + ".image }}" container.Command = service.Command + locker.Lock() Values[servicename] = map[string]interface{}{ "image": service.Image, } + locker.Unlock() prepareProbes(servicename, service, container) generateContainerPorts(service, servicename, container) } @@ -159,7 +160,8 @@ func generateServicesAndIngresses(name string, s types.ServiceConfig) []interfac ks := helm.NewService(name) for _, p := range s.Ports { - ks.Spec.Ports = append(ks.Spec.Ports, helm.NewServicePort(int(p.Target), int(p.Target))) + target := int(p.Target) + ks.Spec.Ports = append(ks.Spec.Ports, helm.NewServicePort(target, target)) } ks.Spec.Selector = buildSelector(name, s) @@ -286,7 +288,10 @@ func generateContainerPorts(s types.ServiceConfig, name string, container *helm. // split port by "," ports := strings.Split(v, ",") for _, port := range ports { - port, _ := strconv.Atoi(port) + port, err := strconv.Atoi(port) + if err != nil { + log.Fatalf("The given port \"%v\" as container port in \"%s\" service is not an integer\n", v, name) + } container.Ports = append(container.Ports, &helm.ContainerPort{ Name: name, ContainerPort: port, @@ -343,7 +348,13 @@ func prepareVolumes(deployment, name string, s types.ServiceConfig, container *h } if isCM { // check if the volname path points on a file, if so, we need to add subvolume to the interface - stat, _ := os.Stat(volname) + stat, err := os.Stat(volname) + if err != nil { + logger.ActivateColors = true + logger.Redf("An error occured reading volume path %s\n", err.Error()) + logger.ActivateColors = false + continue + } pointToFile := "" if !stat.IsDir() { pointToFile = filepath.Base(volname) @@ -447,13 +458,13 @@ func prepareInitContainers(name string, s types.ServiceConfig, container *helm.C foundPort := -1 locker.Lock() - defer locker.Unlock() if defaultPort, ok := servicesMap[dp]; !ok { logger.Redf("Error while getting port for service %s\n", dp) os.Exit(1) } else { foundPort = defaultPort } + locker.Unlock() if foundPort == -1 { log.Fatalf( "ERROR, the %s service is waiting for %s port number, "+ diff --git a/generator/main_test.go b/generator/main_test.go index df43dd0..2cde4a1 100644 --- a/generator/main_test.go +++ b/generator/main_test.go @@ -5,7 +5,6 @@ import ( "katenary/compose" "katenary/helm" "katenary/logger" - "log" "os" "path/filepath" "strings" @@ -98,7 +97,7 @@ volumes: var defaultCliFiles = cli.DefaultFileNames func init() { - logger.NOLOG = true + logger.NOLOG = false } func setUp(t *testing.T) (string, *compose.Parser) { @@ -120,8 +119,7 @@ func setUp(t *testing.T) (string, *compose.Parser) { // Check if the web2 service has got a command. func TestCommand(t *testing.T) { tmp, p := setUp(t) - //defer os.RemoveAll(tmp) - log.Println(tmp) + defer os.RemoveAll(tmp) for _, service := range p.Data.Services { name := service.Name diff --git a/generator/writer.go b/generator/writer.go index 55a0425..fd61e14 100644 --- a/generator/writer.go +++ b/generator/writer.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "regexp" + "strconv" "strings" "time" @@ -34,15 +35,29 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, composeFi // Manage services, avoid linked pods and store all services port in servicesMap avoids := make(map[string]bool) - linked := make(map[string]types.ServiceConfig, 0) - for _, service := range p.Data.Services { + for i, service := range p.Data.Services { n := service.Name + if ports, ok := service.Labels[helm.LABEL_PORT]; ok { + if service.Ports == nil { + service.Ports = make([]types.ServicePortConfig, 0) + } + for _, port := range strings.Split(ports, ",") { + target, err := strconv.Atoi(port) + if err != nil { + log.Fatal(err) + } + service.Ports = append(service.Ports, types.ServicePortConfig{ + Target: uint32(target), + }) + } + } // find port and store it in servicesMap for _, port := range service.Ports { target := int(port.Target) if target != 0 { servicesMap[n] = target + break } } @@ -58,16 +73,8 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, composeFi //append them in EmptyDirs EmptyDirs = append(EmptyDirs, emptyDirs...) } + p.Data.Services[i] = service - // find service linked to this one - for _, service := range p.Data.Services { - n := service.Name - for _, label := range service.Labels { - if label == helm.LABEL_SAMEPOD { - linked[n] = service - } - } - } } // for all services in linked map, and not in avoids map, generate the service @@ -77,6 +84,15 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, composeFi if _, found := avoids[name]; found { continue } + linked := make(map[string]types.ServiceConfig, 0) + // find service + for _, service := range p.Data.Services { + n := service.Name + if linkname, ok := service.Labels[helm.LABEL_SAMEPOD]; ok && linkname == name { + linked[n] = service + } + } + files[name] = CreateReplicaObject(name, s, linked) } diff --git a/go.mod b/go.mod index f5e777e..4e007b6 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.16 require ( github.com/compose-spec/compose-go v1.2.2 - github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/kr/pretty v0.2.0 // indirect github.com/spf13/cobra v1.4.0 golang.org/x/mod v0.5.1 diff --git a/go.sum b/go.sum index 1638ead..ac3e4ea 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,6 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=