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/create subcommands/destroy subcommands/exists subcommands/info subcommands/list subcommands/rebuild subcommands/rebuildall subcommands/report subcommands/set
TRIGGERS = triggers/core-post-deploy triggers/docker-args-process-build triggers/docker-args-process-deploy triggers/docker-args-process-run triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-get-static-listeners triggers/network-write-ipaddr triggers/network-write-port triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-container-create triggers/post-create triggers/post-delete triggers/report
TRIGGERS = triggers/core-post-deploy triggers/docker-args-process-build triggers/docker-args-process-deploy triggers/docker-args-process-run triggers/install triggers/network-build-config triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-property triggers/network-get-static-listeners triggers/network-write-ipaddr triggers/network-write-port triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-container-create triggers/post-create triggers/post-delete triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = network

View File

@@ -2,7 +2,6 @@ package network
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
@@ -10,9 +9,6 @@ import (
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
sh "github.com/codeskyblue/go-sh"
)
var (
@@ -65,8 +61,6 @@ func BuildConfig(appName string) error {
return nil
}
image := common.GetAppImageName(appName, "", "")
isHerokuishContainer := common.IsImageHerokuishBased(image, appName)
common.LogInfo1(fmt.Sprintf("Ensuring network configuration is in sync for %s", appName))
for processType, procCount := range scale {
@@ -82,8 +76,6 @@ func BuildConfig(appName string) error {
}
ipAddress := GetContainerIpaddress(appName, processType, containerID)
port := GetContainerPort(appName, processType, containerID, isHerokuishContainer)
if ipAddress != "" {
args := []string{appName, processType, containerIndexString, ipAddress}
_, err := common.PlugnTriggerOutput("network-write-ipaddr", args...)
@@ -91,14 +83,6 @@ func BuildConfig(appName string) error {
common.LogWarn(err.Error())
}
}
if port != "" {
args := []string{appName, processType, containerIndexString, port}
_, err := common.PlugnTriggerOutput("network-write-port", args...)
if err != nil {
common.LogWarn(err.Error())
}
}
}
}
@@ -147,53 +131,6 @@ func GetContainerIpaddress(appName, processType, containerID string) (ipAddr str
return
}
// GetContainerPort returns the port for a given app container
func GetContainerPort(appName, processType string, containerID string, isHerokuishContainer bool) (port string) {
if processType == "web" {
if staticWebListener := reportStaticWebListener(appName); staticWebListener != "" {
_, port, err := net.SplitHostPort(staticWebListener)
if err == nil {
return port
}
return "80"
}
}
dockerfilePorts := make([]string, 0)
if !isHerokuishContainer {
configValue := config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", "")
if configValue != "" {
dockerfilePorts = strings.Split(configValue, " ")
}
}
if len(dockerfilePorts) > 0 {
for _, p := range dockerfilePorts {
if strings.HasSuffix(p, "/udp") {
continue
}
port = strings.TrimSuffix(p, "/tcp")
if port != "" {
break
}
}
if port != "" {
cmd := sh.Command(common.DockerBin(), "container", "port", containerID, port)
cmd.Stderr = ioutil.Discard
b, err := cmd.Output()
if err == nil {
port = strings.Split(string(b[:]), ":")[1]
}
}
} else {
port = "5000"
}
return
}
// GetListeners returns a string array of app listeners
func GetListeners(appName string, processType string) []string {
if processType == "web" {
@@ -214,6 +151,9 @@ func GetListeners(appName string, processType string) []string {
portfile := strings.Replace(ipfile, ipPrefix, portPrefix, 1)
ipAddress := common.ReadFirstLine(ipfile)
port := common.ReadFirstLine(portfile)
if port == "" {
port = "5000"
}
listeners = append(listeners, fmt.Sprintf("%s:%s", ipAddress, port))
}
return listeners

View File

@@ -32,11 +32,6 @@ func main() {
case "network-build-config":
appName := flag.Arg(0)
err = network.BuildConfig(appName)
case "network-compute-ports":
appName := flag.Arg(0)
processType := flag.Arg(1)
isHerokuishContainer := common.ToBool(flag.Arg(2))
err = network.TriggerNetworkComputePorts(appName, processType, isHerokuishContainer)
case "network-config-exists":
appName := flag.Arg(0)
err = network.TriggerNetworkConfigExists(appName)
@@ -49,12 +44,6 @@ func main() {
appName := flag.Arg(0)
processType := flag.Arg(1)
err = network.TriggerNetworkGetListeners(appName, processType)
case "network-get-port":
appName := flag.Arg(0)
processType := flag.Arg(1)
containerID := flag.Arg(2)
isHerokuishContainer := common.ToBool(flag.Arg(3))
err = network.TriggerNetworkGetPort(appName, processType, containerID, isHerokuishContainer)
case "network-get-property":
appName := flag.Arg(0)
property := flag.Arg(1)

View File

@@ -5,10 +5,8 @@ import (
"io/ioutil"
"os"
"strings"
"unicode/utf8"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
)
// TriggerDockerArgsProcess outputs the network plugin docker options for an app
@@ -58,33 +56,6 @@ func TriggerInstall() error {
return nil
}
// TriggerNetworkComputePorts computes the ports for a given app container
func TriggerNetworkComputePorts(appName string, processType string, isHerokuishContainer bool) error {
var dockerfilePorts []string
if !isHerokuishContainer {
dokkuDockerfilePorts := strings.Trim(config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", ""), " ")
if utf8.RuneCountInString(dokkuDockerfilePorts) > 0 {
dockerfilePorts = strings.Split(dokkuDockerfilePorts, " ")
}
}
var ports []string
if len(dockerfilePorts) == 0 {
ports = append(ports, "5000")
} else {
for _, port := range dockerfilePorts {
port = strings.TrimSuffix(strings.TrimSpace(port), "/tcp")
if port == "" || strings.HasSuffix(port, "/udp") {
continue
}
ports = append(ports, port)
}
}
fmt.Println(strings.Join(ports, " "))
return nil
}
// TriggerNetworkConfigExists writes true or false to stdout whether a given app has network config
func TriggerNetworkConfigExists(appName string) error {
if HasNetworkConfig(appName) {
@@ -114,13 +85,6 @@ func TriggerNetworkGetListeners(appName string, processType string) error {
return nil
}
// TriggerNetworkGetPort writes the port to stdout for a given app container
func TriggerNetworkGetPort(appName string, processType string, containerID string, isHerokuishContainer bool) error {
port := GetContainerPort(appName, processType, containerID, isHerokuishContainer)
fmt.Println(port)
return nil
}
// TriggerNetworkGetProperty writes the network property to stdout for a given app container
func TriggerNetworkGetProperty(appName string, property string) error {
computedValueMap := map[string]common.ReportFunc{