mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat: add support for standard compose ports directive (#95)
This commit is contained in:
@@ -4,26 +4,59 @@ import (
|
||||
"fmt"
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const PortsExtensionKey = "x-ports"
|
||||
|
||||
type PortsSource []string
|
||||
|
||||
// TransformServicesPortsExtension transforms the ports extension of all services in the project by replacing a string
|
||||
// representation of each port with a parsed PortSpec.
|
||||
// transformServicesPortsExtension transforms both standard 'ports' and 'x-ports' to PortSpecs.
|
||||
func transformServicesPortsExtension(project *types.Project) (*types.Project, error) {
|
||||
return project.WithServicesTransform(func(name string, service types.ServiceConfig) (types.ServiceConfig, error) {
|
||||
ports, ok := service.Extensions[PortsExtensionKey].(PortsSource)
|
||||
if !ok {
|
||||
// Check for mutual exclusivity
|
||||
hasStandardPorts := len(service.Ports) > 0
|
||||
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)
|
||||
}
|
||||
|
||||
var (
|
||||
specs []api.PortSpec
|
||||
err error
|
||||
)
|
||||
|
||||
if hasStandardPorts {
|
||||
// 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)
|
||||
}
|
||||
} else if hasXPorts {
|
||||
// Use existing x-ports string-based processing for backward compatibility
|
||||
var portsSource PortsSource
|
||||
var ok bool
|
||||
portsSource, ok = service.Extensions[PortsExtensionKey].(PortsSource)
|
||||
if !ok {
|
||||
return service, nil
|
||||
}
|
||||
|
||||
// Parse the port strings using existing logic
|
||||
specs, err = transformPortsExtension(portsSource)
|
||||
if err != nil {
|
||||
return service, err
|
||||
}
|
||||
} else {
|
||||
// No ports specified
|
||||
return service, nil
|
||||
}
|
||||
|
||||
specs, err := transformPortsExtension(ports)
|
||||
if err != nil {
|
||||
return service, err
|
||||
// Ensure extensions map exists before setting the port specs
|
||||
if service.Extensions == nil {
|
||||
service.Extensions = make(types.Extensions)
|
||||
}
|
||||
|
||||
service.Extensions[PortsExtensionKey] = specs
|
||||
return service, nil
|
||||
})
|
||||
@@ -41,3 +74,54 @@ func transformPortsExtension(ports PortsSource) ([]api.PortSpec, error) {
|
||||
|
||||
return specs, nil
|
||||
}
|
||||
|
||||
// convertServicePortConfigToPortSpec converts types.ServicePortConfig directly to api.PortSpec
|
||||
func convertServicePortConfigToPortSpec(port types.ServicePortConfig) (api.PortSpec, error) {
|
||||
spec := api.PortSpec{
|
||||
ContainerPort: uint16(port.Target),
|
||||
Protocol: port.Protocol,
|
||||
Mode: port.Mode,
|
||||
}
|
||||
// Set published port if specified
|
||||
if 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)
|
||||
}
|
||||
spec.PublishedPort = uint16(publishedPort)
|
||||
}
|
||||
|
||||
// Set host IP if specified
|
||||
if port.HostIP != "" {
|
||||
hostIP, err := netip.ParseAddr(port.HostIP)
|
||||
if err != nil {
|
||||
return spec, fmt.Errorf("invalid host IP %q: %w", port.HostIP, err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
// convertStandardPortsToPortSpecs converts []types.ServicePortConfig directly to api.PortSpecs.
|
||||
func convertStandardPortsToPortSpecs(ports []types.ServicePortConfig) ([]api.PortSpec, error) {
|
||||
var specs = make([]api.PortSpec, 0, len(ports))
|
||||
|
||||
for _, port := range ports {
|
||||
spec, err := convertServicePortConfigToPortSpec(port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
specs = append(specs, spec)
|
||||
}
|
||||
|
||||
return specs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user