MCPcopy Index your code
hub / github.com/ddev/ddev / StartDdevRouter

Function StartDdevRouter

pkg/ddevapp/router.go:76–213  ·  view source on GitHub ↗

StartDdevRouter ensures the router is running.

()

Source from the content-addressed store, hash-verified

74
75// StartDdevRouter ensures the router is running.
76func StartDdevRouter() error {
77 // Kill the router if not running or if its image is outdated, so it restarts fresh.
78 // Use HasSuffix to handle registry prefixes (e.g. docker.io/) that Podman includes in image names.
79 router, err := FindDdevRouter()
80 if router != nil && err == nil && (router.State != "running" || !strings.HasSuffix(router.Image, ddevImages.GetRouterImage())) {
81 err = dockerutil.RemoveContainer(nodeps.RouterContainer)
82 if err != nil {
83 return err
84 }
85 router = nil
86 }
87
88 activeApps := GetActiveProjects()
89
90 // Check if router needs to be recreated due to port or hostname changes
91 needsRecreation := false
92 if router != nil && err == nil && router.State == "running" {
93 // Router is running, check if ports or hostnames have changed
94 var portsChanged, hostnamesChanged bool
95
96 // Check ports
97 existingPorts, err := dockerutil.GetBoundHostPorts(router.ID)
98 if err != nil {
99 util.Debug("Error getting bound ports, will recreate router: %v", err)
100 needsRecreation = true
101 } else {
102 neededPorts := determineRouterPorts(activeApps)
103 // Add the Traefik monitor port to the needed list for comparison
104 // (it's always bound by the router but not returned by determineRouterPorts
105 // since it's added separately in the static config template)
106 neededPorts = append(neededPorts, globalconfig.DdevGlobalConfig.TraefikMonitorPort)
107 portsChanged = !PortsMatch(existingPorts, neededPorts)
108 util.Debug("Router port comparison: existing=%v needed=%v changed=%v", existingPorts, neededPorts, portsChanged)
109 }
110
111 // Check hostnames (network aliases)
112 if !needsRecreation {
113 existingHostnames, err := dockerutil.GetRouterNetworkAliases(router.ID)
114 if err != nil {
115 util.Debug("Error getting network aliases, will recreate router: %v", err)
116 needsRecreation = true
117 } else {
118 neededHostnames := determineRouterHostnames(activeApps)
119 hostnamesChanged = !HostnamesMatch(existingHostnames, neededHostnames)
120 util.Debug("Router hostname comparison: existing=%v needed=%v changed=%v", existingHostnames, neededHostnames, hostnamesChanged)
121 }
122 }
123
124 // Determine if recreation is needed
125 if !needsRecreation {
126 if portsChanged || hostnamesChanged {
127 if portsChanged && hostnamesChanged {
128 util.Debug("Router ports and hostnames have changed, will recreate router")
129 } else if portsChanged {
130 util.Debug("Router ports have changed, will recreate router")
131 } else {
132 util.Debug("Router hostnames have changed, will recreate router")
133 }

Callers 3

debug-rebuild.goFile · 0.92
StartMethod · 0.85
StartOptionalProfilesMethod · 0.85

Calls 15

RemoveContainerFunction · 0.92
GetBoundHostPortsFunction · 0.92
DebugFunction · 0.92
GetRouterNetworkAliasesFunction · 0.92
ComposeCmdFunction · 0.92
StartWaitFunction · 0.92
ContainerWaitFunction · 0.92
WarningFunction · 0.92
FindDdevRouterFunction · 0.85
GetActiveProjectsFunction · 0.85
determineRouterPortsFunction · 0.85
PortsMatchFunction · 0.85

Tested by

no test coverage detected