From c01365b416d380c59f6aa70dba986a782945dc3e Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Thu, 21 Aug 2025 08:54:06 +1000 Subject: [PATCH] chore: add header to generated Caddyfile that it's autogenerated --- internal/machine/caddyconfig/caddyfile.go | 9 ++- .../machine/caddyconfig/caddyfile_test.go | 70 ++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/internal/machine/caddyconfig/caddyfile.go b/internal/machine/caddyconfig/caddyfile.go index 3d0864c3..693f8160 100644 --- a/internal/machine/caddyconfig/caddyfile.go +++ b/internal/machine/caddyconfig/caddyfile.go @@ -17,7 +17,11 @@ import ( "github.com/psviderski/uncloud/pkg/api" ) -const caddyfileTemplate = `http:// { +const ( + caddyfileHeader = `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. +` + caddyfileTemplate = `http:// { handle {{.VerifyPath}} { respond "{{.VerifyResponse}}" 200 } @@ -47,6 +51,7 @@ https://{{$hostname}} { log }{{end}} ` +) // CaddyfileGenerator generates a Caddyfile configuration for the Caddy reverse proxy. type CaddyfileGenerator struct { @@ -188,7 +193,7 @@ func (g *CaddyfileGenerator) Generate(ctx context.Context, records []store.Conta } } - return caddyfile, nil + return caddyfileHeader + "\n" + caddyfile, nil } func (g *CaddyfileGenerator) generateBaseFromPorts(containers []api.ServiceContainer) (string, error) { diff --git a/internal/machine/caddyconfig/caddyfile_test.go b/internal/machine/caddyconfig/caddyfile_test.go index 2e780854..59a092f4 100644 --- a/internal/machine/caddyconfig/caddyfile_test.go +++ b/internal/machine/caddyconfig/caddyfile_test.go @@ -19,7 +19,10 @@ import ( ) func TestCaddyfileGenerator(t *testing.T) { - caddyfileHeader := `http:// { + caddyfileHeader := `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. + +http:// { handle /.uncloud-verify { respond "test-machine-id" 200 } @@ -185,7 +188,10 @@ https://secure.example.com { } func TestCaddyfileGeneratorWithCustomConfigs(t *testing.T) { - caddyfileBase := `http:// { + caddyfileBase := `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. + +http:// { handle /.uncloud-verify { respond "test-machine-id" 200 } @@ -220,12 +226,28 @@ func TestCaddyfileGeneratorWithCustomConfigs(t *testing.T) { time.Now(), ), }, - want: `# Global Caddy configuration + want: `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. + +# Global Caddy configuration { global directive } -` + caddyfileBase, +http:// { + handle /.uncloud-verify { + respond "test-machine-id" 200 + } + log +} + +(common_proxy) { + # Retry failed requests up to lb_retries times against other available upstreams. + lb_retries 3 + # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking). + fail_duration 30s +} +`, }, { name: "regular service with valid custom config", @@ -386,12 +408,28 @@ web.example.com { time.Now(), ), }, - want: `# Global config + want: `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. + +# Global config { global directive } -` + caddyfileBase + ` +http:// { + handle /.uncloud-verify { + respond "test-machine-id" 200 + } + log +} + +(common_proxy) { + # Retry failed requests up to lb_retries times against other available upstreams. + lb_retries 3 + # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking). + fail_duration 30s +} + http://app.example.com { reverse_proxy 10.210.0.2:8080 { import common_proxy @@ -601,7 +639,10 @@ badconfig.com { time.Now(), ), }, - want: `# Global config from test machine + want: `# This file is autogenerated by Uncloud. Do not edit manually. +# Any manual changes will be overwritten on the next update. + +# Global config from test machine { admin off } @@ -610,7 +651,20 @@ localhost:8080 { respond "Admin panel" } -` + caddyfileBase + ` +http:// { + handle /.uncloud-verify { + respond "test-machine-id" 200 + } + log +} + +(common_proxy) { + # Retry failed requests up to lb_retries times against other available upstreams. + lb_retries 3 + # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking). + fail_duration 30s +} + http://api.example.com { reverse_proxy 10.210.1.2:8080 10.210.2.2:8080 10.210.3.2:8080 { import common_proxy