Fix label ports
This commit is contained in:
@@ -40,7 +40,7 @@ func NewParser(filename string, content ...string) *Parser {
|
|||||||
tmpfile.Close()
|
tmpfile.Close()
|
||||||
filename = tmpfile.Name()
|
filename = tmpfile.Name()
|
||||||
p.temporary = &tmp
|
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 filename is not in cli Default files, add it
|
||||||
if len(filename) > 0 {
|
if len(filename) > 0 {
|
||||||
@@ -56,7 +56,6 @@ func NewParser(filename string, content ...string) *Parser {
|
|||||||
cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...)
|
cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println(cli.DefaultFileNames)
|
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
@@ -15,10 +15,6 @@ import (
|
|||||||
"github.com/compose-spec/compose-go/types"
|
"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 (
|
const (
|
||||||
ICON_PACKAGE = "📦"
|
ICON_PACKAGE = "📦"
|
||||||
ICON_SERVICE = "🔌"
|
ICON_SERVICE = "🔌"
|
||||||
@@ -33,11 +29,15 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Values is kept in memory to create a values.yaml file.
|
// Values is kept in memory to create a values.yaml file.
|
||||||
var Values = make(map[string]map[string]interface{})
|
var (
|
||||||
var VolumeValues = make(map[string]map[string]map[string]interface{})
|
Values = make(map[string]map[string]interface{})
|
||||||
var EmptyDirs = []string{}
|
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
|
OK=0
|
||||||
echo "Checking __service__ port"
|
echo "Checking __service__ port"
|
||||||
while [ $OK != 1 ]; do
|
while [ $OK != 1 ]; do
|
||||||
@@ -48,11 +48,12 @@ echo
|
|||||||
echo "Done"
|
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).
|
// 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{} {
|
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)
|
go parseService(name, s, linked, ret)
|
||||||
return 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.
|
// prepareContainer assigns image, command, env, and labels to a container.
|
||||||
func prepareContainer(container *helm.Container, service types.ServiceConfig, servicename string) {
|
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 there is no image name, this should fail!
|
||||||
if service.Image == "" {
|
if service.Image == "" {
|
||||||
log.Fatal(ICON_PACKAGE+" No image name for service ", servicename)
|
log.Fatal(ICON_PACKAGE+" No image name for service ", servicename)
|
||||||
}
|
}
|
||||||
container.Image = "{{ .Values." + servicename + ".image }}"
|
container.Image = "{{ .Values." + servicename + ".image }}"
|
||||||
container.Command = service.Command
|
container.Command = service.Command
|
||||||
|
locker.Lock()
|
||||||
Values[servicename] = map[string]interface{}{
|
Values[servicename] = map[string]interface{}{
|
||||||
"image": service.Image,
|
"image": service.Image,
|
||||||
}
|
}
|
||||||
|
locker.Unlock()
|
||||||
prepareProbes(servicename, service, container)
|
prepareProbes(servicename, service, container)
|
||||||
generateContainerPorts(service, servicename, container)
|
generateContainerPorts(service, servicename, container)
|
||||||
}
|
}
|
||||||
@@ -159,7 +160,8 @@ func generateServicesAndIngresses(name string, s types.ServiceConfig) []interfac
|
|||||||
ks := helm.NewService(name)
|
ks := helm.NewService(name)
|
||||||
|
|
||||||
for _, p := range s.Ports {
|
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)
|
ks.Spec.Selector = buildSelector(name, s)
|
||||||
|
|
||||||
@@ -286,7 +288,10 @@ func generateContainerPorts(s types.ServiceConfig, name string, container *helm.
|
|||||||
// split port by ","
|
// split port by ","
|
||||||
ports := strings.Split(v, ",")
|
ports := strings.Split(v, ",")
|
||||||
for _, port := range ports {
|
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{
|
container.Ports = append(container.Ports, &helm.ContainerPort{
|
||||||
Name: name,
|
Name: name,
|
||||||
ContainerPort: port,
|
ContainerPort: port,
|
||||||
@@ -343,7 +348,13 @@ func prepareVolumes(deployment, name string, s types.ServiceConfig, container *h
|
|||||||
}
|
}
|
||||||
if isCM {
|
if isCM {
|
||||||
// check if the volname path points on a file, if so, we need to add subvolume to the interface
|
// 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 := ""
|
pointToFile := ""
|
||||||
if !stat.IsDir() {
|
if !stat.IsDir() {
|
||||||
pointToFile = filepath.Base(volname)
|
pointToFile = filepath.Base(volname)
|
||||||
@@ -447,13 +458,13 @@ func prepareInitContainers(name string, s types.ServiceConfig, container *helm.C
|
|||||||
|
|
||||||
foundPort := -1
|
foundPort := -1
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
defer locker.Unlock()
|
|
||||||
if defaultPort, ok := servicesMap[dp]; !ok {
|
if defaultPort, ok := servicesMap[dp]; !ok {
|
||||||
logger.Redf("Error while getting port for service %s\n", dp)
|
logger.Redf("Error while getting port for service %s\n", dp)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
foundPort = defaultPort
|
foundPort = defaultPort
|
||||||
}
|
}
|
||||||
|
locker.Unlock()
|
||||||
if foundPort == -1 {
|
if foundPort == -1 {
|
||||||
log.Fatalf(
|
log.Fatalf(
|
||||||
"ERROR, the %s service is waiting for %s port number, "+
|
"ERROR, the %s service is waiting for %s port number, "+
|
||||||
|
@@ -5,7 +5,6 @@ import (
|
|||||||
"katenary/compose"
|
"katenary/compose"
|
||||||
"katenary/helm"
|
"katenary/helm"
|
||||||
"katenary/logger"
|
"katenary/logger"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -98,7 +97,7 @@ volumes:
|
|||||||
var defaultCliFiles = cli.DefaultFileNames
|
var defaultCliFiles = cli.DefaultFileNames
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
logger.NOLOG = true
|
logger.NOLOG = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUp(t *testing.T) (string, *compose.Parser) {
|
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.
|
// Check if the web2 service has got a command.
|
||||||
func TestCommand(t *testing.T) {
|
func TestCommand(t *testing.T) {
|
||||||
tmp, p := setUp(t)
|
tmp, p := setUp(t)
|
||||||
//defer os.RemoveAll(tmp)
|
defer os.RemoveAll(tmp)
|
||||||
log.Println(tmp)
|
|
||||||
|
|
||||||
for _, service := range p.Data.Services {
|
for _, service := range p.Data.Services {
|
||||||
name := service.Name
|
name := service.Name
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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
|
// Manage services, avoid linked pods and store all services port in servicesMap
|
||||||
avoids := make(map[string]bool)
|
avoids := make(map[string]bool)
|
||||||
linked := make(map[string]types.ServiceConfig, 0)
|
for i, service := range p.Data.Services {
|
||||||
for _, service := range p.Data.Services {
|
|
||||||
n := service.Name
|
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
|
// find port and store it in servicesMap
|
||||||
for _, port := range service.Ports {
|
for _, port := range service.Ports {
|
||||||
target := int(port.Target)
|
target := int(port.Target)
|
||||||
if target != 0 {
|
if target != 0 {
|
||||||
servicesMap[n] = target
|
servicesMap[n] = target
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,16 +73,8 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, composeFi
|
|||||||
//append them in EmptyDirs
|
//append them in EmptyDirs
|
||||||
EmptyDirs = append(EmptyDirs, 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
|
// 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 {
|
if _, found := avoids[name]; found {
|
||||||
continue
|
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)
|
files[name] = CreateReplicaObject(name, s, linked)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
go.mod
1
go.mod
@@ -4,7 +4,6 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/compose-spec/compose-go v1.2.2
|
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/kr/pretty v0.2.0 // indirect
|
||||||
github.com/spf13/cobra v1.4.0
|
github.com/spf13/cobra v1.4.0
|
||||||
golang.org/x/mod v0.5.1
|
golang.org/x/mod v0.5.1
|
||||||
|
2
go.sum
2
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 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
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/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/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/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||||
|
Reference in New Issue
Block a user