Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in asoctl #3862

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@ func FindGroupKindForResourceType(t string) (schema.GroupKind, bool) {
resourceTypeGK = createTypeToGKMap()
})

t = canonicalizeAzureTypeString(t)
gk, ok := resourceTypeGK[t]
return gk, ok
}

func canonicalizeAzureTypeString(t string) string {
// ToLower the type, as Azure type names are not case-sensitive
return strings.ToLower(t)
}

func createTypeToGKMap() map[string]schema.GroupKind {
result := make(map[string]schema.GroupKind)
scheme := api.CreateScheme()
Expand All @@ -43,7 +51,8 @@ func createTypeToGKMap() map[string]schema.GroupKind {
continue
}

result[rsrc.GetType()] = gvk.GroupKind()
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
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
Loading