Work in progress on compose-go v2

As explained in #102, the compose-go package changes types and needs
massive changes.
This branches is OK for now, but needs some tests.
It MUST be quickly fixed to be integrated in main branch.
This commit is contained in:
2024-12-28 19:37:51 +01:00
parent 41a4292939
commit 1fa07c29e5
22 changed files with 77 additions and 64 deletions

View File

@@ -12,7 +12,7 @@ import (
"reflect"
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/invopop/jsonschema"
"gopkg.in/yaml.v3"
)
@@ -72,27 +72,29 @@ func OverrideWithConfig(project *types.Project) {
}
for i, p := range project.Services {
name := p.Name
if project.Services[i].Labels == nil {
project.Services[i].Labels = make(map[string]string)
if p.Labels == nil {
p.Labels = make(map[string]string)
project.Services[i] = p
}
if s, ok := services[name]; ok {
getLabelContent(s.MainApp, &project.Services[i], labels.LabelMainApp)
getLabelContent(s.Values, &project.Services[i], labels.LabelValues)
getLabelContent(s.Secrets, &project.Services[i], labels.LabelSecrets)
getLabelContent(s.Ports, &project.Services[i], labels.LabelPorts)
getLabelContent(s.Ingress, &project.Services[i], labels.LabelIngress)
getLabelContent(s.HealthCheck, &project.Services[i], labels.LabelHealthCheck)
getLabelContent(s.SamePod, &project.Services[i], labels.LabelSamePod)
getLabelContent(s.Description, &project.Services[i], labels.LabelDescription)
getLabelContent(s.Ignore, &project.Services[i], labels.LabelIgnore)
getLabelContent(s.Dependencies, &project.Services[i], labels.LabelDependencies)
getLabelContent(s.ConfigMapFile, &project.Services[i], labels.LabelConfigMapFiles)
getLabelContent(s.MapEnv, &project.Services[i], labels.LabelMapEnv)
getLabelContent(s.CronJob, &project.Services[i], labels.LabelCronJob)
getLabelContent(s.EnvFrom, &project.Services[i], labels.LabelEnvFrom)
getLabelContent(s.ExchangeVolumes, &project.Services[i], labels.LabelExchangeVolume)
getLabelContent(s.ValuesFrom, &project.Services[i], labels.LabelValueFrom)
service := project.Services[i]
getLabelContent(s.MainApp, &service, labels.LabelMainApp)
getLabelContent(s.Values, &service, labels.LabelValues)
getLabelContent(s.Secrets, &service, labels.LabelSecrets)
getLabelContent(s.Ports, &service, labels.LabelPorts)
getLabelContent(s.Ingress, &service, labels.LabelIngress)
getLabelContent(s.HealthCheck, &service, labels.LabelHealthCheck)
getLabelContent(s.SamePod, &service, labels.LabelSamePod)
getLabelContent(s.Description, &service, labels.LabelDescription)
getLabelContent(s.Ignore, &service, labels.LabelIgnore)
getLabelContent(s.Dependencies, &service, labels.LabelDependencies)
getLabelContent(s.ConfigMapFile, &service, labels.LabelConfigMapFiles)
getLabelContent(s.MapEnv, &service, labels.LabelMapEnv)
getLabelContent(s.CronJob, &service, labels.LabelCronJob)
getLabelContent(s.EnvFrom, &service, labels.LabelEnvFrom)
getLabelContent(s.ExchangeVolumes, &service, labels.LabelExchangeVolume)
getLabelContent(s.ValuesFrom, &service, labels.LabelValueFrom)
}
}
fmt.Println(utils.IconInfo, "Katenary file loaded successfully, the services are now configured.")
@@ -123,6 +125,9 @@ func getLabelContent(o any, service *types.ServiceConfig, labelName string) erro
val = strings.TrimSpace(string(c))
}
if service.Labels == nil {
service.Labels = make(map[string]string)
}
service.Labels[labelName] = val
return nil
}

View File

@@ -1,13 +1,14 @@
package katenaryfile
import (
"context"
"katenary/generator/labels"
"log"
"os"
"path/filepath"
"testing"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/v2/cli"
)
func TestBuildSchema(t *testing.T) {
@@ -33,7 +34,7 @@ webapp:
// create /tmp/katenary-test-override directory, save the compose.yaml file
tmpDir, err := os.MkdirTemp("", "katenary-test-override")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}
composeFile := filepath.Join(tmpDir, "compose.yaml")
katenaryFile := filepath.Join(tmpDir, "katenary.yaml")
@@ -56,10 +57,13 @@ webapp:
cli.WithWorkingDirectory(tmpDir),
cli.WithDefaultConfigPath,
)
project, err := cli.ProjectFromOptions(options)
project, err := cli.ProjectFromOptions(context.TODO(), options)
if err != nil {
t.Fatal(err)
}
OverrideWithConfig(project)
w := project.Services[0].Labels
w := project.Services["webapp"].Labels
if v, ok := w[labels.LabelPorts]; !ok {
t.Fatal("Expected ports to be defined", v)
}
@@ -83,7 +87,7 @@ webapp:
// create /tmp/katenary-test-override directory, save the compose.yaml file
tmpDir, err := os.MkdirTemp("", "katenary-test-override")
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}
composeFile := filepath.Join(tmpDir, "compose.yaml")
katenaryFile := filepath.Join(tmpDir, "katenary.yaml")
@@ -106,10 +110,14 @@ webapp:
cli.WithWorkingDirectory(tmpDir),
cli.WithDefaultConfigPath,
)
project, err := cli.ProjectFromOptions(options)
project, err := cli.ProjectFromOptions(context.TODO(), options)
if err != nil {
t.Fatal(err)
}
OverrideWithConfig(project)
w := project.Services[0].Labels
log.Println(project.Services["webapp"].Labels)
w := project.Services["webapp"].Labels
if v, ok := w[labels.LabelPorts]; !ok {
t.Fatal("Expected ports to be defined", v)
}