Skip to content

Commit dc20aa8

Browse files
committed
use custom GroupVersionKind struct
1 parent 8066e43 commit dc20aa8

File tree

10 files changed

+98
-37
lines changed

10 files changed

+98
-37
lines changed

apis/core/v1alpha1/iam_role_selector.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package v1alpha1
1515

1616
import (
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18-
"k8s.io/apimachinery/pkg/runtime/schema"
1918
)
2019

2120
// LabelSelector is a label query over a set of resources.
@@ -29,10 +28,16 @@ type NamespaceSelector struct {
2928
LabelSelector LabelSelector `json:"labelSelector,omitempty"`
3029
}
3130

31+
type GroupVersionKind struct {
32+
Group string `json:"group"`
33+
Version string `json:"version"`
34+
Kind string `json:"kind"`
35+
}
36+
3237
type IAMRoleSelectorSpec struct {
33-
ARN string `json:"arn"`
34-
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
35-
ResourceTypeSelector []schema.GroupVersionKind `json:"resourceTypeSelector,omitempty"`
38+
ARN string `json:"arn"`
39+
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
40+
ResourceTypeSelector []GroupVersionKind `json:"resourceTypeSelector,omitempty"`
3641
}
3742

3843
type IAMRoleSelectorStatus struct{}

apis/core/v1alpha1/resource_metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ResourceMetadata struct {
3333
// Region is the AWS region in which the resource exists or will exist.
3434
Region *AWSRegion `json:"region"`
3535

36-
IAMRoleSelector *SelectedIAMRole `json:"iamRoleSelector"`
36+
IAMRoleSelector *SelectedIAMRole `json:"iamRoleSelector,omitempty"`
3737
}
3838

3939
type SelectedIAMRole struct {

apis/core/v1alpha1/zz_generated.deepcopy.go

Lines changed: 36 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/services.k8s.aws_iamroleselectors.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ spec:
6262
type: object
6363
resourceTypeSelector:
6464
items:
65-
description: |-
66-
GroupVersionKind unambiguously identifies a kind. It doesn't anonymously include GroupVersion
67-
to avoid automatic coercion. It doesn't use a GroupVersion to avoid custom marshalling
65+
properties:
66+
group:
67+
type: string
68+
kind:
69+
type: string
70+
version:
71+
type: string
72+
required:
73+
- group
74+
- kind
75+
- version
6876
type: object
6977
type: array
7078
required:

mocks/pkg/types/aws_resource.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/iamroleselector/cache_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package iamroleselector
1515

1616
import (
17-
"context"
1817
"testing"
1918
"time"
2019

@@ -79,7 +78,7 @@ func TestCache_Matches(t *testing.T) {
7978
NamespaceSelector: ackv1alpha1.NamespaceSelector{
8079
Names: []string{"production"},
8180
},
82-
ResourceTypeSelector: []schema.GroupVersionKind{
81+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
8382
{Kind: "Bucket"},
8483
},
8584
},
@@ -89,7 +88,7 @@ func TestCache_Matches(t *testing.T) {
8988
ObjectMeta: metav1.ObjectMeta{Name: "all-rds"},
9089
Spec: ackv1alpha1.IAMRoleSelectorSpec{
9190
ARN: "arn:aws:iam::123456789012:role/rds-role",
92-
ResourceTypeSelector: []schema.GroupVersionKind{
91+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
9392
{
9493
Group: "rds.services.k8s.aws",
9594
Kind: "DBInstance",
@@ -150,11 +149,10 @@ func TestCache_Matches(t *testing.T) {
150149
wantCount: 0,
151150
},
152151
}
153-
ctx := context.TODO()
154152

155153
for _, tt := range tests {
156154
t.Run(tt.name, func(t *testing.T) {
157-
matches, err := cache.Matches(ctx, tt.resource)
155+
matches, err := cache.Matches(tt.resource)
158156
require.NoError(t, err)
159157
require.Len(t, matches, tt.wantCount)
160158

pkg/runtime/iamroleselector/matcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func matchesNamespace(nsSelector ackv1alpha1.NamespaceSelector, namespace string
7575
return true
7676
}
7777

78-
func matchesResourceType(rtSelectors []schema.GroupVersionKind, gvk schema.GroupVersionKind) bool {
78+
func matchesResourceType(rtSelectors []ackv1alpha1.GroupVersionKind, gvk schema.GroupVersionKind) bool {
7979
// If no resource type selector specified, matches all resources
8080
if len(rtSelectors) == 0 {
8181
return true
@@ -153,7 +153,7 @@ func validateNamespaceSelector(nsSelector ackv1alpha1.NamespaceSelector) error {
153153

154154
// validateResourceTypeSelectors checks that each resource type selector has at least one field specified
155155
// and that there are no duplicate selectors
156-
func validateResourceTypeSelectors(rtSelectors []schema.GroupVersionKind) error {
156+
func validateResourceTypeSelectors(rtSelectors []ackv1alpha1.GroupVersionKind) error {
157157
seen := make(map[string]bool)
158158

159159
for i, rts := range rtSelectors {

pkg/runtime/iamroleselector/matcher_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestMatches(t *testing.T) {
183183
selector: &ackv1alpha1.IAMRoleSelector{
184184
Spec: ackv1alpha1.IAMRoleSelectorSpec{
185185
ARN: "arn:aws:iam::123456789012:role/test-role",
186-
ResourceTypeSelector: []schema.GroupVersionKind{
186+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
187187
{
188188
Group: "s3.services.k8s.aws",
189189
Version: "v1alpha1",
@@ -207,7 +207,7 @@ func TestMatches(t *testing.T) {
207207
selector: &ackv1alpha1.IAMRoleSelector{
208208
Spec: ackv1alpha1.IAMRoleSelectorSpec{
209209
ARN: "arn:aws:iam::123456789012:role/test-role",
210-
ResourceTypeSelector: []schema.GroupVersionKind{
210+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
211211
{
212212
Kind: "Bucket",
213213
},
@@ -229,7 +229,7 @@ func TestMatches(t *testing.T) {
229229
selector: &ackv1alpha1.IAMRoleSelector{
230230
Spec: ackv1alpha1.IAMRoleSelectorSpec{
231231
ARN: "arn:aws:iam::123456789012:role/test-role",
232-
ResourceTypeSelector: []schema.GroupVersionKind{
232+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
233233
{
234234
Group: "rds.services.k8s.aws",
235235
Version: "v1alpha1",
@@ -258,7 +258,7 @@ func TestMatches(t *testing.T) {
258258
selector: &ackv1alpha1.IAMRoleSelector{
259259
Spec: ackv1alpha1.IAMRoleSelectorSpec{
260260
ARN: "arn:aws:iam::123456789012:role/test-role",
261-
ResourceTypeSelector: []schema.GroupVersionKind{
261+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
262262
{
263263
Group: "rds.services.k8s.aws",
264264
Version: "v1alpha1",
@@ -285,7 +285,7 @@ func TestMatches(t *testing.T) {
285285
NamespaceSelector: ackv1alpha1.NamespaceSelector{
286286
Names: []string{"production"},
287287
},
288-
ResourceTypeSelector: []schema.GroupVersionKind{
288+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
289289
{
290290
Kind: "Bucket",
291291
},
@@ -310,7 +310,7 @@ func TestMatches(t *testing.T) {
310310
NamespaceSelector: ackv1alpha1.NamespaceSelector{
311311
Names: []string{"production"},
312312
},
313-
ResourceTypeSelector: []schema.GroupVersionKind{
313+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
314314
{
315315
Kind: "DBInstance",
316316
},
@@ -430,7 +430,7 @@ func TestValidateSelector(t *testing.T) {
430430
selector: &ackv1alpha1.IAMRoleSelector{
431431
Spec: ackv1alpha1.IAMRoleSelectorSpec{
432432
ARN: "arn:aws:iam::123456789012:role/test-role",
433-
ResourceTypeSelector: []schema.GroupVersionKind{
433+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
434434
{
435435
// all fields empty
436436
},
@@ -445,7 +445,7 @@ func TestValidateSelector(t *testing.T) {
445445
selector: &ackv1alpha1.IAMRoleSelector{
446446
Spec: ackv1alpha1.IAMRoleSelectorSpec{
447447
ARN: "arn:aws:iam::123456789012:role/test-role",
448-
ResourceTypeSelector: []schema.GroupVersionKind{
448+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
449449
{
450450
Group: "s3.services.k8s.aws",
451451
Version: "v1alpha1",
@@ -475,7 +475,7 @@ func TestValidateSelector(t *testing.T) {
475475
},
476476
},
477477
},
478-
ResourceTypeSelector: []schema.GroupVersionKind{
478+
ResourceTypeSelector: []ackv1alpha1.GroupVersionKind{
479479
{
480480
Kind: "Bucket",
481481
},
@@ -672,13 +672,13 @@ func TestMatchesNamespace(t *testing.T) {
672672
func TestMatchesResourceType(t *testing.T) {
673673
tests := []struct {
674674
name string
675-
rtSelectors []schema.GroupVersionKind
675+
rtSelectors []ackv1alpha1.GroupVersionKind
676676
gvk schema.GroupVersionKind
677677
want bool
678678
}{
679679
{
680680
name: "empty selector matches all",
681-
rtSelectors: []schema.GroupVersionKind{},
681+
rtSelectors: []ackv1alpha1.GroupVersionKind{},
682682
gvk: schema.GroupVersionKind{
683683
Group: "s3.services.k8s.aws",
684684
Version: "v1alpha1",
@@ -688,7 +688,7 @@ func TestMatchesResourceType(t *testing.T) {
688688
},
689689
{
690690
name: "exact match",
691-
rtSelectors: []schema.GroupVersionKind{
691+
rtSelectors: []ackv1alpha1.GroupVersionKind{
692692
{
693693
Group: "s3.services.k8s.aws",
694694
Version: "v1alpha1",
@@ -704,7 +704,7 @@ func TestMatchesResourceType(t *testing.T) {
704704
},
705705
{
706706
name: "partial match - only kind",
707-
rtSelectors: []schema.GroupVersionKind{
707+
rtSelectors: []ackv1alpha1.GroupVersionKind{
708708
{
709709
Kind: "Bucket",
710710
},
@@ -718,7 +718,7 @@ func TestMatchesResourceType(t *testing.T) {
718718
},
719719
{
720720
name: "partial match - only group",
721-
rtSelectors: []schema.GroupVersionKind{
721+
rtSelectors: []ackv1alpha1.GroupVersionKind{
722722
{
723723
Group: "s3.services.k8s.aws",
724724
},
@@ -732,7 +732,7 @@ func TestMatchesResourceType(t *testing.T) {
732732
},
733733
{
734734
name: "partial match - group and version",
735-
rtSelectors: []schema.GroupVersionKind{
735+
rtSelectors: []ackv1alpha1.GroupVersionKind{
736736
{
737737
Group: "s3.services.k8s.aws",
738738
Version: "v1alpha1",
@@ -747,7 +747,7 @@ func TestMatchesResourceType(t *testing.T) {
747747
},
748748
{
749749
name: "no match - wrong kind",
750-
rtSelectors: []schema.GroupVersionKind{
750+
rtSelectors: []ackv1alpha1.GroupVersionKind{
751751
{
752752
Kind: "DBInstance",
753753
},
@@ -761,7 +761,7 @@ func TestMatchesResourceType(t *testing.T) {
761761
},
762762
{
763763
name: "no match - wrong group",
764-
rtSelectors: []schema.GroupVersionKind{
764+
rtSelectors: []ackv1alpha1.GroupVersionKind{
765765
{
766766
Group: "rds.services.k8s.aws",
767767
Version: "v1alpha1",
@@ -777,7 +777,7 @@ func TestMatchesResourceType(t *testing.T) {
777777
},
778778
{
779779
name: "OR logic - multiple selectors",
780-
rtSelectors: []schema.GroupVersionKind{
780+
rtSelectors: []ackv1alpha1.GroupVersionKind{
781781
{
782782
Kind: "DBInstance",
783783
},
@@ -797,7 +797,7 @@ func TestMatchesResourceType(t *testing.T) {
797797
},
798798
{
799799
name: "OR logic - no match",
800-
rtSelectors: []schema.GroupVersionKind{
800+
rtSelectors: []ackv1alpha1.GroupVersionKind{
801801
{
802802
Kind: "DBInstance",
803803
},

0 commit comments

Comments
 (0)