mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
feat: follow-up compose 'ports' support: use ingress mode by default (closes #81)
This commit is contained in:
@@ -2,10 +2,12 @@ package compose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
)
|
||||
|
||||
const PortsExtensionKey = "x-ports"
|
||||
@@ -20,7 +22,8 @@ func transformServicesPortsExtension(project *types.Project) (*types.Project, er
|
||||
hasXPorts := service.Extensions[PortsExtensionKey] != nil
|
||||
|
||||
if hasStandardPorts && hasXPorts {
|
||||
return service, fmt.Errorf("service %q cannot specify both 'ports' and 'x-ports' directives, use only one", name)
|
||||
return service, fmt.Errorf("service %q cannot specify both 'ports' and 'x-ports' directives, use only one",
|
||||
name)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -32,7 +35,7 @@ func transformServicesPortsExtension(project *types.Project) (*types.Project, er
|
||||
// Convert standard ports directly to api.PortSpec
|
||||
specs, err = convertStandardPortsToPortSpecs(service.Ports)
|
||||
if err != nil {
|
||||
return service, fmt.Errorf("convert standard ports for service %q: %w", name, err)
|
||||
return service, fmt.Errorf("convert standard 'ports' for service '%s': %w", name, err)
|
||||
}
|
||||
} else if hasXPorts {
|
||||
// Use existing x-ports string-based processing for backward compatibility
|
||||
@@ -82,8 +85,23 @@ func convertServicePortConfigToPortSpec(port types.ServicePortConfig) (api.PortS
|
||||
Protocol: port.Protocol,
|
||||
Mode: port.Mode,
|
||||
}
|
||||
// Compose parser sets the default protocol to "tcp" and mode to "ingress". We still explicitly set these values
|
||||
// to avoid relying on implicit behavior and improve code robustness.
|
||||
if spec.Protocol == "" {
|
||||
spec.Protocol = api.ProtocolTCP
|
||||
}
|
||||
if spec.Mode == "" {
|
||||
spec.Mode = api.PortModeIngress
|
||||
}
|
||||
|
||||
// Set published port if specified
|
||||
if port.Published != "" {
|
||||
if strings.Contains(port.Published, "-") {
|
||||
// 'a-b:x' format is not automatically expanded by the compose parser and our PortSpec does not support port
|
||||
// ranges for now.
|
||||
return spec, fmt.Errorf("port range '%s' for published port is not supported, use a single port",
|
||||
port.Published)
|
||||
}
|
||||
publishedPort, err := strconv.ParseUint(port.Published, 10, 16)
|
||||
if err != nil {
|
||||
return spec, fmt.Errorf("invalid published port %q: %w", port.Published, err)
|
||||
@@ -100,9 +118,6 @@ func convertServicePortConfigToPortSpec(port types.ServicePortConfig) (api.PortS
|
||||
spec.HostIP = hostIP
|
||||
}
|
||||
|
||||
// Apply defaults according to uncloud
|
||||
spec.AdjustUncloudMode()
|
||||
|
||||
// Validate the resulting spec
|
||||
if err := spec.Validate(); err != nil {
|
||||
return spec, fmt.Errorf("invalid port configuration: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user