-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix uniqueness bug with RoleAssignment owned by ARM ID
Fix bug where RoleAssignment owned by ARM ID doesn't account for the ARM ID in the seed of the random UUID generate. This bugfix is BREAKING if the owner is using ARM ID and in the following cases: * User migrates RoleAssignment from one cluster to another. * User sets reconcile-policy: skip, deletes the RoleAssignment and then recreates it. In the above two cases, the new correct algorithm will consider the ARM ID of the owner and generate a different UUID than before. Other cases such as standard updates will not be impacted as Kubernetes sends the WHOLE object to the mutating webhook and for updates the object contains the (old) generated UUID.
- Loading branch information
Showing
6 changed files
with
173 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package randextensions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
"github.com/Azure/azure-service-operator/v2/internal/util/randextensions" | ||
"github.com/Azure/azure-service-operator/v2/pkg/genruntime" | ||
) | ||
|
||
func Test_MakeUniqueOwnerScopedString(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
ref *genruntime.ResourceReference | ||
objGK schema.GroupKind | ||
objNamespace string | ||
objName string | ||
expected string | ||
}{ | ||
{ | ||
name: "nil owner returns empty parent string", | ||
ref: nil, | ||
objGK: schema.GroupKind{Group: "resources.azure.com", Kind: "ResourceGroup"}, | ||
objNamespace: "default", | ||
objName: "myrg", | ||
// Note that group and kind are backwards for the object here because I typoed the ordering originally... This is OK as we just want a unique seed for a GUID. | ||
expected: "ResourceGroup/resources.azure.com:default/myrg", | ||
}, | ||
{ | ||
name: "GVK-based owner, full owner string included", | ||
ref: &genruntime.ResourceReference{Group: "resources.azure.com", Kind: "ResourceGroup", Name: "myrg"}, | ||
objGK: schema.GroupKind{Group: "authorization.azure.com", Kind: "RoleAssignment"}, | ||
objNamespace: "default", | ||
objName: "myroleassignment", | ||
// Note that group and kind are backwards for the object here because I typoed the ordering originally... This is OK as we just want a unique seed for a GUID. | ||
expected: "resources.azure.com/ResourceGroup:default/myrg:RoleAssignment/authorization.azure.com:default/myroleassignment", | ||
}, | ||
{ | ||
name: "ARM-ID-based owner, ARM-ID included", | ||
ref: &genruntime.ResourceReference{ARMID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myrg"}, | ||
objGK: schema.GroupKind{Group: "authorization.azure.com", Kind: "RoleAssignment"}, | ||
objNamespace: "default", | ||
objName: "myroleassignment", | ||
// Note that group and kind are backwards for the object here because I typoed the ordering originally... This is OK as we just want a unique seed for a GUID. | ||
expected: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myrg:RoleAssignment/authorization.azure.com:default/myroleassignment", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
g := NewGomegaWithT(t) | ||
|
||
result := randextensions.MakeUniqueOwnerScopedString(tt.ref, tt.objGK, tt.objNamespace, tt.objName) | ||
g.Expect(result).To(Equal(tt.expected)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_MakeUniqueOwnerScopedStringLegacy(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
ref *genruntime.ResourceReference | ||
objGK schema.GroupKind | ||
objNamespace string | ||
objName string | ||
expected string | ||
}{ | ||
{ | ||
name: "nil owner returns empty parent string", | ||
ref: nil, | ||
objGK: schema.GroupKind{Group: "resources.azure.com", Kind: "ResourceGroup"}, | ||
objNamespace: "default", | ||
objName: "myrg", | ||
expected: "ResourceGroup/resources.azure.com:default/myrg", | ||
}, | ||
{ | ||
name: "GVK-based owner, full owner string included", | ||
ref: &genruntime.ResourceReference{Group: "resources.azure.com", Kind: "ResourceGroup", Name: "myrg"}, | ||
objGK: schema.GroupKind{Group: "authorization.azure.com", Kind: "RoleAssignment"}, | ||
objNamespace: "default", | ||
objName: "myroleassignment", | ||
expected: "resources.azure.com/ResourceGroup:default/myrg:RoleAssignment/authorization.azure.com:default/myroleassignment", | ||
}, | ||
{ | ||
name: "ARM-ID-based owner, ARM-ID included", | ||
ref: &genruntime.ResourceReference{ARMID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myrg"}, | ||
objGK: schema.GroupKind{Group: "authorization.azure.com", Kind: "RoleAssignment"}, | ||
objNamespace: "default", | ||
objName: "myroleassignment", | ||
// Note that group and kind are backwards for the object here because I typoed the ordering originally... This is OK as we just want a unique seed for a GUID. | ||
expected: "/:default/:RoleAssignment/authorization.azure.com:default/myroleassignment", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
g := NewGomegaWithT(t) | ||
|
||
result := randextensions.MakeUniqueOwnerScopedStringLegacy(tt.ref, tt.objGK, tt.objNamespace, tt.objName) | ||
g.Expect(result).To(Equal(tt.expected)) | ||
}) | ||
} | ||
} |