From 7fbb86093bdbda735d05b1159c970711341e9c77 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 19 Jan 2024 04:13:55 -0500 Subject: [PATCH] feat: implement global support for setting proxy type --- docs/networking/proxy-management.md | 28 +++++++++++++++++++- plugins/proxy/functions.go | 15 ++++++++++- plugins/proxy/report.go | 19 +++++++++++-- plugins/proxy/src/subcommands/subcommands.go | 5 ++++ plugins/proxy/subcommands.go | 12 ++++++--- plugins/proxy/triggers.go | 2 +- 6 files changed, 73 insertions(+), 8 deletions(-) diff --git a/docs/networking/proxy-management.md b/docs/networking/proxy-management.md index 49f58890b..470b525ec 100644 --- a/docs/networking/proxy-management.md +++ b/docs/networking/proxy-management.md @@ -9,13 +9,39 @@ proxy:clear-config [--all|] # Clears config for given app proxy:disable [--parallel count] [--all|] # Disable proxy for app proxy:enable [--parallel count] [--all|] # Enable proxy for app proxy:report [] [] # Displays a proxy report for one or more apps -proxy:set # Set proxy type for app +proxy:set [|--global] # Set proxy type for app ``` In Dokku 0.5.0, port proxying was decoupled from the `nginx-vhosts` plugin into the proxy plugin. Dokku 0.6.0 introduced the ability to map host ports to specific container ports. In the future this will allow other proxy software - such as HAProxy or Caddy - to be used in place of nginx. ## Usage +### Changing the proxy + +The default proxy shipped with Dokku is `nginx`. It can be changed via the `proxy:set` command. + +```shell +dokku proxy:set node-js-app caddy +``` + +``` +-----> Setting config vars + DOKKU_APP_PROXY_TYPE: caddy +``` + +The proxy may also be set on a global basis. This is usually preferred as running multiple proxy implementations may cause port collision issues. + +```shell +dokku proxy:set --global caddy +``` + +``` +-----> Setting config vars + DOKKU_PROXY_TYPE: caddy +``` + +Changing the proxy does not stop or start any given proxy implementation. Please see the documentation for your proxy implementation for details on how to perform a change. + ### Regenerating proxy config In certain cases, your app proxy configs may drift from the correct config for your app. You may regenerate the config at any point via the `proxy:build-config` command. This command will trigger a rebuild for the configured proxy implementation (default: nginx) for a given app. The command may fail if there are no current web listeners for your app. diff --git a/plugins/proxy/functions.go b/plugins/proxy/functions.go index 4b5467f50..bf1fedc3c 100644 --- a/plugins/proxy/functions.go +++ b/plugins/proxy/functions.go @@ -5,5 +5,18 @@ import ( ) func getAppProxyType(appName string) string { - return config.GetWithDefault(appName, "DOKKU_APP_PROXY_TYPE", "nginx") + return config.GetWithDefault(appName, "DOKKU_APP_PROXY_TYPE", "") +} + +func getComputedProxyType(appName string) string { + proxyType := getGlobalProxyType() + if proxyType == "" { + proxyType = getAppProxyType(appName) + } + + return proxyType +} + +func getGlobalProxyType() string { + return config.GetWithDefault("--global", "DOKKU_PROXY_TYPE", "nginx") } diff --git a/plugins/proxy/report.go b/plugins/proxy/report.go index 5db7792a2..00f126cd1 100644 --- a/plugins/proxy/report.go +++ b/plugins/proxy/report.go @@ -11,8 +11,10 @@ func ReportSingleApp(appName string, format string, infoFlag string) error { } flags := map[string]common.ReportFunc{ - "--proxy-enabled": reportEnabled, - "--proxy-type": reportType, + "--proxy-enabled": reportEnabled, + "--proxy-computed-type": reportComputedType, + "--proxy-global-type": reportGlobalType, + "--proxy-type": reportType, } flagKeys := []string{} @@ -35,6 +37,19 @@ func reportEnabled(appName string) string { return proxyEnabled } +func reportComputedType(appName string) string { + proxyType := getGlobalProxyType() + if proxyType == "" { + proxyType = getAppProxyType(appName) + } + + return proxyType +} + +func reportGlobalType(appName string) string { + return getGlobalProxyType() +} + func reportType(appName string) string { return getAppProxyType(appName) } diff --git a/plugins/proxy/src/subcommands/subcommands.go b/plugins/proxy/src/subcommands/subcommands.go index d181068da..ca06d2660 100644 --- a/plugins/proxy/src/subcommands/subcommands.go +++ b/plugins/proxy/src/subcommands/subcommands.go @@ -56,9 +56,14 @@ func main() { } case "set": args := flag.NewFlagSet("proxy:set", flag.ExitOnError) + global := args.Bool("global", false, "--global: set a global property") args.Parse(os.Args[2:]) appName := args.Arg(0) proxyType := args.Arg(1) + if *global { + appName = "--global" + proxyType = args.Arg(0) + } err = proxy.CommandSet(appName, proxyType) default: err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) diff --git a/plugins/proxy/subcommands.go b/plugins/proxy/subcommands.go index 58a57a870..755ff9338 100644 --- a/plugins/proxy/subcommands.go +++ b/plugins/proxy/subcommands.go @@ -79,16 +79,22 @@ func CommandReport(appName string, format string, infoFlag string) error { // CommandSet sets a proxy for an app func CommandSet(appName string, proxyType string) error { - if err := common.VerifyAppName(appName); err != nil { - return err + if appName != "--global" { + if err := common.VerifyAppName(appName); err != nil { + return err + } } if len(proxyType) < 2 { return errors.New("Please specify a proxy type") } + key := "DOKKU_APP_PROXY_TYPE" + if appName == "--global" { + key = "DOKKU_PROXY_TYPE" + } entries := map[string]string{ - "DOKKU_APP_PROXY_TYPE": proxyType, + key: proxyType, } return config.SetMany(appName, entries, false) } diff --git a/plugins/proxy/triggers.go b/plugins/proxy/triggers.go index 49c0fef06..0dff90629 100644 --- a/plugins/proxy/triggers.go +++ b/plugins/proxy/triggers.go @@ -17,7 +17,7 @@ func TriggerProxyIsEnabled(appName string) error { // TriggerProxyType prints out the current proxy type, defaulting to nginx func TriggerProxyType(appName string) error { - proxyType := getAppProxyType(appName) + proxyType := getComputedProxyType(appName) fmt.Println(proxyType) return nil