refactor: move all port management code to standalone ports plugin

This change makes interacting with port mappings more clear - folks might previously set the port mapping to the proxy type or vice-versa.

The existing proxy:ports* commands still exist but will show a deprecation warning for a single minor release.
This commit is contained in:
Jose Diaz-Gonzalez
2023-07-02 01:29:01 -04:00
parent a0b84d52ef
commit 64f0f2674d
43 changed files with 894 additions and 553 deletions

View File

@@ -6,4 +6,4 @@
/install
/post-*
/report
!/proxy-configure-ports

View File

@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/build-config subcommands/clear-config subcommands/disable subcommands/enable subcommands/ports subcommands/ports-add subcommands/ports-clear subcommands/ports-remove subcommands/ports-set subcommands/report subcommands/set
TRIGGERS = triggers/proxy-is-enabled triggers/proxy-type triggers/post-certs-remove triggers/post-certs-update triggers/report
TRIGGERS = triggers/proxy-is-enabled triggers/proxy-type triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = proxy

View File

@@ -1,192 +1,9 @@
package proxy
import (
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
"github.com/ryanuber/columnize"
)
func addProxyPorts(appName string, proxyPortMap []PortMap) error {
allPortMaps := getProxyPortMap(appName)
allPortMaps = append(allPortMaps, proxyPortMap...)
return setProxyPorts(appName, allPortMaps)
}
func filterAppProxyPorts(appName string, scheme string, hostPort int) []PortMap {
var filteredProxyMaps []PortMap
for _, portMap := range getProxyPortMap(appName) {
if portMap.Scheme == scheme && portMap.HostPort == hostPort {
filteredProxyMaps = append(filteredProxyMaps, portMap)
}
}
return filteredProxyMaps
}
func getAppProxyType(appName string) string {
return config.GetWithDefault(appName, "DOKKU_APP_PROXY_TYPE", "nginx")
}
func getProxyPortMap(appName string) []PortMap {
value := config.GetWithDefault(appName, "DOKKU_PROXY_PORT_MAP", "")
portMaps, _ := parseProxyPortMapString(value)
return portMaps
}
func inRange(value int, min int, max int) bool {
return min < value && value < max
}
func listAppProxyPorts(appName string) error {
proxyPortMap := getProxyPortMap(appName)
if len(proxyPortMap) == 0 {
return errors.New("No port mappings configured for app")
}
var lines []string
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
lines = append(lines, "-----> scheme:host port:container port")
}
for _, portMap := range proxyPortMap {
lines = append(lines, portMap.String())
}
sort.Strings(lines)
common.LogInfo1Quiet(fmt.Sprintf("Port mappings for %s", appName))
config := columnize.DefaultConfig()
config.Delim = ":"
config.Prefix = " "
config.Empty = ""
fmt.Println(columnize.Format(lines, config))
return nil
}
func parseProxyPortMapString(stringPortMap string) ([]PortMap, error) {
var proxyPortMap []PortMap
for _, v := range strings.Split(strings.TrimSpace(stringPortMap), " ") {
parts := strings.SplitN(v, ":", 3)
if len(parts) == 1 {
hostPort, err := strconv.Atoi(v)
if err != nil {
return proxyPortMap, fmt.Errorf("Invalid port map %s [err=%s]", v, err.Error())
}
if !inRange(hostPort, 0, 65536) {
return proxyPortMap, fmt.Errorf("Invalid port map %s [hostPort=%d]", v, hostPort)
}
proxyPortMap = append(proxyPortMap, PortMap{
HostPort: hostPort,
Scheme: "__internal__",
})
continue
}
if len(parts) != 3 {
return proxyPortMap, fmt.Errorf("Invalid port map %s [len=%d]", v, len(parts))
}
hostPort, err := strconv.Atoi(parts[1])
if err != nil {
return proxyPortMap, fmt.Errorf("Invalid port map %s [err=%s]", v, err.Error())
}
containerPort, err := strconv.Atoi(parts[2])
if err != nil {
return proxyPortMap, fmt.Errorf("Invalid port map %s [err=%s]", v, err.Error())
}
if !inRange(hostPort, 0, 65536) {
return proxyPortMap, fmt.Errorf("Invalid port map %s [hostPort=%d]", v, hostPort)
}
if !inRange(containerPort, 0, 65536) {
return proxyPortMap, fmt.Errorf("Invalid port map %s [containerPort=%d]", v, containerPort)
}
proxyPortMap = append(proxyPortMap, PortMap{
ContainerPort: containerPort,
HostPort: hostPort,
Scheme: parts[0],
})
}
return uniqueProxyPortMap(proxyPortMap), nil
}
func removeProxyPorts(appName string, proxyPortMap []PortMap) error {
toRemove := map[string]bool{}
toRemoveByPort := map[int]bool{}
for _, portMap := range proxyPortMap {
if portMap.AllowsPersistence() {
toRemoveByPort[portMap.HostPort] = true
continue
}
toRemove[portMap.String()] = true
}
var toSet []PortMap
for _, portMap := range getProxyPortMap(appName) {
if toRemove[portMap.String()] {
continue
}
if toRemoveByPort[portMap.HostPort] {
continue
}
toSet = append(toSet, portMap)
}
if len(toSet) == 0 {
keys := []string{"DOKKU_PROXY_PORT_MAP"}
return config.UnsetMany(appName, keys, false)
}
return setProxyPorts(appName, toSet)
}
func setProxyPorts(appName string, proxyPortMap []PortMap) error {
var value []string
for _, portMap := range uniqueProxyPortMap(proxyPortMap) {
if portMap.AllowsPersistence() {
continue
}
value = append(value, portMap.String())
}
sort.Strings(value)
entries := map[string]string{
"DOKKU_PROXY_PORT_MAP": strings.Join(value, " "),
}
return config.SetMany(appName, entries, false)
}
func uniqueProxyPortMap(proxyPortMap []PortMap) []PortMap {
var unique []PortMap
existingPortMaps := map[string]bool{}
for _, portMap := range proxyPortMap {
if existingPortMaps[portMap.String()] {
continue
}
existingPortMaps[portMap.String()] = true
unique = append(unique, portMap)
}
return unique
}

View File

@@ -5,7 +5,7 @@ go 1.19
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/ryanuber/columnize v2.1.2+incompatible
github.com/dokku/dokku/plugins/ports v0.0.0-00010101000000-000000000000
github.com/spf13/pflag v1.0.5
)
@@ -14,6 +14,7 @@ require (
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect
github.com/joho/godotenv v1.2.0 // indirect
github.com/otiai10/copy v1.12.0 // indirect
github.com/ryanuber/columnize v2.1.2+incompatible // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)
@@ -21,3 +22,5 @@ require (
replace github.com/dokku/dokku/plugins/common => ../common
replace github.com/dokku/dokku/plugins/config => ../config
replace github.com/dokku/dokku/plugins/ports => ../ports

View File

@@ -1,64 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-proxy-proxy-configure-ports() {
declare desc="proxy proxy-configure-ports plugin trigger"
declare trigger="proxy-configure-ports"
declare APP="$1"
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")"
local DOKKU_PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT)
local DOKKU_PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT)
local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP)
local IS_APP_VHOST_ENABLED=true
local UPSTREAM_PORT="5000"
plugn trigger domains-vhost-enabled "$APP" 2>/dev/null || IS_APP_VHOST_ENABLED=false
if [[ -z "$DOKKU_PROXY_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then
if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then
dokku_log_info1 "No proxy port set, setting to random open high port"
local PROXY_PORT=$(get_available_port)
else
local PROXY_PORT=$(config_get --global DOKKU_PROXY_PORT)
PROXY_PORT=${PROXY_PORT:=80}
fi
DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT="$PROXY_PORT"
fi
if [[ -z "$DOKKU_PROXY_SSL_PORT" ]]; then
if [[ "$(plugn trigger certs-exists "$APP")" == "true" ]]; then
local PROXY_SSL_PORT=$(config_get --global DOKKU_PROXY_SSL_PORT)
PROXY_SSL_PORT=${PROXY_SSL_PORT:=443}
if [[ -z "$RAW_TCP_PORTS" ]] && [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then
dokku_log_info1 "No proxy ssl port set, setting to random open high port"
PROXY_SSL_PORT=$(get_available_port)
fi
DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_SSL_PORT="$PROXY_SSL_PORT"
fi
fi
if [[ -z "$DOKKU_PROXY_PORT_MAP" ]]; then
if [[ -n "$RAW_TCP_PORTS" ]]; then
local RAW_TCP_PORT
for RAW_TCP_PORT in $RAW_TCP_PORTS; do
local PROXY_PORT_MAP+=" http:${RAW_TCP_PORT}:${RAW_TCP_PORT} "
done
else
local PROXY_PORT=${PROXY_PORT:-$DOKKU_PROXY_PORT}
local PROXY_SSL_PORT=${PROXY_SSL_PORT:-$DOKKU_PROXY_SSL_PORT}
[[ -f "$DOKKU_ROOT/$APP/PORT.web.1" ]] && local UPSTREAM_PORT="$(<"$DOKKU_ROOT/$APP/PORT.web.1")"
if [[ -n "$PROXY_PORT" ]] && [[ -n "$PROXY_SSL_PORT" ]]; then
local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT https:${PROXY_SSL_PORT}:$UPSTREAM_PORT "
elif [[ -n "$PROXY_PORT" ]]; then
local PROXY_PORT_MAP+=" http:${PROXY_PORT}:$UPSTREAM_PORT "
fi
fi
if [[ -n "$PROXY_PORT_MAP" ]]; then
local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)"
local PROXY_PORT_MAP+=" $(merge_dedupe_list "$(remove_val_from_list "$PORT_MAP" "$DOKKU_PROXY_PORT_MAP" " ")" " ") "
DOKKU_QUIET_OUTPUT=1 config_set --no-restart "$APP" DOKKU_PROXY_PORT_MAP="$PROXY_PORT_MAP"
fi
fi
}
trigger-proxy-proxy-configure-ports "$@"

View File

@@ -1,8 +1,6 @@
package proxy
import (
"fmt"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
)
@@ -11,22 +9,6 @@ import (
// and defaults to -1 (false)
const RunInSerial = 0
// PortMap is a struct that contains a scheme:host-port:container-port mapping
type PortMap struct {
ContainerPort int
HostPort int
Scheme string
}
func (p PortMap) String() string {
return fmt.Sprintf("%s:%d:%d", p.Scheme, p.HostPort, p.ContainerPort)
}
// AllowsPersistence returns true if the port map is not to be persisted
func (p PortMap) AllowsPersistence() bool {
return p.Scheme == "__internal__"
}
// BuildConfig rebuilds the proxy config for the specified app
func BuildConfig(appName string) error {
return common.PlugnTrigger("proxy-build-config", []string{appName}...)

View File

@@ -1,8 +1,6 @@
package proxy
import (
"strings"
"github.com/dokku/dokku/plugins/common"
)
@@ -13,9 +11,8 @@ func ReportSingleApp(appName string, format string, infoFlag string) error {
}
flags := map[string]common.ReportFunc{
"--proxy-enabled": reportEnabled,
"--proxy-type": reportType,
"--proxy-port-map": reportPortMap,
"--proxy-enabled": reportEnabled,
"--proxy-type": reportType,
}
flagKeys := []string{}
@@ -41,12 +38,3 @@ func reportEnabled(appName string) string {
func reportType(appName string) string {
return getAppProxyType(appName)
}
func reportPortMap(appName string) string {
var proxyPortMap []string
for _, portMap := range getProxyPortMap(appName) {
proxyPortMap = append(proxyPortMap, portMap.String())
}
return strings.Join(proxyPortMap, " ")
}

View File

@@ -22,11 +22,6 @@ Additional commands:`
proxy:clear-config [--all|<app>], Clears config for a given app
proxy:disable <app>, Disable proxy for app
proxy:enable <app>, Enable proxy for app
proxy:ports <app>, List proxy port mappings for app
proxy:ports-add <app> [<scheme>:<host-port>:<container-port>...], Add proxy port mappings to an app
proxy:ports-clear <app>, Clear all proxy port mappings for an app
proxy:ports-remove <app> [<host-port>|<scheme>:<host-port>:<container-port>...], Remove specific proxy port mappings from an app
proxy:ports-set <app> [<scheme>:<host-port>:<container-port>...], Set proxy port mappings for an app
proxy:report [<app>] [<flag>], Displays a proxy report for one or more apps
proxy:set <app> <proxy-type>, Set proxy type for app
`

View File

@@ -6,6 +6,7 @@ import (
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/ports"
"github.com/dokku/dokku/plugins/proxy"
flag "github.com/spf13/pflag"
@@ -49,30 +50,35 @@ func main() {
args := flag.NewFlagSet("proxy:ports", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
err = proxy.CommandPorts(appName)
common.LogWarn("Deprecated: Use 'ports:list' instead")
err = ports.CommandList(appName)
case "ports-add":
args := flag.NewFlagSet("proxy:ports-add", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsAdd(appName, portMaps)
common.LogWarn("Deprecated: Use 'ports:add' instead")
err = ports.CommandAdd(appName, portMaps)
case "ports-clear":
args := flag.NewFlagSet("proxy:ports-clear", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
err = proxy.CommandPortsClear(appName)
common.LogWarn("Deprecated: Use 'ports:clear' instead")
err = ports.CommandClear(appName)
case "ports-remove":
args := flag.NewFlagSet("proxy:ports-remove", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsRemove(appName, portMaps)
common.LogWarn("Deprecated: Use 'ports:remove' instead")
err = ports.CommandRemove(appName, portMaps)
case "ports-set":
args := flag.NewFlagSet("proxy:ports-set", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsSet(appName, portMaps)
common.LogWarn("Deprecated: Use 'ports:set' instead")
err = ports.CommandSet(appName, portMaps)
case "report":
args := flag.NewFlagSet("proxy:report", flag.ExitOnError)
format := args.String("format", "stdout", "format: [ stdout | json ]")

View File

@@ -24,12 +24,6 @@ func main() {
case "proxy-type":
appName := flag.Arg(0)
err = proxy.TriggerProxyType(appName)
case "post-certs-remove":
appName := flag.Arg(0)
err = proxy.TriggerPostCertsRemove(appName)
case "post-certs-update":
appName := flag.Arg(0)
err = proxy.TriggerPostCertsUpdate(appName)
case "report":
appName := flag.Arg(0)
err = proxy.ReportSingleApp(appName, "", "")

View File

@@ -2,7 +2,6 @@ package proxy
import (
"errors"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
@@ -60,95 +59,6 @@ func CommandEnable(appName string, allApps bool, parallelCount int) error {
return Enable(appName)
}
// CommandPorts is a cmd wrapper to list proxy port mappings for an app
func CommandPorts(appName string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
return listAppProxyPorts(appName)
}
// CommandPortsAdd adds proxy port mappings to an app
func CommandPortsAdd(appName string, portMaps []string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
if len(portMaps) == 0 {
return errors.New("No port mapping specified")
}
proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
if err != nil {
return err
}
if err := addProxyPorts(appName, proxyPortMap); err != nil {
return err
}
return common.PlugnTrigger("post-proxy-ports-update", []string{appName, "add"}...)
}
// CommandPortsClear clears all proxy port mappings for an app
func CommandPortsClear(appName string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
keys := []string{"DOKKU_PROXY_PORT_MAP"}
if err := config.UnsetMany(appName, keys, false); err != nil {
return err
}
return common.PlugnTrigger("post-proxy-ports-update", []string{appName, "clear"}...)
}
// CommandPortsRemove removes specific proxy port mappings from an app
func CommandPortsRemove(appName string, portMaps []string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
if len(portMaps) == 0 {
return errors.New("No port mapping specified")
}
proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
if err != nil {
return err
}
if err := removeProxyPorts(appName, proxyPortMap); err != nil {
return err
}
return common.PlugnTrigger("post-proxy-ports-update", []string{appName, "remove"}...)
}
// CommandPortsSet sets proxy port mappings for an app
func CommandPortsSet(appName string, portMaps []string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
if len(portMaps) == 0 {
return errors.New("No port mapping specified")
}
proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
if err != nil {
return err
}
if err := setProxyPorts(appName, proxyPortMap); err != nil {
return err
}
return common.PlugnTrigger("post-proxy-ports-update", []string{appName, "set"}...)
}
// CommandReport displays a proxy report for one or more apps
func CommandReport(appName string, format string, infoFlag string) error {
if len(appName) == 0 {

View File

@@ -2,8 +2,6 @@ package proxy
import (
"fmt"
"github.com/dokku/dokku/plugins/config"
)
// TriggerProxyIsEnabled prints true or false depending on whether the proxy is enabled
@@ -24,69 +22,3 @@ func TriggerProxyType(appName string) error {
return nil
}
// TriggerPostCertsRemove unsets port config vars after SSL cert is added
func TriggerPostCertsRemove(appName string) error {
keys := []string{"DOKKU_PROXY_SSL_PORT"}
if err := config.UnsetMany(appName, keys, false); err != nil {
return err
}
return removeProxyPorts(appName, filterAppProxyPorts(appName, "https", 443))
}
// TriggerPostCertsUpdate sets port config vars after SSL cert is added
func TriggerPostCertsUpdate(appName string) error {
port := config.GetWithDefault(appName, "DOKKU_PROXY_PORT", "")
sslPort := config.GetWithDefault(appName, "DOKKU_PROXY_SSL_PORT", "")
proxyPortMap := getProxyPortMap(appName)
toUnset := []string{}
if port == "80" {
toUnset = append(toUnset, "DOKKU_PROXY_PORT")
}
if sslPort == "443" {
toUnset = append(toUnset, "DOKKU_PROXY_SSL_PORT")
}
if len(toUnset) > 0 {
if err := config.UnsetMany(appName, toUnset, false); err != nil {
return err
}
}
var http80Ports []PortMap
for _, portMap := range proxyPortMap {
if portMap.Scheme == "http" && portMap.HostPort == 80 {
http80Ports = append(http80Ports, portMap)
}
}
if len(http80Ports) > 0 {
var https443Ports []PortMap
for _, portMap := range proxyPortMap {
if portMap.Scheme == "https" && portMap.HostPort == 443 {
https443Ports = append(https443Ports, portMap)
}
}
if err := removeProxyPorts(appName, https443Ports); err != nil {
return err
}
var toAdd []PortMap
for _, portMap := range http80Ports {
toAdd = append(toAdd, PortMap{
Scheme: "https",
HostPort: 443,
ContainerPort: portMap.ContainerPort,
})
}
if err := addProxyPorts(appName, toAdd); err != nil {
return err
}
}
return nil
}