From d94f22bee2a34c198bc436a0592aabcd15c65ae5 Mon Sep 17 00:00:00 2001 From: Yorick Gersie <6005868+ygersie@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:46:45 +0100 Subject: [PATCH] Ensure infra_image gets proper label used for reconciliation (#15898) * Ensure infra_image gets proper label used for reconciliation Currently infra containers are not cleaned up as part of the dangling container cleanup routine. The reason is that Nomad checks if a container is a Nomad owned container by verifying the existence of the: `com.hashicorp.nomad.alloc_id` label. Ensure we set this label on the infra container as well. * fix unit test * changelog: add entry --------- Co-authored-by: Seth Hoenig --- .changelog/15898.txt | 3 +++ drivers/docker/network.go | 4 +++- drivers/docker/network_test.go | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changelog/15898.txt diff --git a/.changelog/15898.txt b/.changelog/15898.txt new file mode 100644 index 000000000..64d96190d --- /dev/null +++ b/.changelog/15898.txt @@ -0,0 +1,3 @@ +```release-note:bug +docker: Fixed a bug where infra_image did not get alloc_id label +``` diff --git a/drivers/docker/network.go b/drivers/docker/network.go index 71a9ab512..1cc57db77 100644 --- a/drivers/docker/network.go +++ b/drivers/docker/network.go @@ -119,12 +119,14 @@ func (d *Driver) DestroyNetwork(allocID string, spec *drivers.NetworkIsolationSp // createSandboxContainerConfig creates a docker container configuration which // starts a container with an empty network namespace. func (d *Driver) createSandboxContainerConfig(allocID string, createSpec *drivers.NetworkCreateRequest) (*docker.CreateContainerOptions, error) { - return &docker.CreateContainerOptions{ Name: fmt.Sprintf("nomad_init_%s", allocID), Config: &docker.Config{ Image: d.config.InfraImage, Hostname: createSpec.Hostname, + Labels: map[string]string{ + dockerLabelAllocID: allocID, + }, }, HostConfig: &docker.HostConfig{ // Set the network mode to none which creates a network namespace diff --git a/drivers/docker/network_test.go b/drivers/docker/network_test.go index 80c235cf8..8d53306f6 100644 --- a/drivers/docker/network_test.go +++ b/drivers/docker/network_test.go @@ -26,6 +26,9 @@ func TestDriver_createSandboxContainerConfig(t *testing.T) { Name: "nomad_init_768b5e8c-a52e-825c-d564-51100230eb62", Config: &docker.Config{ Image: "gcr.io/google_containers/pause-amd64:3.1", + Labels: map[string]string{ + dockerLabelAllocID: "768b5e8c-a52e-825c-d564-51100230eb62", + }, }, HostConfig: &docker.HostConfig{ NetworkMode: "none", @@ -44,6 +47,9 @@ func TestDriver_createSandboxContainerConfig(t *testing.T) { Config: &docker.Config{ Image: "gcr.io/google_containers/pause-amd64:3.1", Hostname: "linux", + Labels: map[string]string{ + dockerLabelAllocID: "768b5e8c-a52e-825c-d564-51100230eb62", + }, }, HostConfig: &docker.HostConfig{ NetworkMode: "none",