From 594984d35faba191955c1a1022ec4f8fb5febf3d Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 26 Jul 2022 11:56:54 -0700 Subject: [PATCH] Added handling of arm64 images (#6325) (#4533) Signed-off-by: Modular Magician --- .changelog/6325.txt | 3 + google-beta/image.go | 2 +- google-beta/resource_compute_disk.go | 19 +++-- google-beta/resource_compute_disk_test.go | 86 ++++++++++++++++++++++- 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 .changelog/6325.txt diff --git a/.changelog/6325.txt b/.changelog/6325.txt new file mode 100644 index 0000000000..6ba39de5f7 --- /dev/null +++ b/.changelog/6325.txt @@ -0,0 +1,3 @@ +```release-note:bug +compute: fixed perma-diff on `google_compute_disk` for new arm64 images +``` diff --git a/google-beta/image.go b/google-beta/image.go index 11ec84a8e3..e60e5b19b7 100644 --- a/google-beta/image.go +++ b/google-beta/image.go @@ -26,7 +26,7 @@ var ( resolveImageLink = regexp.MustCompile(fmt.Sprintf("^https://www.googleapis.com/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)", ProjectRegex, resolveImageImageRegex)) windowsSqlImage = regexp.MustCompile("^sql-(?:server-)?([0-9]{4})-([a-z]+)-windows-(?:server-)?([0-9]{4})(?:-r([0-9]+))?-dc-v[0-9]+$") - canonicalUbuntuLtsImage = regexp.MustCompile("^ubuntu-(minimal-)?([0-9]+)-") + canonicalUbuntuLtsImage = regexp.MustCompile("^ubuntu-(minimal-)?([0-9]+)(?:.*(arm64))?.*$") cosLtsImage = regexp.MustCompile("^cos-([0-9]+)-") ) diff --git a/google-beta/resource_compute_disk.go b/google-beta/resource_compute_disk.go index 0695c4e2d9..75ccdd6c66 100644 --- a/google-beta/resource_compute_disk.go +++ b/google-beta/resource_compute_disk.go @@ -141,8 +141,14 @@ func diskImageEquals(oldImageName, newImageName string) bool { func diskImageFamilyEquals(imageName, familyName string) bool { // Handles the case when the image name includes the family name // e.g. image name: debian-9-drawfork-v20180109, family name: debian-9 - if strings.Contains(imageName, familyName) { - return true + // We have to check for arm64 because of cases like: + // image name: opensuse-leap-15-4-v20220713-arm64, family name: opensuse-leap (should not suppress) + if strings.Contains(imageName, strings.TrimSuffix(familyName, "-arm64")) { + if strings.Contains(imageName, "-arm64") { + return strings.HasSuffix(familyName, "-arm64") + } else { + return !strings.HasSuffix(familyName, "-arm64") + } } if suppressCanonicalFamilyDiff(imageName, familyName) { @@ -167,8 +173,13 @@ func diskImageFamilyEquals(imageName, familyName string) bool { // e.g. image: ubuntu-1404-trusty-v20180122, family: ubuntu-1404-lts func suppressCanonicalFamilyDiff(imageName, familyName string) bool { parts := canonicalUbuntuLtsImage.FindStringSubmatch(imageName) - if len(parts) == 3 { - f := fmt.Sprintf("ubuntu-%s%s-lts", parts[1], parts[2]) + if len(parts) == 4 { + var f string + if parts[3] == "" { + f = fmt.Sprintf("ubuntu-%s%s-lts", parts[1], parts[2]) + } else { + f = fmt.Sprintf("ubuntu-%s%s-lts-%s", parts[1], parts[2], parts[3]) + } if f == familyName { return true } diff --git a/google-beta/resource_compute_disk_test.go b/google-beta/resource_compute_disk_test.go index 525bf78969..0f4df1e565 100644 --- a/google-beta/resource_compute_disk_test.go +++ b/google-beta/resource_compute_disk_test.go @@ -171,12 +171,92 @@ func TestDiskImageDiffSuppress(t *testing.T) { New: "different-cloud/debian-8", ExpectDiffSuppress: false, }, + // arm images + "matching image opensuse arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/opensuse-cloud/global/images/opensuse-leap-15-4-v20220713-arm64", + New: "opensuse-leap-arm64", + ExpectDiffSuppress: true, + }, + "matching image sles arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/suse-cloud/global/images/sles-15-sp4-v20220713-arm64", + New: "sles-15-arm64", + ExpectDiffSuppress: true, + }, + "matching image ubuntu arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-arm64-v20220712", + New: "ubuntu-1804-lts-arm64", + ExpectDiffSuppress: true, + }, + "matching image ubuntu-minimal arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-minimal-2004-focal-arm64-v20220713", + New: "ubuntu-minimal-2004-lts-arm64", + ExpectDiffSuppress: true, + }, + "matching image debian arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-11-bullseye-arm64-v20220719", + New: "debian-11-arm64", + ExpectDiffSuppress: true, + }, + "different architecture image opensuse arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/opensuse-cloud/global/images/opensuse-leap-15-4-v20220713-arm64", + New: "opensuse-leap", + ExpectDiffSuppress: false, + }, + "different architecture image sles arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/suse-cloud/global/images/sles-15-sp4-v20220713-arm64", + New: "sles-15", + ExpectDiffSuppress: false, + }, + "different architecture image ubuntu arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-arm64-v20220712", + New: "ubuntu-1804-lts", + ExpectDiffSuppress: false, + }, + "different architecture image ubuntu-minimal arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-minimal-2004-focal-arm64-v20220713", + New: "ubuntu-minimal-2004-lts", + ExpectDiffSuppress: false, + }, + "different architecture image debian arm64 self_link": { + Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-11-bullseye-arm64-v20220719", + New: "debian-11", + ExpectDiffSuppress: false, + }, + "different architecture image opensuse arm64 family": { + Old: "https://www.googleapis.com/compute/v1/projects/opensuse-cloud/global/images/opensuse-leap-15-2-v20200702", + New: "opensuse-leap-arm64", + ExpectDiffSuppress: false, + }, + "different architecture image sles arm64 family": { + Old: "https://www.googleapis.com/compute/v1/projects/suse-cloud/global/images/sles-15-sp4-v20220722-x86-64", + New: "sles-15-arm64", + ExpectDiffSuppress: false, + }, + "different architecture image ubuntu arm64 family": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20220712", + New: "ubuntu-1804-lts-arm64", + ExpectDiffSuppress: false, + }, + "different architecture image ubuntu-minimal arm64 family": { + Old: "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-minimal-2004-focal-v20220713", + New: "ubuntu-minimal-2004-lts-arm64", + ExpectDiffSuppress: false, + }, + "different architecture image debian arm64 family": { + Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-11-bullseye-v20220719", + New: "debian-11-arm64", + ExpectDiffSuppress: false, + }, } for tn, tc := range cases { - if diskImageDiffSuppress("image", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress { - t.Errorf("bad: %s, %q => %q expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress) - } + tc := tc + t.Run(tn, func(t *testing.T) { + t.Parallel() + if diskImageDiffSuppress("image", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress { + t.Fatalf("%q => %q expect DiffSuppress to return %t", tc.Old, tc.New, tc.ExpectDiffSuppress) + } + }) } }