feat: route paths to non-web processes via proxy:route
Adds proxy:route:set/remove/clear/report so users can declare path-prefix routes to non-web Procfile processes without hand-writing nginx.conf.sigil. Routes are stored on the proxy plugin as a PropertyMap of path to process:port:strip, with set semantics so each invocation produces a fully-determined state and re-application is idempotent. A proxy-supports-routes capability trigger gates the command per backend - nginx-vhosts, traefik-vhosts, caddy-vhosts, and scheduler-k3s render routes today; openresty-vhosts and haproxy-vhosts refuse until their sidecar images learn a path-prefix vocabulary (tracked in dokku/openresty-docker-proxy#137 and the haproxy sidecar). Closes #8725.
This commit is contained in:
@@ -292,6 +292,12 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
return cleanup(fmt.Errorf("Error getting security context: %w", err))
|
||||
}
|
||||
|
||||
appRoutes, err := GetAppRoutes(appName)
|
||||
if err != nil {
|
||||
return cleanup(fmt.Errorf("Error getting app routes: %w", err))
|
||||
}
|
||||
routedPorts := RoutedProcessPorts(appRoutes)
|
||||
|
||||
values := &AppValues{
|
||||
Global: GlobalValues{
|
||||
Annotations: globalAnnotations,
|
||||
@@ -311,6 +317,7 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
IngressClass: getComputedIngressClass(),
|
||||
PrimaryPort: primaryPort,
|
||||
PrimaryServicePort: primaryServicePort,
|
||||
Routes: appRoutes,
|
||||
},
|
||||
SecurityContext: securityContext,
|
||||
},
|
||||
@@ -508,13 +515,32 @@ func BuildAppChart(ctx context.Context, appName, imageTag string, opts BuildOpti
|
||||
Protocol: PortmapProtocol_TCP,
|
||||
Scheme: "http",
|
||||
})
|
||||
} else if ports := SortedRoutedPorts(routedPorts, processType); len(ports) > 0 {
|
||||
processValues.Web = ProcessWeb{
|
||||
Domains: []ProcessDomains{},
|
||||
PortMaps: []ProcessPortMap{},
|
||||
TLS: ProcessTls{
|
||||
Enabled: false,
|
||||
},
|
||||
}
|
||||
for _, port := range ports {
|
||||
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
|
||||
ContainerPort: port,
|
||||
HostPort: port,
|
||||
Name: fmt.Sprintf("http-%d-%d", port, port),
|
||||
Protocol: PortmapProtocol_TCP,
|
||||
Scheme: "http",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
values.Processes[processType] = processValues
|
||||
|
||||
templateFiles := []string{"deployment", "keda-scaled-object"}
|
||||
if processType == "web" {
|
||||
templateFiles = append(templateFiles, "service", "certificate", "issuer", "ingress", "ingress-route", "compression-middleware", "https-redirect-middleware", "keda-http-scaled-object", "keda-interceptor-proxy-service")
|
||||
templateFiles = append(templateFiles, "service", "certificate", "issuer", "ingress", "ingress-route", "compression-middleware", "https-redirect-middleware", "strip-prefix-middleware", "keda-http-scaled-object", "keda-interceptor-proxy-service")
|
||||
} else if _, routed := routedPorts[processType]; routed {
|
||||
templateFiles = append(templateFiles, "service")
|
||||
}
|
||||
for _, templateName := range templateFiles {
|
||||
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
|
||||
|
||||
18
plugins/scheduler-k3s/proxy-supports-routes
Executable file
18
plugins/scheduler-k3s/proxy-supports-routes
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
||||
|
||||
trigger-scheduler-k3s-proxy-supports-routes() {
|
||||
declare desc="advertises path-based routing support for k3s"
|
||||
declare trigger="proxy-supports-routes"
|
||||
declare APP="$1"
|
||||
|
||||
if [[ "$(plugn trigger proxy-type "$APP")" != "k3s" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo "true"
|
||||
}
|
||||
|
||||
trigger-scheduler-k3s-proxy-supports-routes "$@"
|
||||
96
plugins/scheduler-k3s/routes.go
Normal file
96
plugins/scheduler-k3s/routes.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package scheduler_k3s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
// GetAppRoutes loads the proxy:route:set route set for an app via the
|
||||
// proxy-routes-list plugin trigger. The trigger emits one
|
||||
// "process|port|path|strip" line per route, sorted by descending path length.
|
||||
// Returns an empty slice when no routes are configured.
|
||||
func GetAppRoutes(appName string) ([]GlobalRoute, error) {
|
||||
result, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "proxy-routes-list",
|
||||
Args: []string{appName},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading proxy routes for %s: %w", appName, err)
|
||||
}
|
||||
|
||||
stdout := result.StdoutContents()
|
||||
if stdout == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
routes := []GlobalRoute{}
|
||||
for _, line := range strings.Split(stdout, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
fields := strings.SplitN(line, "|", 4)
|
||||
if len(fields) < 3 {
|
||||
return nil, fmt.Errorf("invalid route line %q for %s", line, appName)
|
||||
}
|
||||
port, err := strconv.Atoi(fields[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid port %q in route line %q for %s: %w", fields[1], line, appName, err)
|
||||
}
|
||||
strip := false
|
||||
if len(fields) == 4 && fields[3] == "1" {
|
||||
strip = true
|
||||
}
|
||||
routes = append(routes, GlobalRoute{
|
||||
Process: fields[0],
|
||||
Path: fields[2],
|
||||
Port: int32(port),
|
||||
StripPrefix: strip,
|
||||
Slug: routeSlug(fields[2]),
|
||||
})
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
// routeSlug returns a deterministic kebab-case identifier derived from a
|
||||
// route path. Example: "/api/v0" -> "api-v0".
|
||||
func routeSlug(path string) string {
|
||||
s := strings.Trim(path, "/")
|
||||
s = strings.ReplaceAll(s, "/", "-")
|
||||
s = strings.ToLower(s)
|
||||
if s == "" {
|
||||
return "root"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// RoutedProcessPorts returns, per process type, the set of upstream container
|
||||
// ports referenced by any route targeting that process. The chart-values
|
||||
// builder uses this to synthesize port_maps on routed non-web processes so
|
||||
// their Kubernetes Services expose the right port.
|
||||
func RoutedProcessPorts(routes []GlobalRoute) map[string]map[int32]bool {
|
||||
out := map[string]map[int32]bool{}
|
||||
for _, route := range routes {
|
||||
if _, ok := out[route.Process]; !ok {
|
||||
out[route.Process] = map[int32]bool{}
|
||||
}
|
||||
out[route.Process][route.Port] = true
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SortedRoutedPorts returns the ports for a process in deterministic order
|
||||
// so the resulting Service has a stable port list across renders.
|
||||
func SortedRoutedPorts(routedPorts map[string]map[int32]bool, processType string) []int32 {
|
||||
set := routedPorts[processType]
|
||||
ports := make([]int32, 0, len(set))
|
||||
for port := range set {
|
||||
ports = append(ports, port)
|
||||
}
|
||||
sort.Slice(ports, func(i, j int) bool { return ports[i] < ports[j] })
|
||||
return ports
|
||||
}
|
||||
@@ -70,6 +70,31 @@ type GlobalNetwork struct {
|
||||
|
||||
// PrimaryServicePort is the primary service port to use
|
||||
PrimaryServicePort int32 `yaml:"primary_service_port"`
|
||||
|
||||
// Routes are the path-based routes declared via proxy:route:set
|
||||
Routes []GlobalRoute `yaml:"routes,omitempty"`
|
||||
}
|
||||
|
||||
// GlobalRoute is one path-based route declared via proxy:route:set. The
|
||||
// chart templates iterate this slice to emit additional Ingress paths /
|
||||
// IngressRoute Rules per route.
|
||||
type GlobalRoute struct {
|
||||
// Process is the Procfile process type the route targets.
|
||||
Process string `yaml:"process"`
|
||||
|
||||
// Path is the URL path prefix to match.
|
||||
Path string `yaml:"path"`
|
||||
|
||||
// Port is the upstream container port the route forwards to.
|
||||
Port int32 `yaml:"port"`
|
||||
|
||||
// StripPrefix controls whether the matched prefix is stripped before
|
||||
// forwarding the request upstream.
|
||||
StripPrefix bool `yaml:"strip_prefix"`
|
||||
|
||||
// Slug is a deterministic kebab-case identifier derived from Path. It is
|
||||
// used as a stable name fragment for generated CRDs.
|
||||
Slug string `yaml:"slug"`
|
||||
}
|
||||
|
||||
// GlobalKedaValues contains the global keda configuration
|
||||
|
||||
@@ -38,6 +38,28 @@ spec:
|
||||
- {{ $entryPoint }}
|
||||
routes:
|
||||
{{- range $ddx, $domain := $config.web.domains }}
|
||||
{{- range $rdx, $route := $.Values.global.network.routes }}
|
||||
- kind: Rule
|
||||
match: Host(`{{ $domain.name }}`) && PathPrefix(`{{ $route.path }}`)
|
||||
priority: {{ len $route.path }}
|
||||
middlewares:
|
||||
- name: {{ $.Values.global.app_name}}-{{ $processName }}-compression
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
{{- if and (eq $entryPoint "web") $config.web.tls.enabled }}
|
||||
- name: {{ $.Values.global.app_name}}-{{ $processName }}-redirect-to-https
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
{{- end }}
|
||||
{{- if $route.strip_prefix }}
|
||||
- name: {{ $.Values.global.app_name }}-{{ $route.process }}-{{ $route.slug }}-strip
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
{{- end }}
|
||||
services:
|
||||
- name: {{ $.Values.global.app_name }}-{{ $route.process }}
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
passHostHeader: true
|
||||
port: {{ $route.port }}
|
||||
scheme: http
|
||||
{{- end }}
|
||||
- kind: Rule
|
||||
match: {{ include "traefik.host.match" $domain.name }}
|
||||
{{- if hasPrefix "*." $domain.name }}
|
||||
|
||||
@@ -65,6 +65,76 @@ spec:
|
||||
{{- end }}
|
||||
path: /
|
||||
{{- end }}
|
||||
{{- range $rdx, $route := $.Values.global.network.routes }}
|
||||
{{- if not $route.strip_prefix }}
|
||||
- backend:
|
||||
service:
|
||||
name: {{ $.Values.global.app_name }}-{{ $route.process }}
|
||||
port:
|
||||
number: {{ $route.port }}
|
||||
pathType: Prefix
|
||||
path: {{ $route.path }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- /* Strip-prefix routes need their own Ingress objects because the
|
||||
rewrite-target annotation is Ingress-level. */}}
|
||||
{{- range $processName, $config := .Values.processes }}
|
||||
{{- if not (hasKey $config "web") }}
|
||||
{{- continue }}
|
||||
{{- end }}
|
||||
{{- if and $config.web.domains (eq $.Values.global.network.ingress_class "nginx") }}
|
||||
{{- range $pdx, $domain := $config.web.domains }}
|
||||
{{- range $rdx, $route := $.Values.global.network.routes }}
|
||||
{{- if $route.strip_prefix }}
|
||||
---
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
annotations:
|
||||
dokku.com/managed: "true"
|
||||
dokku.com/ingress-method: "routes"
|
||||
nginx.ingress.kubernetes.io/rewrite-target: /$2
|
||||
{{- if $config.web.tls.enabled }}
|
||||
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
|
||||
{{- end }}
|
||||
{{ include "print.annotations" (dict "config" $.Values.global "key" "ingress") | indent 4 }}
|
||||
{{ include "print.annotations" (dict "config" $config "key" "ingress") | indent 4 }}
|
||||
labels:
|
||||
app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $route.process }}
|
||||
app.kubernetes.io/name: {{ $route.process }}
|
||||
app.kubernetes.io/part-of: {{ $.Values.global.app_name }}
|
||||
{{ include "print.labels" (dict "config" $.Values.global "key" "ingress") | indent 4 }}
|
||||
{{ include "print.labels" (dict "config" $config "key" "ingress") | indent 4 }}
|
||||
name: {{ $.Values.global.app_name }}-{{ $route.process }}-{{ $route.slug }}-strip-{{ $domain.slug }}
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
spec:
|
||||
ingressClassName: nginx
|
||||
{{- if $config.web.tls.enabled }}
|
||||
tls:
|
||||
- hosts:
|
||||
- {{ $domain.name | quote }}
|
||||
{{- if $config.web.tls.use_imported_cert }}
|
||||
secretName: tls-{{ $.Values.global.app_name }}
|
||||
{{- else }}
|
||||
secretName: tls-{{ $.Values.global.app_name }}-{{ $processName }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
rules:
|
||||
- host: {{ $domain.name | quote }}
|
||||
http:
|
||||
paths:
|
||||
- backend:
|
||||
service:
|
||||
name: {{ $.Values.global.app_name }}-{{ $route.process }}
|
||||
port:
|
||||
number: {{ $route.port }}
|
||||
pathType: ImplementationSpecific
|
||||
path: {{ $route.path }}(/|$)(.*)
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
{{- /* Emit a StripPrefix Middleware CRD per route that uses --strip-prefix
|
||||
when the ingress class is traefik. Referenced from the IngressRoute
|
||||
Rule rendered in ingress-route.yaml. */}}
|
||||
{{- range $processName, $config := .Values.processes }}
|
||||
{{- if not (hasKey $config "web") }}
|
||||
{{- continue }}
|
||||
{{- end }}
|
||||
|
||||
{{- if and $config.web.domains (eq $.Values.global.network.ingress_class "traefik") }}
|
||||
{{- range $rdx, $route := $.Values.global.network.routes }}
|
||||
{{- if $route.strip_prefix }}
|
||||
---
|
||||
apiVersion: traefik.io/v1alpha1
|
||||
kind: Middleware
|
||||
metadata:
|
||||
annotations:
|
||||
dokku.com/managed: "true"
|
||||
{{ include "print.annotations" (dict "config" $.Values.global "key" "traefik_middleware") | indent 4 }}
|
||||
{{ include "print.annotations" (dict "config" $config "key" "traefik_middleware") | indent 4 }}
|
||||
labels:
|
||||
app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $route.process }}-{{ $route.slug }}-strip
|
||||
app.kubernetes.io/name: {{ $route.process }}
|
||||
app.kubernetes.io/part-of: {{ $.Values.global.app_name }}
|
||||
{{ include "print.labels" (dict "config" $.Values.global "key" "traefik_middleware") | indent 4 }}
|
||||
{{ include "print.labels" (dict "config" $config "key" "traefik_middleware") | indent 4 }}
|
||||
name: {{ $.Values.global.app_name }}-{{ $route.process }}-{{ $route.slug }}-strip
|
||||
namespace: {{ $.Values.global.namespace }}
|
||||
spec:
|
||||
stripPrefix:
|
||||
prefixes:
|
||||
- {{ $route.path }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
Reference in New Issue
Block a user