Fix samepod generation
The container was not merged to the target deployment. It necessary to make one more loop to apply the container + remove the source deployment.
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
package generator
|
package generator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"katenary/utils"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"katenary/utils"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
goyaml "gopkg.in/yaml.v3"
|
goyaml "gopkg.in/yaml.v3"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@@ -53,7 +54,6 @@ type ConfigMap struct {
|
|||||||
// NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name.
|
// NewConfigMap creates a new ConfigMap from a compose service. The appName is the name of the application taken from the project name.
|
||||||
// The ConfigMap is filled by environment variables and labels "map-env".
|
// The ConfigMap is filled by environment variables and labels "map-env".
|
||||||
func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
|
func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
|
||||||
|
|
||||||
done := map[string]bool{}
|
done := map[string]bool{}
|
||||||
drop := map[string]bool{}
|
drop := map[string]bool{}
|
||||||
secrets := []string{}
|
secrets := []string{}
|
||||||
@@ -98,7 +98,7 @@ func NewConfigMap(service types.ServiceConfig, appName string) *ConfigMap {
|
|||||||
done[value] = true
|
done[value] = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//val := `{{ tpl .Values.` + service.Name + `.environment.` + value + ` $ }}`
|
// val := `{{ tpl .Values.` + service.Name + `.environment.` + value + ` $ }}`
|
||||||
val := utils.TplValue(service.Name, "environment."+value)
|
val := utils.TplValue(service.Name, "environment."+value)
|
||||||
service.Environment[value] = &val
|
service.Environment[value] = &val
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,6 @@ func (c *ConfigMap) AppendDir(path string) {
|
|||||||
stat, err := os.Stat(path)
|
stat, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Path %s does not exist\n", path)
|
log.Fatalf("Path %s does not exist\n", path)
|
||||||
|
|
||||||
}
|
}
|
||||||
// recursively read all files in the path and add them to the configmap
|
// recursively read all files in the path and add them to the configmap
|
||||||
if stat.IsDir() {
|
if stat.IsDir() {
|
||||||
|
@@ -124,6 +124,7 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) {
|
|||||||
name := utils.GetServiceNameByPort(int(port.Target))
|
name := utils.GetServiceNameByPort(int(port.Target))
|
||||||
if name == "" {
|
if name == "" {
|
||||||
utils.Warn("Port name not found for port ", port.Target, " in service ", service.Name, ". Using port number instead")
|
utils.Warn("Port name not found for port ", port.Target, " in service ", service.Name, ". Using port number instead")
|
||||||
|
name = fmt.Sprintf("port-%d", port.Target)
|
||||||
}
|
}
|
||||||
ports = append(ports, corev1.ContainerPort{
|
ports = append(ports, corev1.ContainerPort{
|
||||||
ContainerPort: int32(port.Target),
|
ContainerPort: int32(port.Target),
|
||||||
@@ -288,14 +289,11 @@ func (d *Deployment) AddVolumes(service types.ServiceConfig, appName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) {
|
func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) {
|
||||||
log.Printf("In %s deployment, add volumes for service %s from binded deployment %s", d.Name, service.Name, binded.Name)
|
|
||||||
// find the volume in the binded deployment
|
// find the volume in the binded deployment
|
||||||
for _, bindedVolume := range binded.Spec.Template.Spec.Volumes {
|
for _, bindedVolume := range binded.Spec.Template.Spec.Volumes {
|
||||||
log.Println("bindedVolume.Name found", bindedVolume.Name)
|
|
||||||
skip := false
|
skip := false
|
||||||
for _, targetVol := range d.Spec.Template.Spec.Volumes {
|
for _, targetVol := range d.Spec.Template.Spec.Volumes {
|
||||||
if targetVol.Name == bindedVolume.Name {
|
if targetVol.Name == bindedVolume.Name {
|
||||||
log.Println("Volume", bindedVolume.Name, "already exists in deployment", d.Name)
|
|
||||||
skip = true
|
skip = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -303,16 +301,13 @@ func (d *Deployment) BindFrom(service types.ServiceConfig, binded *Deployment) {
|
|||||||
if !skip {
|
if !skip {
|
||||||
// add the volume to the current deployment
|
// add the volume to the current deployment
|
||||||
d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, bindedVolume)
|
d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, bindedVolume)
|
||||||
log.Println("d.Spec.Template.Spec.Volumes", d.Spec.Template.Spec.Volumes)
|
|
||||||
// get the container
|
// get the container
|
||||||
|
|
||||||
}
|
}
|
||||||
// add volume mount to the container
|
// add volume mount to the container
|
||||||
targetContainer, ti := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers)
|
targetContainer, ti := utils.GetContainerByName(service.Name, d.Spec.Template.Spec.Containers)
|
||||||
sourceContainer, _ := utils.GetContainerByName(service.Name, binded.Spec.Template.Spec.Containers)
|
sourceContainer, _ := utils.GetContainerByName(service.Name, binded.Spec.Template.Spec.Containers)
|
||||||
for _, bindedMount := range sourceContainer.VolumeMounts {
|
for _, bindedMount := range sourceContainer.VolumeMounts {
|
||||||
if bindedMount.Name == bindedVolume.Name {
|
if bindedMount.Name == bindedVolume.Name {
|
||||||
log.Println("bindedMount.Name found", bindedMount.Name)
|
|
||||||
targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount)
|
targetContainer.VolumeMounts = append(targetContainer.VolumeMounts, bindedMount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -38,7 +38,7 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
appName = project.Name
|
appName = project.Name
|
||||||
deployments = make(map[string]*Deployment, len(project.Services))
|
deployments = make(map[string]*Deployment, len(project.Services))
|
||||||
services = make(map[string]*Service)
|
services = make(map[string]*Service)
|
||||||
podToMerge = make(map[string]*Deployment)
|
podToMerge = make(map[string]*types.ServiceConfig)
|
||||||
)
|
)
|
||||||
chart := NewChart(appName)
|
chart := NewChart(appName)
|
||||||
|
|
||||||
@@ -54,7 +54,6 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
mainCount := 0
|
mainCount := 0
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
if serviceIsMain(service) {
|
if serviceIsMain(service) {
|
||||||
log.Printf("Found main app %s", service.Name)
|
|
||||||
mainCount++
|
mainCount++
|
||||||
if mainCount > 1 {
|
if mainCount > 1 {
|
||||||
return nil, fmt.Errorf("found more than one main app")
|
return nil, fmt.Errorf("found more than one main app")
|
||||||
@@ -96,7 +95,7 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
// get the same-pod label if exists, add it to the list.
|
// get the same-pod label if exists, add it to the list.
|
||||||
// We later will copy some parts to the target deployment and remove this one.
|
// We later will copy some parts to the target deployment and remove this one.
|
||||||
if samePod, ok := service.Labels[LABEL_SAME_POD]; ok && samePod != "" {
|
if samePod, ok := service.Labels[LABEL_SAME_POD]; ok && samePod != "" {
|
||||||
podToMerge[samePod] = d
|
podToMerge[samePod] = &service
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the needed service for the container port
|
// create the needed service for the container port
|
||||||
@@ -124,12 +123,12 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
}
|
}
|
||||||
// drop all "same-pod" deployments because the containers and volumes are already
|
// drop all "same-pod" deployments because the containers and volumes are already
|
||||||
// in the target deployment
|
// in the target deployment
|
||||||
for _, service := range project.Services {
|
for _, service := range podToMerge {
|
||||||
if samepod, ok := service.Labels[LABEL_SAME_POD]; ok && samepod != "" {
|
if samepod, ok := service.Labels[LABEL_SAME_POD]; ok && samepod != "" {
|
||||||
// move this deployment volumes to the target deployment
|
// move this deployment volumes to the target deployment
|
||||||
if target, ok := deployments[samepod]; ok {
|
if target, ok := deployments[samepod]; ok {
|
||||||
target.AddContainer(service)
|
target.AddContainer(*service)
|
||||||
target.BindFrom(service, deployments[service.Name])
|
target.BindFrom(*service, deployments[service.Name])
|
||||||
delete(deployments, service.Name)
|
delete(deployments, service.Name)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, LABEL_SAME_POD)
|
log.Printf("service %[1]s is declared as %[2]s, but %[2]s is not defined", service.Name, LABEL_SAME_POD)
|
||||||
@@ -168,6 +167,13 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
|
|
||||||
// generate all services
|
// generate all services
|
||||||
for _, s := range services {
|
for _, s := range services {
|
||||||
|
// add the service ports to the target service if it's a "same-pod" service
|
||||||
|
if samePod, ok := podToMerge[s.service.Name]; ok {
|
||||||
|
// get the target service
|
||||||
|
target := services[samePod.Name]
|
||||||
|
// merge the services
|
||||||
|
s.Spec.Ports = append(s.Spec.Ports, target.Spec.Ports...)
|
||||||
|
}
|
||||||
y, _ := s.Yaml()
|
y, _ := s.Yaml()
|
||||||
chart.Templates[s.Filename()] = &ChartTemplate{
|
chart.Templates[s.Filename()] = &ChartTemplate{
|
||||||
Content: y,
|
Content: y,
|
||||||
@@ -175,6 +181,13 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// drop all "same-pod" services
|
||||||
|
for _, s := range podToMerge {
|
||||||
|
// get the target service
|
||||||
|
target := services[s.Name]
|
||||||
|
delete(chart.Templates, target.Filename())
|
||||||
|
}
|
||||||
|
|
||||||
// compute all needed resplacements in YAML templates
|
// compute all needed resplacements in YAML templates
|
||||||
for n, v := range chart.Templates {
|
for n, v := range chart.Templates {
|
||||||
v.Content = removeReplaceString(v.Content)
|
v.Content = removeReplaceString(v.Content)
|
||||||
|
Reference in New Issue
Block a user