diff --git a/plugins/scheduler-k3s/functions.go b/plugins/scheduler-k3s/functions.go index 9d39ead31..a5b7101a3 100644 --- a/plugins/scheduler-k3s/functions.go +++ b/plugins/scheduler-k3s/functions.go @@ -14,6 +14,7 @@ import ( appjson "github.com/dokku/dokku/plugins/app-json" "github.com/dokku/dokku/plugins/common" + nginxvhosts "github.com/dokku/dokku/plugins/nginx-vhosts" resty "github.com/go-resty/resty/v2" "golang.org/x/sync/errgroup" "gopkg.in/yaml.v3" @@ -498,6 +499,167 @@ func getGlobalIngressClass() string { return common.PropertyGetDefault("scheduler-k3s", "global", "ingress-class", DefaultIngressClass) } +func getIngressAnnotations(appName string, processType string) map[string]string { + type annotation struct { + annotation string + getter func(appName string) string + locationSnippet func(value string) string + serverSnippet func(value string) string + } + + locationLines := []string{} + serverLines := []string{} + + properties := map[string]annotation{ + "access-log-path": { + getter: nginxvhosts.ComputedAccessLogPath, + serverSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("access_log %s;", value) + }, + }, + "bind-address-ipv4": { + getter: nginxvhosts.ComputedBindAddressIPv4, + }, + "bind-address-ipv6": { + getter: nginxvhosts.ComputedBindAddressIPv6, + }, + "client-max-body-size": { + annotation: "nginx.ingress.kubernetes.io/proxy-body-size", + getter: nginxvhosts.ComputedClientMaxBodySize, + }, + "disable-custom-config": { + getter: nginxvhosts.ComputedDisableCustomConfig, + }, + "error-log-path": { + getter: nginxvhosts.ComputedErrorLogPath, + serverSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("error_log %s;", value) + }, + }, + // todo: handle hsts properly + "hsts-include-subdomains": { + getter: nginxvhosts.ComputedHSTSIncludeSubdomains, + }, + "hsts-max-age": { + getter: nginxvhosts.ComputedHSTSMaxAge, + }, + "hsts-preload": { + getter: nginxvhosts.ComputedHSTSPreload, + }, + "hsts": { + getter: nginxvhosts.ComputedHSTS, + }, + "nginx-conf-sigil-path": { + getter: nginxvhosts.ComputedNginxConfSigilPath, + }, + "proxy-buffer-size": { + annotation: "nginx.ingress.kubernetes.io/proxy-buffer-size", + getter: nginxvhosts.ComputedProxyBufferSize, + }, + "proxy-buffering": { + getter: nginxvhosts.ComputedProxyBuffering, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_buffering %s;", value) + }, + }, + "proxy-buffers": { + annotation: "nginx.ingress.kubernetes.io/proxy-buffers-number", + getter: nginxvhosts.ComputedProxyBuffers, + }, + "proxy-busy-buffers-size": { + getter: nginxvhosts.ComputedProxyBusyBuffersSize, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_busy_buffers_size %s;", value) + }, + }, + "proxy-read-timeout": { + annotation: "nginx.ingress.kubernetes.io/proxy-read-timeout", + getter: nginxvhosts.ComputedProxyReadTimeout, + }, + "x-forwarded-for-value": { + getter: nginxvhosts.ComputedXForwardedForValue, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_set_header X-Forwarded-For %s;", value) + }, + }, + "x-forwarded-port-value": { + getter: nginxvhosts.ComputedXForwardedPortValue, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_set_header X-Forwarded-Port %s;", value) + }, + }, + "x-forwarded-proto-value": { + getter: nginxvhosts.ComputedXForwardedProtoValue, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_set_header X-Forwarded-Proto %s;", value) + }, + }, + "x-forwarded-ssl": { + getter: nginxvhosts.ComputedXForwardedSSL, + locationSnippet: func(value string) string { + if value == "" { + return "" + } + return fmt.Sprintf("proxy_set_header X-Forwarded-SSL %s;", value) + }, + }, + } + + data := map[string]string{} + for _, newKey := range properties { + if newKey.locationSnippet != nil { + locationLines = append(locationLines, newKey.locationSnippet(newKey.getter(appName))) + } else if newKey.serverSnippet != nil { + serverLines = append(serverLines, newKey.serverSnippet(newKey.getter(appName))) + } else if newKey.annotation != "" { + data[newKey.annotation] = newKey.getter(appName) + } + } + + var locationSnippet string + for _, line := range locationLines { + if line != "" { + locationSnippet += line + "\n" + } + } + var serverSnippet string + for _, line := range serverLines { + if line != "" { + serverSnippet += line + "\n" + } + } + + if locationSnippet != "" { + data["nginx.ingress.kubernetes.io/configuration-snippet"] = locationSnippet + } + if serverSnippet != "" { + data["nginx.ingress.kubernetes.io/server-snippet"] = serverSnippet + } + + return data +} + func getLetsencryptServer(appName string) string { return common.PropertyGetDefault("scheduler-k3s", appName, "letsencrypt-server", "") } diff --git a/plugins/scheduler-k3s/go.mod b/plugins/scheduler-k3s/go.mod index ba0ea0ab4..9ef162d0d 100644 --- a/plugins/scheduler-k3s/go.mod +++ b/plugins/scheduler-k3s/go.mod @@ -8,6 +8,7 @@ require ( github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/config v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/cron v0.0.0-00010101000000-000000000000 + github.com/dokku/dokku/plugins/nginx-vhosts v0.0.0-00010101000000-000000000000 github.com/fatih/color v1.16.0 github.com/go-openapi/jsonpointer v0.19.6 github.com/go-resty/resty/v2 v2.11.0 @@ -18,6 +19,7 @@ require ( github.com/ryanuber/columnize v2.1.2+incompatible github.com/spf13/pflag v1.0.5 github.com/traefik/traefik/v2 v2.10.7 + golang.org/x/sync v0.6.0 gopkg.in/yaml.v3 v3.0.1 helm.sh/helm/v3 v3.13.3 k8s.io/api v0.29.0 @@ -152,7 +154,6 @@ require ( golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.12.0 // indirect - golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect @@ -186,3 +187,5 @@ replace github.com/dokku/dokku/plugins/common => ../common replace github.com/dokku/dokku/plugins/config => ../config replace github.com/dokku/dokku/plugins/cron => ../cron + +replace github.com/dokku/dokku/plugins/nginx-vhosts => ../nginx-vhosts diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index 16f824271..1591ecb03 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -410,6 +410,8 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e }, } + processValues.Annotations.IngressAnnotations = getIngressAnnotations(appName, processType) + processValues.ProcessType = ProcessType_Web for _, portMap := range portMaps { protocol := PortmapProtocol_TCP