Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/settings-catalog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ func knownFunction(name string) (functionSpec, bool) {
"GetBoolOrDefault": directSpec("bool", 3),
"GetBoolOrFalse": directSpec("bool", 2),
"GetBoolOrTrue": directSpec("bool", 2),
"GetDuration": directSpec("string", 2),
"GetDurationOrDefault": directSpec("string", 3),
"GetMap": directSpec("map[string]string", 2),
"GetMapOrEmpty": directSpec("map[string]string", 2),
"GetEnvVars": {
Expand Down
2 changes: 2 additions & 0 deletions docs/09-Configuration reference/01-Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ While we have some basic types (string, number, bool ...), we also have some com
| gateway.ingress.tls.enabled | bool | true | Enable TLS if not enabled at Gateway CRD level |
| gateway.caddyfile.trusted-proxies | string | 10.0.0.0/8,192.168.0.0/16 | Comma-separated list of IP ranges (CIDRs) of trusted proxy servers. Caddy will parse the real client IP from HTTP headers when requests come from these proxies. Use `private_ranges` to match all private IPv4 and IPv6 ranges. |
| gateway.caddyfile.trusted-proxies-strict | bool | false | Enable strict (right-to-left) parsing of the X-Forwarded-For header. Recommended when using upstream proxies like HAProxy, Cloudflare, AWS ALB, or CloudFront. |
| gateway.caddyfile.shutdown-delay | string | | Delay before Caddy starts the graceful shutdown sequence, allowing load balancers to remove the pod from rotation before in-flight requests are drained. Use Go duration format (e.g., 30s, 5m). |
| gateway.caddyfile.grace-period | string | | Maximum time to wait for in-flight requests to complete before forcefully closing connections during shutdown. Use Go duration format (e.g., 30s, 5m). |
| gateway.config.idle-timeout | string | 10m | Configure the idle timeout for client connections (default: 5m). Use Go duration format (e.g., 30s, 5m, 1h). |
| gateway.dns.private.enabled | bool | false | Enable generation of private DNS endpoints for the gateway |
| gateway.dns.private.dns-names | string | | DNS name pattern(s) for private DNS endpoints. Comma-separated list. Supports `{stack}` placeholder |
Expand Down
16 changes: 15 additions & 1 deletion docs/09-Configuration reference/settings.catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@
"internal/resources/applications/application.go:357"
]
},
{
"key": "gateway.caddyfile.grace-period",
"valueType": "string",
"sources": [
"internal/resources/gateways/configuration.go:41"
]
},
{
"key": "gateway.caddyfile.shutdown-delay",
"valueType": "string",
"sources": [
"internal/resources/gateways/configuration.go:33"
]
},
{
"key": "gateway.caddyfile.trusted-proxies",
"valueType": "string[]",
Expand All @@ -209,7 +223,7 @@
"key": "gateway.config.idle-timeout",
"valueType": "string",
"sources": [
"internal/resources/gateways/configuration.go:33"
"internal/resources/gateways/configuration.go:49"
]
},
{
Expand Down
7 changes: 7 additions & 0 deletions internal/resources/gateways/Caddyfile.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
# Global metrics endpoint (moved from servers block - deprecated location)
metrics

{{- if .ShutdownDelay }}
shutdown_delay {{ .ShutdownDelay }}
{{- end }}
{{- if .GracePeriod }}
grace_period {{ .GracePeriod }}
{{- end }}

servers {
{{- if and .TrustedProxies (gt (len .TrustedProxies) 0) }}
trusted_proxies {{ .TrustedProxies }}
Expand Down
19 changes: 17 additions & 2 deletions internal/resources/gateways/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateways

import (
"strings"
"time"

collectionutils "github.com/formancehq/go-libs/v5/pkg/types/collections"

Expand Down Expand Up @@ -55,9 +56,23 @@ func withTrustedProxiesStrict() func(data map[string]any) error {
}
}

func withIdleTimeout(timeout string) func(data map[string]any) error {
func withIdleTimeout(timeout time.Duration) func(data map[string]any) error {
return func(data map[string]any) error {
data["IdleTimeout"] = timeout
data["IdleTimeout"] = timeout.String()
return nil
}
}

func withShutdownDelay(delay time.Duration) func(data map[string]any) error {
return func(data map[string]any) error {
data["ShutdownDelay"] = delay.String()
return nil
}
}

func withGracePeriod(period time.Duration) func(data map[string]any) error {
return func(data map[string]any) error {
data["GracePeriod"] = period.String()
return nil
}
}
20 changes: 18 additions & 2 deletions internal/resources/gateways/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,27 @@ func createConfigMap(ctx core.Context, stack *v1beta1.Stack,
options = append(options, withTrustedProxiesStrict())
}

idleTimeout, err := settings.GetString(ctx, stack.Name, "gateway", "config", "idle-timeout")
shutdownDelay, err := settings.GetDuration(ctx, stack.Name, "gateway", "caddyfile", "shutdown-delay")
if err != nil {
return nil, err
}
if idleTimeout != nil && *idleTimeout != "" {
if shutdownDelay != nil {
options = append(options, withShutdownDelay(*shutdownDelay))
}

gracePeriod, err := settings.GetDuration(ctx, stack.Name, "gateway", "caddyfile", "grace-period")
if err != nil {
return nil, err
}
if gracePeriod != nil {
options = append(options, withGracePeriod(*gracePeriod))
}

idleTimeout, err := settings.GetDuration(ctx, stack.Name, "gateway", "config", "idle-timeout")
if err != nil {
return nil, err
}
if idleTimeout != nil {
options = append(options, withIdleTimeout(*idleTimeout))
}

Expand Down
28 changes: 28 additions & 0 deletions internal/resources/settings/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"slices"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -424,6 +425,33 @@ func GetMapOrEmpty(ctx core.Context, stack string, keys ...string) (map[string]s
return value, nil
}

func GetDuration(ctx core.Context, stack string, keys ...string) (*time.Duration, error) {
value, err := GetString(ctx, stack, keys...)
if err != nil {
return nil, err
}

if value == nil {
return nil, nil
}
duration, err := time.ParseDuration(*value)
if err != nil {
return nil, err
}
return &duration, nil
}

func GetDurationOrDefault(ctx core.Context, stack string, defaultValue time.Duration, keys ...string) (time.Duration, error) {
duration, err := GetDuration(ctx, stack, keys...)
if err != nil {
return 0, err
}
if duration == nil {
return defaultValue, nil
}
return *duration, nil
}

func findMatchingSettings(settings []v1beta1.Settings, flattenKeys ...string) (*string, error) {

// Keys can be passed as "a.b.c", instead of "a", "b", "c"
Expand Down
Loading