From b437659678197ef611443389ed969b4431fb1a4b Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Thu, 21 Aug 2025 13:14:54 +1000 Subject: [PATCH] chore: update comments in generated Caddyfile --- internal/machine/caddyconfig/caddyfile.go | 31 ++++++---- .../machine/caddyconfig/caddyfile_test.go | 60 +++++++++++++------ 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/internal/machine/caddyconfig/caddyfile.go b/internal/machine/caddyconfig/caddyfile.go index 693f8160..c73aa9e8 100644 --- a/internal/machine/caddyconfig/caddyfile.go +++ b/internal/machine/caddyconfig/caddyfile.go @@ -18,10 +18,11 @@ import ( ) const ( - caddyfileHeader = `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + caddyfileHeader = `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. ` - caddyfileTemplate = `http:// { + caddyfileTemplate = `# Health check endpoint to verify Caddy reachability on this machine. +http:// { handle {{.VerifyPath}} { respond "{{.VerifyResponse}}" 200 } @@ -34,6 +35,9 @@ const ( # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking). fail_duration 30s } +{{- if or .HTTPHostUpstreams .HTTPSHostUpstreams }} + +# Sites generated from service ports.{{end}} {{- range $hostname, $upstreams := .HTTPHostUpstreams}} http://{{$hostname}} { @@ -86,7 +90,7 @@ func NewCaddyfileGenerator(machineID string, validator CaddyfileValidator, log * // // The final Caddyfile structure includes: // -// [caddy x-caddy] +// [caddy x-caddy (global config)] // [generated Caddyfile from all service ports] // [service-a x-caddy] // ... @@ -130,13 +134,14 @@ func (g *CaddyfileGenerator) Generate(ctx context.Context, records []store.Conta } renderedConfig, err := renderCaddyfile(tmplCtx, caddyCtr.ServiceSpec.CaddyConfig()) if err != nil { - g.log.Error("Failed to render template directives in custom global Caddy config, skipping it.", + g.log.Error("Failed to render template directives in user-defined global Caddy config, skipping it.", "service", caddyCtr.ServiceName(), "container", caddyCtr.ID, "err", err) } else { - caddyfileCandidate := renderedConfig + "\n\n" + caddyfile + caddyfileCandidate := fmt.Sprintf("# User-defined global config from service '%s'.\n%s\n\n%s", + caddyCtr.ServiceName(), renderedConfig, caddyfile) if err = g.validator.Validate(ctx, caddyfileCandidate); err != nil { - g.log.Error("Custom global Caddy config is invalid, skipping it.", + g.log.Error("User-defined global Caddy config is invalid, skipping it.", "service", caddyCtr.ServiceName(), "container", caddyCtr.ID, "err", err) } else { caddyfile = caddyfileCandidate @@ -179,20 +184,26 @@ func (g *CaddyfileGenerator) Generate(ctx context.Context, records []store.Conta } renderedConfig, err := renderCaddyfile(tmplCtx, ctr.ServiceSpec.CaddyConfig()) if err != nil { - g.log.Error("Failed to render template directives in custom Caddy config for service, skipping it.", + g.log.Error("Failed to render template directives in user-defined Caddy config for service, skipping it.", "service", serviceName, "err", err) continue } - caddyfileCandidate := fmt.Sprintf("%s\n# Service: %s\n%s\n", caddyfile, serviceName, renderedConfig) + caddyfileCandidate := fmt.Sprintf("%s\n# User-defined config for service '%s'.\n%s\n", + caddyfile, serviceName, renderedConfig) if err = g.validator.Validate(ctx, caddyfileCandidate); err != nil { - g.log.Error("Custom Caddy config for service is invalid, skipping it.", + g.log.Error("User-defined Caddy config for service is invalid, skipping it.", "service", serviceName, "err", err) } else { caddyfile = caddyfileCandidate } } + // TODO: add ac omment with invalid configs and their validation errors: + // # Skipped invalid configs: + // # - Service 'api': Template error in x-caddy + // # - Service 'web': Invalid Caddyfile syntax + return caddyfileHeader + "\n" + caddyfile, nil } diff --git a/internal/machine/caddyconfig/caddyfile_test.go b/internal/machine/caddyconfig/caddyfile_test.go index 59a092f4..f7cba036 100644 --- a/internal/machine/caddyconfig/caddyfile_test.go +++ b/internal/machine/caddyconfig/caddyfile_test.go @@ -19,9 +19,10 @@ import ( ) func TestCaddyfileGenerator(t *testing.T) { - caddyfileHeader := `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + caddyfileHeader := `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. +# Health check endpoint to verify Caddy reachability on this machine. http:// { handle /.uncloud-verify { respond "test-machine-id" 200 @@ -54,6 +55,8 @@ http:// { newContainerRecord(newContainer("10.210.0.2", "app.example.com:8080/http"), "mach1"), }, want: caddyfileHeader + ` +# Sites generated from service ports. + http://app.example.com { reverse_proxy 10.210.0.2:8080 { import common_proxy @@ -69,6 +72,8 @@ http://app.example.com { newContainerRecord(newContainer("10.210.0.3", "app.example.com:8080/http"), "mach1"), }, want: caddyfileHeader + ` +# Sites generated from service ports. + http://app.example.com { reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 { import common_proxy @@ -83,6 +88,8 @@ http://app.example.com { newContainerRecord(newContainer("10.210.0.2", "secure.example.com:8000/https"), "mach1"), }, want: caddyfileHeader + ` +# Sites generated from service ports. + https://secure.example.com { reverse_proxy 10.210.0.2:8000 { import common_proxy @@ -121,6 +128,8 @@ https://secure.example.com { ), }, want: caddyfileHeader + ` +# Sites generated from service ports. + http://app.example.com { reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 10.210.0.5:8080 { import common_proxy @@ -188,9 +197,10 @@ https://secure.example.com { } func TestCaddyfileGeneratorWithCustomConfigs(t *testing.T) { - caddyfileBase := `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + caddyfileBase := `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. +# Health check endpoint to verify Caddy reachability on this machine. http:// { handle /.uncloud-verify { respond "test-machine-id" 200 @@ -226,14 +236,16 @@ http:// { time.Now(), ), }, - want: `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + want: `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. +# User-defined global config from service 'caddy'. # Global Caddy configuration { global directive } +# Health check endpoint to verify Caddy reachability on this machine. http:// { handle /.uncloud-verify { respond "test-machine-id" 200 @@ -264,7 +276,7 @@ web.example.com { ), }, want: caddyfileBase + ` -# Service: web +# User-defined config for service 'web'. # Custom config for web service web.example.com { reverse_proxy web:3000 @@ -368,12 +380,12 @@ bad.example.com { ), }, want: caddyfileBase + ` -# Service: api +# User-defined config for service 'api'. api.example.com { reverse_proxy api:8080 } -# Service: web +# User-defined config for service 'web'. web.example.com { reverse_proxy web:3000 } @@ -408,14 +420,16 @@ web.example.com { time.Now(), ), }, - want: `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + want: `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. +# User-defined global config from service 'caddy'. # Global config { global directive } +# Health check endpoint to verify Caddy reachability on this machine. http:// { handle /.uncloud-verify { respond "test-machine-id" 200 @@ -430,6 +444,8 @@ http:// { fail_duration 30s } +# Sites generated from service ports. + http://app.example.com { reverse_proxy 10.210.0.2:8080 { import common_proxy @@ -437,7 +453,7 @@ http://app.example.com { log } -# Service: api +# User-defined config for service 'api'. api.example.com { reverse_proxy api:8000 } @@ -463,6 +479,8 @@ api.example.com { ), }, want: caddyfileBase + ` +# Sites generated from service ports. + http://api.example.com { reverse_proxy 10.210.0.3:8080 { import common_proxy @@ -470,7 +488,7 @@ http://api.example.com { log } -# Service: web +# User-defined config for service 'web'. web.example.com { reverse_proxy 10.210.0.2 } @@ -501,7 +519,7 @@ new.example.com { ), }, want: caddyfileBase + ` -# Service: web +# User-defined config for service 'web'. # New config new.example.com { respond "New" @@ -639,9 +657,10 @@ badconfig.com { time.Now(), ), }, - want: `# This file is autogenerated by Uncloud. Do not edit manually. -# Any manual changes will be overwritten on the next update. + want: `# This file is autogenerated by Uncloud based on the configuration of running services. +# Do not edit manually. Any manual changes will be overwritten on the next update. +# User-defined global config from service 'caddy'. # Global config from test machine { admin off @@ -651,6 +670,7 @@ localhost:8080 { respond "Admin panel" } +# Health check endpoint to verify Caddy reachability on this machine. http:// { handle /.uncloud-verify { respond "test-machine-id" 200 @@ -665,6 +685,8 @@ http:// { fail_duration 30s } +# Sites generated from service ports. + http://api.example.com { reverse_proxy 10.210.1.2:8080 10.210.2.2:8080 10.210.3.2:8080 { import common_proxy @@ -686,7 +708,7 @@ http://web.example.com { log } -# Service: db +# User-defined config for service 'db'. # DB admin panel dbadmin.example.com { basicauth { @@ -695,7 +717,7 @@ dbadmin.example.com { reverse_proxy 10.210.1.4:5432 } -# Service: gateway +# User-defined config for service 'gateway'. # Testing different upstream template functions gateway.example.com { # Current service upstreams (gateway) @@ -729,7 +751,7 @@ gateway.example.com { } } -# Service: web +# User-defined config for service 'web'. # Web service config v2 (most recent) web-v2.example.com { reverse_proxy 10.210.1.3:8080 10.210.3.3:8080 10.210.2.3:8080