Fix label ports

This commit is contained in:
2022-04-04 13:52:28 +02:00
parent e346bb3ae4
commit 30a79998ab
6 changed files with 57 additions and 36 deletions

View File

@@ -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, "+

View File

@@ -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

View File

@@ -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)
}