Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed Jul 5, 2024
1 parent c67e5da commit 1442845
Show file tree
Hide file tree
Showing 22 changed files with 7 additions and 519 deletions.
148 changes: 0 additions & 148 deletions src/pkg/cli/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,154 +9,6 @@ import (
compose "github.com/compose-spec/compose-go/v2/types"
)

// Deprecated: call FixupServices instead
func ConvertServices(serviceConfigs compose.Services) []*defangv1.Service {
//
// Publish updates
//
var services []*defangv1.Service
for _, svccfg := range serviceConfigs {
var healthcheck *defangv1.HealthCheck
if svccfg.HealthCheck != nil && len(svccfg.HealthCheck.Test) > 0 && !svccfg.HealthCheck.Disable {
healthcheck = &defangv1.HealthCheck{
Test: svccfg.HealthCheck.Test,
}
if nil != svccfg.HealthCheck.Interval {
healthcheck.Interval = uint32(*svccfg.HealthCheck.Interval / 1e9)
}
if nil != svccfg.HealthCheck.Timeout {
healthcheck.Timeout = uint32(*svccfg.HealthCheck.Timeout / 1e9)
}
if nil != svccfg.HealthCheck.Retries {
healthcheck.Retries = uint32(*svccfg.HealthCheck.Retries)
}
}

var deploy *defangv1.Deploy
if svccfg.Deploy != nil {
deploy = &defangv1.Deploy{}
if svccfg.Deploy.Replicas != nil {
deploy.Replicas = uint32(*svccfg.Deploy.Replicas)
}

reservations := getResourceReservations(svccfg.Deploy.Resources)
if reservations != nil {
var devices []*defangv1.Device
for _, d := range reservations.Devices {
devices = append(devices, &defangv1.Device{
Capabilities: d.Capabilities,
Count: uint32(d.Count),
Driver: d.Driver,
})
}
deploy.Resources = &defangv1.Resources{
Reservations: &defangv1.Resource{
Cpus: float32(reservations.NanoCPUs),
Memory: float32(reservations.MemoryBytes) / MiB,
Devices: devices,
},
}
}
}

var build *defangv1.Build
if svccfg.Build != nil {
build = &defangv1.Build{
Context: svccfg.Build.Context,
Dockerfile: svccfg.Build.Dockerfile,
ShmSize: float32(svccfg.Build.ShmSize) / MiB,
Target: svccfg.Build.Target,
}

if len(svccfg.Build.Args) > 0 {
build.Args = make(map[string]string, len(svccfg.Build.Args))
for key, value := range svccfg.Build.Args {
build.Args[key] = *value
}
}
}

// Extract environment variables
var configs []*defangv1.Secret
envs := make(map[string]string, len(svccfg.Environment))
for key, value := range svccfg.Environment {
if value == nil {
// Add unset environment variables as "configs"
configs = append(configs, &defangv1.Secret{
Source: key,
})
continue
}
envs[key] = *value
}

// Extract secret references; secrets are supposed to be files, not env, but it's kept for backward compatibility
for _, secret := range svccfg.Secrets {
configs = append(configs, &defangv1.Secret{
Source: secret.Source,
})
}

init := false
if svccfg.Init != nil {
init = *svccfg.Init
}

var dnsRole string
if dnsRoleVal := svccfg.Extensions["x-defang-dns-role"]; dnsRoleVal != nil {
dnsRole = dnsRoleVal.(string) // already validated
}

var staticFiles *defangv1.StaticFiles
if staticFilesVal := svccfg.Extensions["x-defang-static-files"]; staticFilesVal != nil {
if str, ok := staticFilesVal.(string); ok {
staticFiles = &defangv1.StaticFiles{Folder: str}
} else {
obj := staticFilesVal.(map[string]interface{}) // already validated
var redirects []string
if r, ok := obj["redirects"].([]interface{}); ok {
redirects = make([]string, len(r))
for i, v := range r {
redirects[i] = v.(string)
}
}
staticFiles = &defangv1.StaticFiles{
Folder: obj["folder"].(string),
Redirects: redirects,
}
}
}

var redis *defangv1.Redis
if _, ok := svccfg.Extensions["x-defang-redis"]; ok {
redis = &defangv1.Redis{}
}

network := network(&svccfg)
ports := convertPorts(svccfg.Ports)
services = append(services, &defangv1.Service{
Name: svccfg.Name,
Image: svccfg.Image,
Build: build,
Internal: network == defangv1.Network_PRIVATE,
Networks: network,
Init: init,
Ports: ports,
Healthcheck: healthcheck,
Deploy: deploy,
Environment: envs,
Secrets: configs,
Command: svccfg.Command,
Domainname: svccfg.DomainName,
Platform: convertPlatform(svccfg.Platform),
DnsRole: dnsRole,
StaticFiles: staticFiles,
Redis: redis,
})
}
return services
}

func getResourceReservations(r compose.Resources) *compose.Resource {
if r.Reservations == nil {
// TODO: we might not want to default to all the limits, maybe only memory?
Expand Down
29 changes: 0 additions & 29 deletions src/pkg/cli/compose/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package compose
import (
"context"
"encoding/json"
"slices"
"strings"
"testing"

"github.com/DefangLabs/defang/src/pkg/cli/client"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -176,30 +174,3 @@ func TestComposeBlob(t *testing.T) {
t.Errorf("expected empty blob, got %v", blob)
}
}

func TestConvert(t *testing.T) {
testRunCompose(t, func(t *testing.T, path string) {
loader := Loader{path}
proj, err := loader.LoadCompose(context.Background())
if err != nil {
t.Fatal(err)
}
if err := FixupServices(context.Background(), client.MockClient{}, proj.Services, BuildContextIgnore); err != nil {
t.Fatal(err)
}

services := ConvertServices(proj.Services)
// The order of the services is not guaranteed, so we sort the services before comparing
slices.SortFunc(services, func(i, j *defangv1.Service) int { return strings.Compare(i.Name, j.Name) })

// Convert the protobuf services to pretty JSON for comparison (YAML would include all the zero values)
actual, err := json.MarshalIndent(services, "", " ")
if err != nil {
t.Fatal(err)
}

if err := compare(actual, path+".convert"); err != nil {
t.Error(err)
}
})
}
21 changes: 7 additions & 14 deletions src/pkg/cli/composeStart.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,12 @@ func ComposeStart(ctx context.Context, c client.Client, force bool) (*defangv1.D
return nil, err
}

services := compose.ConvertServices(project.Services)
if len(services) == 0 {
return nil, &ComposeError{fmt.Errorf("no services found")}
}

if DoDryRun {
for _, service := range services {
PrintObject(service.Name, service)
}
yaml, _ := project.MarshalYAML()
fmt.Println(string(yaml))
return nil, ErrDryRun
}

for _, service := range services {
term.Info("Deploying service", service.Name)
}

bytes, err := project.MarshalYAML()
if err != nil {
return nil, err
Expand All @@ -76,9 +66,12 @@ func ComposeStart(ctx context.Context, c client.Client, force bool) (*defangv1.D
return nil, err
}

for _, service := range project.Services {
term.Info("Deploying service", service.Name)
}

resp, err := c.Deploy(ctx, &defangv1.DeployRequest{
Services: services,
Compose: str,
Compose: str,
})
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion src/tests/Fancy-Proj_Dir/compose.yaml.convert

This file was deleted.

20 changes: 0 additions & 20 deletions src/tests/alttestproj/compose.yaml.convert

This file was deleted.

15 changes: 0 additions & 15 deletions src/tests/compose-go-warn/compose.yaml.convert

This file was deleted.

14 changes: 0 additions & 14 deletions src/tests/configoverride/compose.yaml.convert

This file was deleted.

1 change: 0 additions & 1 deletion src/tests/empty/compose.yaml.convert

This file was deleted.

23 changes: 0 additions & 23 deletions src/tests/emptyenv/compose.yaml.convert

This file was deleted.

29 changes: 0 additions & 29 deletions src/tests/fixupenv/compose.yaml.convert

This file was deleted.

9 changes: 0 additions & 9 deletions src/tests/longname/compose.yaml.convert

This file was deleted.

28 changes: 0 additions & 28 deletions src/tests/networks/compose.yaml.convert

This file was deleted.

1 change: 0 additions & 1 deletion src/tests/noprojname/compose.yaml.convert

This file was deleted.

Loading

0 comments on commit 1442845

Please sign in to comment.