From 1c1fcfd2cc6f2ddf2e799a79efb60db939a57fd4 Mon Sep 17 00:00:00 2001 From: Matthew Christopher Date: Mon, 11 Mar 2024 17:49:43 -0700 Subject: [PATCH] Fix panic in asoctl Caused by case-mismatch in Azure reported resource type. --- .../internal/importing/find_group_kind.go | 13 ++++++++-- .../importing/importable_arm_resource.go | 4 +-- .../importing/importable_arm_resource_test.go | 26 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/v2/cmd/asoctl/internal/importing/find_group_kind.go b/v2/cmd/asoctl/internal/importing/find_group_kind.go index ad73494e8a2..4603f38049c 100644 --- a/v2/cmd/asoctl/internal/importing/find_group_kind.go +++ b/v2/cmd/asoctl/internal/importing/find_group_kind.go @@ -6,11 +6,13 @@ package importing import ( + "strings" "sync" + "k8s.io/apimachinery/pkg/runtime/schema" + "github.com/Azure/azure-service-operator/v2/api" "github.com/Azure/azure-service-operator/v2/pkg/genruntime" - "k8s.io/apimachinery/pkg/runtime/schema" ) var ( @@ -23,10 +25,15 @@ func FindGroupKindForResourceType(t string) (schema.GroupKind, bool) { resourceTypeGK = createTypeToGKMap() }) + t = canonicalizeAzureTypeString(t) gk, ok := resourceTypeGK[t] return gk, ok } +func canonicalizeAzureTypeString(t string) string { + return strings.ToLower(t) +} + func createTypeToGKMap() map[string]schema.GroupKind { result := make(map[string]schema.GroupKind) scheme := api.CreateScheme() @@ -43,7 +50,9 @@ func createTypeToGKMap() map[string]schema.GroupKind { continue } - result[rsrc.GetType()] = gvk.GroupKind() + // ToLower the type, as Azure type names are not case-sensitive + typeStr := canonicalizeAzureTypeString(rsrc.GetType()) + result[typeStr] = gvk.GroupKind() } return result diff --git a/v2/cmd/asoctl/internal/importing/importable_arm_resource.go b/v2/cmd/asoctl/internal/importing/importable_arm_resource.go index d36c9f122db..ab3cba448c1 100644 --- a/v2/cmd/asoctl/internal/importing/importable_arm_resource.go +++ b/v2/cmd/asoctl/internal/importing/importable_arm_resource.go @@ -383,12 +383,12 @@ func (i *importableARMResource) createImportableObjectFromID( gvk, gvkErr := i.groupVersionKindFromID(armID) if gvkErr != nil { - return nil, errors.Wrap(err, "unable to determine GVK of resource") + return nil, errors.Wrap(gvkErr, "unable to determine GVK of resource") } obj, objErr := i.createBlankObjectFromGVK(gvk) if objErr != nil { - return nil, errors.Wrap(err, "unable to create blank resource") + return nil, errors.Wrap(objErr, "unable to create blank resource") } importable, ok := obj.(genruntime.ImportableARMResource) diff --git a/v2/cmd/asoctl/internal/importing/importable_arm_resource_test.go b/v2/cmd/asoctl/internal/importing/importable_arm_resource_test.go index 2f7182ee2bf..e1fcc13b6b2 100644 --- a/v2/cmd/asoctl/internal/importing/importable_arm_resource_test.go +++ b/v2/cmd/asoctl/internal/importing/importable_arm_resource_test.go @@ -53,6 +53,18 @@ func Test_ARMResourceImporter_GroupKindFromARMID(t *testing.T) { expectedGroup: "containerservice.azure.com", expectedKind: "ManagedCluster", }, + { + name: "Redis cache", + armId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/aso-rg/providers/Microsoft.Cache/redis/my-cache", + expectedGroup: "cache.azure.com", + expectedKind: "Redis", + }, + { + name: "Redis cache different case", + armId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/aso-rg/providers/Microsoft.Cache/REDIS/my-cache", + expectedGroup: "cache.azure.com", + expectedKind: "Redis", + }, //{ // "VMSS Instance", // "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/aso-rg/providers/Microsoft.Compute/virtualMachineScaleSets/aso-scaleset/virtualMachines/0", @@ -113,6 +125,20 @@ func Test_ARMResourceImporter_GroupVersionKindFromARMID(t *testing.T) { expectedKind: "ManagedCluster", expectedVersion: "v1api20231001", }, + { + name: "Redis cache", + armId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/aso-rg/providers/Microsoft.Cache/redis/my-cache", + expectedGroup: "cache.azure.com", + expectedKind: "Redis", + expectedVersion: "v1api20230401", + }, + { + name: "Redis cache different case", + armId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/aso-rg/providers/Microsoft.Cache/REDIS/my-cache", + expectedGroup: "cache.azure.com", + expectedKind: "Redis", + expectedVersion: "v1api20230401", + }, } factory := importableARMResource{