-
-
Notifications
You must be signed in to change notification settings - Fork 580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: internal project network with duplicate check, revert for #5305 #5533
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ type ComposeCmdOpts struct { | |
} | ||
|
||
// EnsureNetwork will ensure the Docker network for DDEV is created. | ||
func EnsureNetwork(client *docker.Client, name string) error { | ||
func EnsureNetwork(client *docker.Client, name string, labels map[string]string) error { | ||
// Pre-check for network duplicates | ||
RemoveNetworkDuplicates(client, name) | ||
|
||
|
@@ -50,7 +50,7 @@ func EnsureNetwork(client *docker.Client, name string) error { | |
Name: name, | ||
Driver: "bridge", | ||
Internal: false, | ||
Labels: map[string]string{"com.ddev.platform": "ddev"}, | ||
Labels: labels, | ||
} | ||
_, err := client.CreateNetwork(netOptions) | ||
if err != nil { | ||
|
@@ -61,24 +61,36 @@ func EnsureNetwork(client *docker.Client, name string) error { | |
return nil | ||
} | ||
|
||
// EnsureNetworkWithFatal creates or ensures the provided network exists or | ||
// EnsureDdevNetwork creates or ensures the DDEV network exists or | ||
// exits with fatal. | ||
func EnsureNetworkWithFatal(name string) { | ||
func EnsureDdevNetwork() { | ||
// Ensure we have the fallback global DDEV network | ||
client := GetDockerClient() | ||
err := EnsureNetwork(client, name) | ||
err := EnsureNetwork(client, NetName, map[string]string{"com.ddev.platform": "ddev"}) | ||
if err != nil { | ||
log.Fatalf("Failed to ensure Docker network %s: %v", name, err) | ||
log.Fatalf("Failed to ensure Docker network %s: %v", NetName, err) | ||
} | ||
} | ||
|
||
// EnsureDdevNetwork creates or ensures the DDEV network exists or | ||
// EnsureProjectNetwork creates or ensures the project network exists or | ||
// exits with fatal. | ||
func EnsureDdevNetwork() { | ||
// Ensure we have the fallback global DDEV network | ||
EnsureNetworkWithFatal(NetName) | ||
// Ensure we have the current project network | ||
if os.Getenv("COMPOSE_PROJECT_NAME") != "" { | ||
EnsureNetworkWithFatal(os.Getenv("COMPOSE_PROJECT_NAME") + "_default") | ||
func EnsureProjectNetwork() { | ||
if os.Getenv("COMPOSE_PROJECT_NAME") == "" { | ||
log.Fatalf("dockerutil.EnsureProjectNetwork() must be called after app.DockerEnv()") | ||
} | ||
networkName := os.Getenv("COMPOSE_PROJECT_NAME") + "_default" | ||
client := GetDockerClient() | ||
err := EnsureNetwork(client, networkName, map[string]string{ | ||
"com.ddev.platform": "ddev", | ||
// add docker-compose labels needed for "docker compose up" | ||
"com.docker.compose.network": "default", | ||
"com.docker.compose.project": os.Getenv("COMPOSE_PROJECT_NAME"), | ||
"com.docker.compose.version": func() string { | ||
version, _ := GetDockerComposeVersion() | ||
return strings.TrimPrefix(version, "v") | ||
}()}) | ||
if err != nil { | ||
log.Fatalf("Failed to ensure Docker network %s: %v", networkName, err) | ||
} | ||
} | ||
|
||
|
@@ -95,7 +107,8 @@ func NetworkExists(netName string) bool { | |
func RemoveNetwork(netName string) error { | ||
client := GetDockerClient() | ||
networks, _ := client.ListNetworks() | ||
var err error | ||
// the loop below may not contain such a network | ||
var err error = &docker.NoSuchNetwork{ID: netName} | ||
Comment on lines
+110
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a fix for a bug that I introduced in #5508 When you run |
||
// loop through all networks because there may be duplicates | ||
// and delete only by ID - it's unique, but the name isn't | ||
for _, network := range networks { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,12 +262,8 @@ func ClearDockerEnv() { | |
|
||
// ContainerCheck determines if a given container name exists and matches a given state | ||
func ContainerCheck(checkName string, checkState string) (bool, error) { | ||
// Ensure we have Docker network | ||
client := dockerutil.GetDockerClient() | ||
err := dockerutil.EnsureNetwork(client, dockerutil.NetName) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Ensure we have DDEV network | ||
dockerutil.EnsureDdevNetwork() | ||
Comment on lines
+265
to
+266
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is only refactoring. |
||
|
||
c, err := dockerutil.FindContainerByName(checkName) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is easy to revert, you only need to remove this function call and the function itself.