chore: update comments in generated Caddyfile

This commit is contained in:
Pasha Sviderski
2025-08-21 13:14:54 +10:00
parent c01365b416
commit b437659678
2 changed files with 62 additions and 29 deletions
+21 -10
View File
@@ -18,10 +18,11 @@ import (
) )
const ( const (
caddyfileHeader = `# This file is autogenerated by Uncloud. Do not edit manually. caddyfileHeader = `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # 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}} { handle {{.VerifyPath}} {
respond "{{.VerifyResponse}}" 200 respond "{{.VerifyResponse}}" 200
} }
@@ -34,6 +35,9 @@ const (
# Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking). # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking).
fail_duration 30s fail_duration 30s
} }
{{- if or .HTTPHostUpstreams .HTTPSHostUpstreams }}
# Sites generated from service ports.{{end}}
{{- range $hostname, $upstreams := .HTTPHostUpstreams}} {{- range $hostname, $upstreams := .HTTPHostUpstreams}}
http://{{$hostname}} { http://{{$hostname}} {
@@ -86,7 +90,7 @@ func NewCaddyfileGenerator(machineID string, validator CaddyfileValidator, log *
// //
// The final Caddyfile structure includes: // The final Caddyfile structure includes:
// //
// [caddy x-caddy] // [caddy x-caddy (global config)]
// [generated Caddyfile from all service ports] // [generated Caddyfile from all service ports]
// [service-a x-caddy] // [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()) renderedConfig, err := renderCaddyfile(tmplCtx, caddyCtr.ServiceSpec.CaddyConfig())
if err != nil { 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) "service", caddyCtr.ServiceName(), "container", caddyCtr.ID, "err", err)
} else { } 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 { 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) "service", caddyCtr.ServiceName(), "container", caddyCtr.ID, "err", err)
} else { } else {
caddyfile = caddyfileCandidate caddyfile = caddyfileCandidate
@@ -179,20 +184,26 @@ func (g *CaddyfileGenerator) Generate(ctx context.Context, records []store.Conta
} }
renderedConfig, err := renderCaddyfile(tmplCtx, ctr.ServiceSpec.CaddyConfig()) renderedConfig, err := renderCaddyfile(tmplCtx, ctr.ServiceSpec.CaddyConfig())
if err != nil { 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) "service", serviceName, "err", err)
continue 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 { 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) "service", serviceName, "err", err)
} else { } else {
caddyfile = caddyfileCandidate 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 return caddyfileHeader + "\n" + caddyfile, nil
} }
+41 -19
View File
@@ -19,9 +19,10 @@ import (
) )
func TestCaddyfileGenerator(t *testing.T) { func TestCaddyfileGenerator(t *testing.T) {
caddyfileHeader := `# This file is autogenerated by Uncloud. Do not edit manually. caddyfileHeader := `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # 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:// { http:// {
handle /.uncloud-verify { handle /.uncloud-verify {
respond "test-machine-id" 200 respond "test-machine-id" 200
@@ -54,6 +55,8 @@ http:// {
newContainerRecord(newContainer("10.210.0.2", "app.example.com:8080/http"), "mach1"), newContainerRecord(newContainer("10.210.0.2", "app.example.com:8080/http"), "mach1"),
}, },
want: caddyfileHeader + ` want: caddyfileHeader + `
# Sites generated from service ports.
http://app.example.com { http://app.example.com {
reverse_proxy 10.210.0.2:8080 { reverse_proxy 10.210.0.2:8080 {
import common_proxy import common_proxy
@@ -69,6 +72,8 @@ http://app.example.com {
newContainerRecord(newContainer("10.210.0.3", "app.example.com:8080/http"), "mach1"), newContainerRecord(newContainer("10.210.0.3", "app.example.com:8080/http"), "mach1"),
}, },
want: caddyfileHeader + ` want: caddyfileHeader + `
# Sites generated from service ports.
http://app.example.com { http://app.example.com {
reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 { reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 {
import common_proxy import common_proxy
@@ -83,6 +88,8 @@ http://app.example.com {
newContainerRecord(newContainer("10.210.0.2", "secure.example.com:8000/https"), "mach1"), newContainerRecord(newContainer("10.210.0.2", "secure.example.com:8000/https"), "mach1"),
}, },
want: caddyfileHeader + ` want: caddyfileHeader + `
# Sites generated from service ports.
https://secure.example.com { https://secure.example.com {
reverse_proxy 10.210.0.2:8000 { reverse_proxy 10.210.0.2:8000 {
import common_proxy import common_proxy
@@ -121,6 +128,8 @@ https://secure.example.com {
), ),
}, },
want: caddyfileHeader + ` want: caddyfileHeader + `
# Sites generated from service ports.
http://app.example.com { http://app.example.com {
reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 10.210.0.5:8080 { reverse_proxy 10.210.0.2:8080 10.210.0.3:8080 10.210.0.5:8080 {
import common_proxy import common_proxy
@@ -188,9 +197,10 @@ https://secure.example.com {
} }
func TestCaddyfileGeneratorWithCustomConfigs(t *testing.T) { func TestCaddyfileGeneratorWithCustomConfigs(t *testing.T) {
caddyfileBase := `# This file is autogenerated by Uncloud. Do not edit manually. caddyfileBase := `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # 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:// { http:// {
handle /.uncloud-verify { handle /.uncloud-verify {
respond "test-machine-id" 200 respond "test-machine-id" 200
@@ -226,14 +236,16 @@ http:// {
time.Now(), time.Now(),
), ),
}, },
want: `# This file is autogenerated by Uncloud. Do not edit manually. want: `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # 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 Caddy configuration
{ {
global directive global directive
} }
# Health check endpoint to verify Caddy reachability on this machine.
http:// { http:// {
handle /.uncloud-verify { handle /.uncloud-verify {
respond "test-machine-id" 200 respond "test-machine-id" 200
@@ -264,7 +276,7 @@ web.example.com {
), ),
}, },
want: caddyfileBase + ` want: caddyfileBase + `
# Service: web # User-defined config for service 'web'.
# Custom config for web service # Custom config for web service
web.example.com { web.example.com {
reverse_proxy web:3000 reverse_proxy web:3000
@@ -368,12 +380,12 @@ bad.example.com {
), ),
}, },
want: caddyfileBase + ` want: caddyfileBase + `
# Service: api # User-defined config for service 'api'.
api.example.com { api.example.com {
reverse_proxy api:8080 reverse_proxy api:8080
} }
# Service: web # User-defined config for service 'web'.
web.example.com { web.example.com {
reverse_proxy web:3000 reverse_proxy web:3000
} }
@@ -408,14 +420,16 @@ web.example.com {
time.Now(), time.Now(),
), ),
}, },
want: `# This file is autogenerated by Uncloud. Do not edit manually. want: `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # Do not edit manually. Any manual changes will be overwritten on the next update.
# User-defined global config from service 'caddy'.
# Global config # Global config
{ {
global directive global directive
} }
# Health check endpoint to verify Caddy reachability on this machine.
http:// { http:// {
handle /.uncloud-verify { handle /.uncloud-verify {
respond "test-machine-id" 200 respond "test-machine-id" 200
@@ -430,6 +444,8 @@ http:// {
fail_duration 30s fail_duration 30s
} }
# Sites generated from service ports.
http://app.example.com { http://app.example.com {
reverse_proxy 10.210.0.2:8080 { reverse_proxy 10.210.0.2:8080 {
import common_proxy import common_proxy
@@ -437,7 +453,7 @@ http://app.example.com {
log log
} }
# Service: api # User-defined config for service 'api'.
api.example.com { api.example.com {
reverse_proxy api:8000 reverse_proxy api:8000
} }
@@ -463,6 +479,8 @@ api.example.com {
), ),
}, },
want: caddyfileBase + ` want: caddyfileBase + `
# Sites generated from service ports.
http://api.example.com { http://api.example.com {
reverse_proxy 10.210.0.3:8080 { reverse_proxy 10.210.0.3:8080 {
import common_proxy import common_proxy
@@ -470,7 +488,7 @@ http://api.example.com {
log log
} }
# Service: web # User-defined config for service 'web'.
web.example.com { web.example.com {
reverse_proxy 10.210.0.2 reverse_proxy 10.210.0.2
} }
@@ -501,7 +519,7 @@ new.example.com {
), ),
}, },
want: caddyfileBase + ` want: caddyfileBase + `
# Service: web # User-defined config for service 'web'.
# New config # New config
new.example.com { new.example.com {
respond "New" respond "New"
@@ -639,9 +657,10 @@ badconfig.com {
time.Now(), time.Now(),
), ),
}, },
want: `# This file is autogenerated by Uncloud. Do not edit manually. want: `# This file is autogenerated by Uncloud based on the configuration of running services.
# Any manual changes will be overwritten on the next update. # 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 # Global config from test machine
{ {
admin off admin off
@@ -651,6 +670,7 @@ localhost:8080 {
respond "Admin panel" respond "Admin panel"
} }
# Health check endpoint to verify Caddy reachability on this machine.
http:// { http:// {
handle /.uncloud-verify { handle /.uncloud-verify {
respond "test-machine-id" 200 respond "test-machine-id" 200
@@ -665,6 +685,8 @@ http:// {
fail_duration 30s fail_duration 30s
} }
# Sites generated from service ports.
http://api.example.com { http://api.example.com {
reverse_proxy 10.210.1.2:8080 10.210.2.2:8080 10.210.3.2:8080 { reverse_proxy 10.210.1.2:8080 10.210.2.2:8080 10.210.3.2:8080 {
import common_proxy import common_proxy
@@ -686,7 +708,7 @@ http://web.example.com {
log log
} }
# Service: db # User-defined config for service 'db'.
# DB admin panel # DB admin panel
dbadmin.example.com { dbadmin.example.com {
basicauth { basicauth {
@@ -695,7 +717,7 @@ dbadmin.example.com {
reverse_proxy 10.210.1.4:5432 reverse_proxy 10.210.1.4:5432
} }
# Service: gateway # User-defined config for service 'gateway'.
# Testing different upstream template functions # Testing different upstream template functions
gateway.example.com { gateway.example.com {
# Current service upstreams (gateway) # 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 service config v2 (most recent)
web-v2.example.com { web-v2.example.com {
reverse_proxy 10.210.1.3:8080 10.210.3.3:8080 10.210.2.3:8080 reverse_proxy 10.210.1.3:8080 10.210.3.3:8080 10.210.2.3:8080