refactor: only use detected port mapping if override is not specified

Previously, we would always set the port mapping during a dockerfile build, making it difficult for users to override mappings. We also only _sometimes_ updated the detected port mapping, further confusing issues when users were migrating from Dockerfile to Buildpacks for builds.

Now, we always detect the port mapping during the build process, and only use that detected port mapping if an override is not specified. This greatly simplifies the experience around port mapping, as now a user can create an app, set a port mapping, and that first deploy will respect the port mapping without an additional deploy.

The builder always has the best context for what the app should be listening on, and thus we can always specify a "default" port mapping at this stage. Users can override this map as desired later.

This change also results in the removal of a ton of internal code that is now centralized in the ports plugin.

Closes #4067
This commit is contained in:
Jose Diaz-Gonzalez
2023-07-11 21:06:40 -04:00
parent 6168b0c71c
commit f8ccf52079
36 changed files with 238 additions and 416 deletions

View File

@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/list subcommands/add subcommands/clear subcommands/remove subcommands/set subcommands/report
TRIGGERS = triggers/install triggers/ports-clear triggers/ports-configure triggers/ports-dockerfile-raw-tcp-ports triggers/ports-get triggers/ports-get-available triggers/ports-set-detected triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-certs-remove triggers/post-certs-update triggers/post-delete triggers/report
TRIGGERS = triggers/install triggers/ports-clear triggers/ports-configure triggers/ports-get triggers/ports-get-available triggers/ports-get-property triggers/ports-set-detected triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-certs-remove triggers/post-certs-update triggers/post-delete triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = ports

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
@@ -23,7 +22,11 @@ func addPortMaps(appName string, portMaps []PortMap) error {
}
func clearPorts(appName string) error {
return common.PropertyDelete("ports", appName, "map")
if err := common.PropertyDelete("ports", appName, "map"); err != nil {
return err
}
return common.PropertyDelete("ports", appName, "map-detected")
}
func doesCertExist(appName string) bool {
@@ -63,11 +66,33 @@ func getAvailablePort() int {
}
}
func getComputedProxyPort(appName string) int {
port := getProxyPort(appName)
if port == 0 {
port = getGlobalProxyPort()
}
return port
}
func getComputedProxySSLPort(appName string) int {
port := getProxySSLPort(appName)
if port == 0 {
port = getGlobalProxySSLPort()
}
return port
}
func getDetectedPortMaps(appName string) []PortMap {
basePort := getComputedProxyPort(appName)
if basePort == 0 {
basePort = 80
}
defaultMapping := []PortMap{
{
ContainerPort: 5000,
HostPort: 80,
HostPort: basePort,
Scheme: "http",
},
}
@@ -84,6 +109,11 @@ func getDetectedPortMaps(appName string) []PortMap {
if doesCertExist(appName) {
setSSLPort := false
baseSSLPort := getComputedProxySSLPort(appName)
if baseSSLPort == 0 {
baseSSLPort = 443
}
for _, portMap := range portMaps {
if portMap.Scheme != "http" || portMap.HostPort != 80 {
continue
@@ -92,7 +122,7 @@ func getDetectedPortMaps(appName string) []PortMap {
setSSLPort = true
portMaps = append(portMaps, PortMap{
ContainerPort: portMap.ContainerPort,
HostPort: 443,
HostPort: baseSSLPort,
Scheme: "https",
})
}
@@ -111,37 +141,6 @@ func getDetectedPortMaps(appName string) []PortMap {
return portMaps
}
func getDockerfileRawTCPPorts(appName string) []int {
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_DOCKERFILE_PORTS"}...)
dockerfilePorts := strings.TrimSpace(string(b[:]))
ports := []int{}
for _, dockerfilePort := range strings.Split(dockerfilePorts, " ") {
dockerfilePort = strings.TrimSpace(dockerfilePort)
if strings.HasSuffix(dockerfilePort, "/udp") {
continue
}
dockerfilePort = strings.TrimSuffix(dockerfilePort, "/tcp")
if dockerfilePort == "" {
continue
}
port, err := strconv.Atoi(dockerfilePort)
if err != nil {
continue
}
if port == 0 {
continue
}
ports = append(ports, port)
}
return ports
}
func getGlobalProxyPort() int {
port := 0
b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_PROXY_PORT"}...)
@@ -192,21 +191,18 @@ func getProxySSLPort(appName string) int {
return port
}
func initializeProxyPort(appName string, rawTCPPorts []int) error {
func initializeProxyPort(appName string) error {
port := getProxyPort(appName)
if port != 0 {
return nil
}
if len(rawTCPPorts) > 0 {
return nil
}
if isAppVhostEnabled(appName) {
port = getGlobalProxyPort()
} else {
common.LogInfo1("No port set, setting to random open high port")
port = getAvailablePort()
common.LogInfo1(fmt.Sprintf("Random port %d", port))
}
if port == 0 {
@@ -219,7 +215,7 @@ func initializeProxyPort(appName string, rawTCPPorts []int) error {
return nil
}
func initializeProxySSLPort(appName string, rawTCPPorts []int) error {
func initializeProxySSLPort(appName string) error {
port := getProxySSLPort(appName)
if port != 0 {
return nil
@@ -234,7 +230,7 @@ func initializeProxySSLPort(appName string, rawTCPPorts []int) error {
port = 443
}
if len(rawTCPPorts) == 0 && !isAppVhostEnabled(appName) {
if !isAppVhostEnabled(appName) {
common.LogInfo1("No ssl port set, setting to random open high port")
port = getAvailablePort()
}
@@ -246,55 +242,6 @@ func initializeProxySSLPort(appName string, rawTCPPorts []int) error {
return nil
}
func initializePortMap(appName string, rawTCPPorts []int) error {
portMaps := getPortMaps(appName)
if len(portMaps) > 0 {
return nil
}
if len(rawTCPPorts) > 0 {
for _, rawTcpPort := range rawTCPPorts {
portMaps = append(portMaps, PortMap{
ContainerPort: rawTcpPort,
HostPort: rawTcpPort,
Scheme: "http",
})
}
} else {
upstreamPort := 5000
portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1")
if common.FileExists(portFile) {
if port, err := strconv.Atoi(common.ReadFirstLine(portFile)); err == nil {
upstreamPort = port
}
}
dokkuProxyPort := getProxyPort(appName)
dokkuProxySSLPort := getProxySSLPort(appName)
if dokkuProxyPort != 0 {
portMaps = append(portMaps, PortMap{
ContainerPort: upstreamPort,
HostPort: dokkuProxyPort,
Scheme: "http",
})
}
if dokkuProxySSLPort != 0 {
portMaps = append(portMaps, PortMap{
ContainerPort: upstreamPort,
HostPort: dokkuProxySSLPort,
Scheme: "https",
})
}
}
if len(portMaps) > 0 {
return setPortMaps(appName, portMaps)
}
return nil
}
func inRange(value int, min int, max int) bool {
return min < value && value < max
}

View File

@@ -26,14 +26,15 @@ func main() {
case "ports-configure":
appName := flag.Arg(0)
err = ports.TriggerPortsConfigure(appName)
case "ports-dockerfile-raw-tcp-ports":
appName := flag.Arg(0)
err = ports.TriggerPortsDockerfileRawTCPPorts(appName)
case "ports-get":
appName := flag.Arg(0)
err = ports.TriggerPortsGet(appName)
case "ports-get-available":
err = ports.TriggerPortsGetAvailable()
case "ports-get-property":
appName := flag.Arg(0)
property := flag.Arg(1)
err = ports.TriggerPortsGetProperty(appName, property)
case "ports-set-detected":
appName := flag.Arg(0)
appName, portMapString := common.ShiftString(flag.Args())

View File

@@ -52,36 +52,25 @@ func TriggerPortsClear(appName string) error {
// TriggerPortsConfigure ensures we have a port mapping
func TriggerPortsConfigure(appName string) error {
rawTCPPorts := getDockerfileRawTCPPorts(appName)
if err := initializeProxyPort(appName, rawTCPPorts); err != nil {
if err := initializeProxyPort(appName); err != nil {
return err
}
if err := initializeProxySSLPort(appName, rawTCPPorts); err != nil {
if err := initializeProxySSLPort(appName); err != nil {
return err
}
if err := initializePortMap(appName, rawTCPPorts); err != nil {
return err
}
return nil
}
// TriggerRawTCPPorts extracts raw tcp port numbers from DOCKERFILE_PORTS config variable
func TriggerPortsDockerfileRawTCPPorts(appName string) error {
ports := getDockerfileRawTCPPorts(appName)
for _, port := range ports {
common.Log(fmt.Sprint(port))
}
return nil
}
// TriggerPortsGet prints out the port mapping for a given app
func TriggerPortsGet(appName string) error {
for _, portMap := range getPortMaps(appName) {
portMaps := getPortMaps(appName)
if len(portMaps) == 0 {
portMaps = getDetectedPortMaps(appName)
}
for _, portMap := range portMaps {
if portMap.AllowsPersistence() {
continue
}
@@ -102,7 +91,23 @@ func TriggerPortsGetAvailable() error {
return nil
}
// TriggerPortsGetAvailable prints out an available port greater than 1024
// TriggerPortsGetProperty writes the ports key to stdout for a given app container
func TriggerPortsGetProperty(appName string, key string) error {
if key == "proxy-port" {
fmt.Println(getComputedProxyPort(appName))
return nil
}
if key == "proxy-ssl-port" {
fmt.Println(getComputedProxySSLPort(appName))
return nil
}
fmt.Println(common.PropertyGet("ports", appName, key))
return nil
}
// TriggerPortsSetDetected writes out detected ports
func TriggerPortsSetDetected(appName string, portMapString string) error {
portMaps, _ := parsePortMapString(portMapString)