Skip to content

Commit

Permalink
Fix panic in asoctl
Browse files Browse the repository at this point in the history
Caused by case-mismatch in Azure reported resource type.
  • Loading branch information
matthchr committed Mar 12, 2024
1 parent 8a8198c commit 1c1fcfd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
13 changes: 11 additions & 2 deletions v2/cmd/asoctl/internal/importing/find_group_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions v2/cmd/asoctl/internal/importing/importable_arm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions v2/cmd/asoctl/internal/importing/importable_arm_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 1c1fcfd

Please sign in to comment.