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 (
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
}
+41 -19
View File
@@ -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