Skip to content

Commit

Permalink
Work in progress on compose-go v2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
metal3d committed Dec 28, 2024
1 parent 41a4292 commit 1fa07c2
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cmd/katenary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"os"
"strings"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/v2/cli"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion generator/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion generator/configMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"unicode/utf8"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion generator/configMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"testing"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)
Expand Down
2 changes: 1 addition & 1 deletion generator/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"strings"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
)

const ingressClassHelp = `# Default value for ingress.class annotation
Expand Down
2 changes: 1 addition & 1 deletion generator/cronJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion generator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"regexp"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion generator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
43 changes: 24 additions & 19 deletions generator/katenaryfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 15 additions & 7 deletions generator/katenaryfile/main_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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")
Expand All @@ -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)
}
Expand All @@ -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")
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion generator/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package generator
import (
"katenary/utils"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion generator/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"katenary/utils"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion generator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down
2 changes: 1 addition & 1 deletion generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)
Expand Down
19 changes: 12 additions & 7 deletions generator/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package generator

import (
"context"
"fmt"
"katenary/generator/labels"
"os"
"path/filepath"
"testing"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/v2/cli"
)

func TestSplitPorts(t *testing.T) {
Expand Down Expand Up @@ -37,15 +38,18 @@ services:
cli.WithWorkingDirectory(tmpDir),
cli.WithDefaultConfigPath,
)
project, err := cli.ProjectFromOptions(options)
project, err := cli.ProjectFromOptions(context.TODO(), options)
if err != nil {
t.Fatal(err)
}
if err := fixPorts(&project.Services[0]); err != nil {
service := project.Services["foo"]
if err := fixPorts(&service); err != nil {
t.Errorf("Expected no error, got %s", err)
}
project.Services["foo"] = service
found := 0
for _, p := range project.Services[0].Ports {
t.Log(service.Ports, project.Services["foo"].Ports)
for _, p := range project.Services["foo"].Ports {
switch p.Target {
case 80, 443:
found++
Expand Down Expand Up @@ -85,15 +89,16 @@ services:
cli.WithWorkingDirectory(tmpDir),
cli.WithDefaultConfigPath,
)
project, err := cli.ProjectFromOptions(options)
project, err := cli.ProjectFromOptions(context.TODO(), options)
if err != nil {
t.Fatal(err)
}
if err := fixPorts(&project.Services[0]); err != nil {
service := project.Services["foo"]
if err := fixPorts(&service); err != nil {
t.Errorf("Expected no error, got %s", err)
}
found := 0
for _, p := range project.Services[0].Ports {
for _, p := range service.Ports {
switch p.Target {
case 80, 443, 8080:
found++
Expand Down
2 changes: 1 addition & 1 deletion generator/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package generator
import (
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
)

// RepositoryValue is a docker repository image and tag that will be saved in values.yaml.
Expand Down
2 changes: 1 addition & 1 deletion generator/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"katenary/utils"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
toolchain go1.23.4

require (
github.com/compose-spec/compose-go v1.20.2
github.com/compose-spec/compose-go/v2 v2.4.6
github.com/invopop/jsonschema v0.12.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/spf13/cobra v1.8.1
Expand All @@ -24,19 +24,17 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
Expand Down
Loading

0 comments on commit 1fa07c2

Please sign in to comment.