From acaae9353b8e0cd9b08399f19fa37170443d31d8 Mon Sep 17 00:00:00 2001 From: HTErik <89977373+hterik@users.noreply.github.com> Date: Thu, 23 Sep 2021 18:03:40 +0200 Subject: [PATCH] Allow empty_dir volume shared across containers in container group (#13374) * Allow empty_dir volume to be shared across containers in container group. fixes hashicorp/terraform-provider-azurerm#11444 * Add acctest for emptydir volume shared across containers * Fix formatting --- .../containers/container_group_resource.go | 13 +++- .../container_group_resource_test.go | 75 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/internal/services/containers/container_group_resource.go b/internal/services/containers/container_group_resource.go index baad04d18c1b..2a1005ee3649 100644 --- a/internal/services/containers/container_group_resource.go +++ b/internal/services/containers/container_group_resource.go @@ -867,6 +867,7 @@ func expandContainerGroupContainers(d *pluginsdk.ResourceData) (*[]containerinst containerInstancePorts := make([]containerinstance.Port, 0) containerGroupPorts := make([]containerinstance.Port, 0) containerGroupVolumes := make([]containerinstance.Volume, 0) + addedEmptyDirs := map[string]bool{} for _, containerConfig := range containersConfig { data := containerConfig.(map[string]interface{}) @@ -964,7 +965,17 @@ func expandContainerGroupContainers(d *pluginsdk.ResourceData) (*[]containerinst } container.VolumeMounts = volumeMounts if containerGroupVolumesPartial != nil { - containerGroupVolumes = append(containerGroupVolumes, *containerGroupVolumesPartial...) + for _, cgVol := range *containerGroupVolumesPartial { + if cgVol.EmptyDir != nil { + if addedEmptyDirs[*cgVol.Name] { + // empty_dir-volumes are allowed to overlap across containers, in fact that is their primary purpose, + // but the containerGroup must not declare same name of such volumes twice. + continue + } + addedEmptyDirs[*cgVol.Name] = true + } + containerGroupVolumes = append(containerGroupVolumes, cgVol) + } } } diff --git a/internal/services/containers/container_group_resource_test.go b/internal/services/containers/container_group_resource_test.go index 69aaee4673e3..21d6b0a211d1 100644 --- a/internal/services/containers/container_group_resource_test.go +++ b/internal/services/containers/container_group_resource_test.go @@ -491,6 +491,21 @@ func TestAccContainerGroup_emptyDirVolume(t *testing.T) { }) } +func TestAccContainerGroup_emptyDirVolumeShared(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_container_group", "test") + r := ContainerGroupResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.emptyDirVolumeShared(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccContainerGroup_secretVolume(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_container_group", "test") r := ContainerGroupResource{} @@ -1553,6 +1568,66 @@ resource "azurerm_container_group" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } +func (ContainerGroupResource) emptyDirVolumeShared(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_container_group" "test" { + name = "acctestcontainergroupemptyshared-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + ip_address_type = "public" + dns_name_label = "acctestcontainergroup-%d" + os_type = "Linux" + restart_policy = "Never" + + container { + name = "writer" + image = "ubuntu:20.04" + cpu = "1" + memory = "1.5" + commands = ["touch", "/sharedempty/file.txt"] + + # Dummy port not used, workaround for https://github.com/hashicorp/terraform-provider-azurerm/issues/1697 + ports { + port = 80 + protocol = "TCP" + } + + volume { + name = "logs" + mount_path = "/sharedempty" + read_only = false + empty_dir = true + } + } + + container { + name = "reader" + image = "ubuntu:20.04" + cpu = "1" + memory = "1.5" + + volume { + name = "logs" + mount_path = "/sharedempty" + read_only = false + empty_dir = true + } + + commands = ["/bin/bash", "-c", "timeout 30 watch --interval 1 --errexit \"! cat /sharedempty/file.txt\""] + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + func (ContainerGroupResource) secretVolume(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" {