From dd7bc6c982be2a321ce7d14b029341ba2925ab58 Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Wed, 13 Aug 2025 19:27:40 +1000 Subject: [PATCH] chore: trim spaces for x-caddy, diff Caddy configs when comparing service specs --- pkg/api/caddy.go | 13 ++++++++ pkg/client/compose/caddy.go | 4 ++- pkg/client/compose/caddy_test.go | 42 ++++++++++++++++++++++--- pkg/client/compose/service_test.go | 49 ++++++++++++++++++++++++++---- pkg/client/deploy/container.go | 4 +++ 5 files changed, 101 insertions(+), 11 deletions(-) diff --git a/pkg/api/caddy.go b/pkg/api/caddy.go index bf18c66b..0f06017c 100644 --- a/pkg/api/caddy.go +++ b/pkg/api/caddy.go @@ -1,8 +1,21 @@ package api +import "strings" + // CaddySpec is the Caddy reverse proxy configuration for a service. type CaddySpec struct { // Config contains the Caddy config (Caddyfile) content. It must not conflict with the Caddy configs // of other services. Config string } + +func (c *CaddySpec) Equals(other *CaddySpec) bool { + if c == nil { + return other == nil || strings.TrimSpace(other.Config) == "" + } + if other == nil { + return strings.TrimSpace(c.Config) == "" + } + + return strings.TrimSpace(c.Config) == strings.TrimSpace(other.Config) +} diff --git a/pkg/client/compose/caddy.go b/pkg/client/compose/caddy.go index 28c7aa7a..fcbe49ba 100644 --- a/pkg/client/compose/caddy.go +++ b/pkg/client/compose/caddy.go @@ -84,9 +84,11 @@ func transformServicesCaddyExtension(project *types.Project) (*types.Project, er } caddy.Config = string(content) - service.Extensions[CaddyExtensionKey] = caddy } + caddy.Config = strings.TrimSpace(caddy.Config) + service.Extensions[CaddyExtensionKey] = caddy + return service, nil }) } diff --git a/pkg/client/compose/caddy_test.go b/pkg/client/compose/caddy_test.go index a4652669..196bde44 100644 --- a/pkg/client/compose/caddy_test.go +++ b/pkg/client/compose/caddy_test.go @@ -27,8 +27,25 @@ services: `, wantConfig: `example.com { reverse_proxy web:80 -} +}`, + }, + { + name: "x-caddy as string with extra spaces", + composeYAML: ` +services: + web: + image: nginx + x-caddy: |+ + + example.com { + reverse_proxy web:80 + } + + `, + wantConfig: `example.com { + reverse_proxy web:80 +}`, }, { name: "x-caddy as object with config field", @@ -44,8 +61,26 @@ services: `, wantConfig: `example.com { reverse_proxy web:80 -} +}`, + }, + { + name: "x-caddy as object with config field and extra spaces", + composeYAML: ` +services: + web: + image: nginx + x-caddy: + config: |+ + + example.com { + reverse_proxy web:80 + } + + `, + wantConfig: `example.com { + reverse_proxy web:80 +}`, }, { name: "x-caddy with path to Caddyfile", @@ -57,8 +92,7 @@ services: `, wantConfig: `test.example.com { reverse_proxy test:8000 -} -`, +}`, }, { name: "x-caddy with empty object", diff --git a/pkg/client/compose/service_test.go b/pkg/client/compose/service_test.go index 946c9258..b1267621 100644 --- a/pkg/client/compose/service_test.go +++ b/pkg/client/compose/service_test.go @@ -264,8 +264,7 @@ func TestServiceSpecFromCompose(t *testing.T) { Caddy: &api.CaddySpec{ Config: `test-caddy-config.example.com { reverse_proxy {{ upstreams 80 }} -} -`, +}`, }, }, }, @@ -315,8 +314,27 @@ services: want: &api.CaddySpec{ Config: `example.com { reverse_proxy web:80 -} +}`, + }, + }, + { + name: "x-caddy as string with extra spaces", + composeYAML: ` +services: + web: + image: nginx + x-caddy: |+ + + example.com { + reverse_proxy web:80 + } + + `, + want: &api.CaddySpec{ + Config: `example.com { + reverse_proxy web:80 +}`, }, }, { @@ -334,8 +352,28 @@ services: want: &api.CaddySpec{ Config: `example.com { reverse_proxy web:80 -} +}`, + }, + }, + { + name: "x-caddy as object with config field and extra spaces", + composeYAML: ` +services: + web: + image: nginx + x-caddy: + config: |+ + + example.com { + reverse_proxy web:80 + } + + `, + want: &api.CaddySpec{ + Config: `example.com { + reverse_proxy web:80 +}`, }, }, { @@ -349,8 +387,7 @@ services: want: &api.CaddySpec{ Config: `test.example.com { reverse_proxy test:8000 -} -`, +}`, }, }, { diff --git a/pkg/client/deploy/container.go b/pkg/client/deploy/container.go index e4bde19b..e00075e3 100644 --- a/pkg/client/deploy/container.go +++ b/pkg/client/deploy/container.go @@ -72,6 +72,10 @@ func EvalContainerSpecChange(current api.ServiceSpec, new api.ServiceSpec) Conta } // Check if any mutable properties changed. + if !current.Caddy.Equals(new.Caddy) { + return ContainerNeedsRecreate + } + if !reflect.DeepEqual(current.Container.Resources, newResources) { return ContainerNeedsUpdate }