mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 12:03:33 +00:00
chore: relax ports+Caddy spec validation to allow host mode ports
This commit is contained in:
+17
-6
@@ -131,12 +131,6 @@ func (s *ServiceSpec) Validate() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that Caddy and Ports are not used together.
|
|
||||||
if s.Caddy != nil && strings.TrimSpace(s.Caddy.Config) != "" && len(s.Ports) > 0 {
|
|
||||||
return fmt.Errorf("ports and Caddy configuration cannot be specified simultaneously: " +
|
|
||||||
"Caddy config is auto-generated from ports, use only one of them")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range s.Ports {
|
for _, p := range s.Ports {
|
||||||
if (p.Mode == "" || p.Mode == PortModeIngress) &&
|
if (p.Mode == "" || p.Mode == PortModeIngress) &&
|
||||||
p.Protocol != ProtocolHTTP && p.Protocol != ProtocolHTTPS {
|
p.Protocol != ProtocolHTTP && p.Protocol != ProtocolHTTPS {
|
||||||
@@ -146,6 +140,23 @@ func (s *ServiceSpec) Validate() error {
|
|||||||
|
|
||||||
// TODO: validate there is no conflict between ports.
|
// TODO: validate there is no conflict between ports.
|
||||||
|
|
||||||
|
// Validate that Caddy and Ports are not used together, unless all ports are host mode.
|
||||||
|
if s.Caddy != nil && strings.TrimSpace(s.Caddy.Config) != "" && len(s.Ports) > 0 {
|
||||||
|
// Check if all ports are in host mode.
|
||||||
|
hasIngressPort := false
|
||||||
|
for _, p := range s.Ports {
|
||||||
|
if p.Mode == "" || p.Mode == PortModeIngress {
|
||||||
|
hasIngressPort = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hasIngressPort {
|
||||||
|
return fmt.Errorf("ingress ports and Caddy configuration cannot be specified simultaneously: " +
|
||||||
|
"Caddy config is auto-generated from ingress ports, use only one of them. " +
|
||||||
|
"Host mode ports can be used with Caddy config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
volumeNames := make(map[string]struct{})
|
volumeNames := make(map[string]struct{})
|
||||||
for _, v := range s.Volumes {
|
for _, v := range s.Volumes {
|
||||||
if err := v.Validate(); err != nil {
|
if err := v.Validate(); err != nil {
|
||||||
|
|||||||
+97
-2
@@ -71,7 +71,7 @@ func TestServiceSpec_Validate_CaddyAndPorts(t *testing.T) {
|
|||||||
wantErr: "",
|
wantErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid with both Caddy and Ports",
|
name: "invalid with Caddy and Ports (default mode is ingress)",
|
||||||
spec: ServiceSpec{
|
spec: ServiceSpec{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Container: ContainerSpec{
|
Container: ContainerSpec{
|
||||||
@@ -84,10 +84,105 @@ func TestServiceSpec_Validate_CaddyAndPorts(t *testing.T) {
|
|||||||
{
|
{
|
||||||
ContainerPort: 80,
|
ContainerPort: 80,
|
||||||
Protocol: ProtocolHTTP,
|
Protocol: ProtocolHTTP,
|
||||||
|
// Mode is empty, defaults to ingress
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantErr: "ports and Caddy configuration cannot be specified simultaneously",
|
wantErr: "ingress ports and Caddy configuration cannot be specified simultaneously",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid with both Caddy and ingress Ports",
|
||||||
|
spec: ServiceSpec{
|
||||||
|
Name: "test",
|
||||||
|
Container: ContainerSpec{
|
||||||
|
Image: "nginx:latest",
|
||||||
|
},
|
||||||
|
Caddy: &CaddySpec{
|
||||||
|
Config: "example.com {\n reverse_proxy :8080\n}",
|
||||||
|
},
|
||||||
|
Ports: []PortSpec{
|
||||||
|
{
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: ProtocolHTTP,
|
||||||
|
Mode: PortModeIngress,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: "ingress ports and Caddy configuration cannot be specified simultaneously",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid with Caddy and host mode Ports",
|
||||||
|
spec: ServiceSpec{
|
||||||
|
Name: "test",
|
||||||
|
Container: ContainerSpec{
|
||||||
|
Image: "nginx:latest",
|
||||||
|
},
|
||||||
|
Caddy: &CaddySpec{
|
||||||
|
Config: "example.com {\n reverse_proxy :8080\n}",
|
||||||
|
},
|
||||||
|
Ports: []PortSpec{
|
||||||
|
{
|
||||||
|
ContainerPort: 3306,
|
||||||
|
PublishedPort: 3306,
|
||||||
|
Protocol: ProtocolTCP,
|
||||||
|
Mode: PortModeHost,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid with Caddy and mixed mode Ports",
|
||||||
|
spec: ServiceSpec{
|
||||||
|
Name: "test",
|
||||||
|
Container: ContainerSpec{
|
||||||
|
Image: "nginx:latest",
|
||||||
|
},
|
||||||
|
Caddy: &CaddySpec{
|
||||||
|
Config: "example.com {\n reverse_proxy :8080\n}",
|
||||||
|
},
|
||||||
|
Ports: []PortSpec{
|
||||||
|
{
|
||||||
|
ContainerPort: 3306,
|
||||||
|
PublishedPort: 3306,
|
||||||
|
Protocol: ProtocolTCP,
|
||||||
|
Mode: PortModeHost,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: ProtocolHTTP,
|
||||||
|
Mode: PortModeIngress,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: "ingress ports and Caddy configuration cannot be specified simultaneously",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid with Caddy and multiple host mode Ports",
|
||||||
|
spec: ServiceSpec{
|
||||||
|
Name: "test",
|
||||||
|
Container: ContainerSpec{
|
||||||
|
Image: "nginx:latest",
|
||||||
|
},
|
||||||
|
Caddy: &CaddySpec{
|
||||||
|
Config: "example.com {\n reverse_proxy :8080\n}",
|
||||||
|
},
|
||||||
|
Ports: []PortSpec{
|
||||||
|
{
|
||||||
|
ContainerPort: 3306,
|
||||||
|
PublishedPort: 3306,
|
||||||
|
Protocol: ProtocolTCP,
|
||||||
|
Mode: PortModeHost,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ContainerPort: 5432,
|
||||||
|
PublishedPort: 5432,
|
||||||
|
Protocol: ProtocolTCP,
|
||||||
|
Mode: PortModeHost,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: "",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user