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
}