Skip to content

Commit d93af8d

Browse files
authored
Merge pull request #4 from liggitt/kind-case-insensitive
Tolerate lowercase Kind field in ownerReference
2 parents d8eb8cb + f598d55 commit d93af8d

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-versions: [1.15.x]
7+
go-versions: [1.15.x, 1.16.x]
88
platform: [ubuntu-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.PHONY: default install build clean test fmt vet lint
22

3+
all: build test fmt vet lint
4+
35
default: build
46

57
build: check_go_version
@@ -10,8 +12,10 @@ build-release: check_go_version
1012
mkdir -p ./bin/darwin/amd64
1113
mkdir -p ./bin/linux/amd64
1214
GOOS=darwin GOARCH=amd64 go build -trimpath -o ./bin/darwin/amd64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
15+
GOOS=darwin GOARCH=arm64 go build -trimpath -o ./bin/darwin/arm64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
1316
GOOS=linux GOARCH=amd64 go build -trimpath -o ./bin/linux/amd64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
1417
tar -cvzf ./bin/kubectl-check-ownerreferences-darwin-amd64.tar.gz LICENSE -C ./bin/darwin/amd64 kubectl-check-ownerreferences
18+
tar -cvzf ./bin/kubectl-check-ownerreferences-darwin-arm64.tar.gz LICENSE -C ./bin/darwin/arm64 kubectl-check-ownerreferences
1519
tar -cvzf ./bin/kubectl-check-ownerreferences-linux-amd64.tar.gz LICENSE -C ./bin/linux/amd64 kubectl-check-ownerreferences
1620

1721
install: check_go_version
@@ -51,9 +55,8 @@ lint:
5155
check_go_version:
5256
@OUTPUT=`go version`; \
5357
case "$$OUTPUT" in \
54-
*"go1.13"*);; \
55-
*"go1.14"*);; \
5658
*"go1.15"*);; \
59+
*"go1.16"*);; \
5760
*"devel"*);; \
5861
*) \
5962
echo "Expected: go version go1.13.*, go1.14.*, go1.15.*, or devel"; \

pkg/verify.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"k8s.io/client-go/tools/pager"
4040
)
4141

42+
// VerifyGCOptions contains options controlling how the verify task is run
4243
type VerifyGCOptions struct {
4344
DiscoveryClient discovery.DiscoveryInterface
4445
MetadataClient metadata.Interface
@@ -47,13 +48,27 @@ type VerifyGCOptions struct {
4748
Stdout io.Writer
4849
}
4950

51+
// Validate ensures the specified options are valid
5052
func (v *VerifyGCOptions) Validate() error {
53+
if v.DiscoveryClient == nil {
54+
return fmt.Errorf("discovery client is required")
55+
}
56+
if v.MetadataClient == nil {
57+
return fmt.Errorf("metadata client is required")
58+
}
59+
if v.Stderr == nil {
60+
return fmt.Errorf("stderr is required")
61+
}
62+
if v.Stdout == nil {
63+
return fmt.Errorf("stdout is required")
64+
}
5165
if v.Output != "" && v.Output != "json" {
5266
return fmt.Errorf("invalid output format, only '' and 'json' are supported: %v", v.Output)
5367
}
5468
return nil
5569
}
5670

71+
// Run executes the verify operation
5772
func (v *VerifyGCOptions) Run() error {
5873
errorCount := 0
5974
warningCount := 0
@@ -252,6 +267,10 @@ func (v *VerifyGCOptions) Run() error {
252267
actualOwnerGV, _ := schema.ParseGroupVersion(actualOwner.APIVersion)
253268
if actualOwner.Kind == ownerRef.Kind && actualOwnerGV.Group == ownerGV.Group {
254269
groupKindOk = true
270+
} else if strings.ToLower(actualOwner.Kind) == ownerRef.Kind && actualOwnerGV.Group == ownerGV.Group {
271+
// RESTMapper tolerates an all-lowercase kind as input to the lookup
272+
// https://github.com/kubernetes/kubernetes/blob/release-1.20/staging/src/k8s.io/client-go/restmapper/discovery.go#L114
273+
groupKindOk = true
255274
} else {
256275
actualGVK = actualOwnerGV.WithKind(actualOwner.Kind)
257276
}

pkg/verify_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,61 @@ func TestVerify(t *testing.T) {
333333
No invalid ownerReferences found
334334
`,
335335
},
336+
{
337+
name: "case-different references",
338+
resources: []*metav1.APIResourceList{
339+
v1Resources,
340+
{
341+
GroupVersion: "group1/v1",
342+
APIResources: []metav1.APIResource{{Name: "multiversionresources", SingularName: "multiversionresource", Namespaced: true, Kind: "MultiVersionKind", Verbs: gcVerbs}},
343+
},
344+
{
345+
GroupVersion: "group1/v1beta1",
346+
APIResources: []metav1.APIResource{{Name: "multiversionresources", SingularName: "multiversionresource", Namespaced: true, Kind: "MultiVersionKind", Verbs: gcVerbs}},
347+
},
348+
},
349+
adjustMetadataClient: func(metadataClient *metadatafake.FakeMetadataClient) {
350+
addObject(t, metadataClient, "group1/v1", "multiversionresources", "MultiVersionKind", "mgr1", "ns1", "mgruid1")
351+
addObject(t, metadataClient, "v1", "pods", "Pod", "exact", "ns1", "poduid1",
352+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MultiVersionKind", Name: "mgr1", UID: types.UID("mgruid1")},
353+
)
354+
addObject(t, metadataClient, "v1", "pods", "Pod", "lowercase", "ns1", "poduid1",
355+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionkind", Name: "mgr1", UID: types.UID("mgruid1")},
356+
)
357+
addObject(t, metadataClient, "v1", "pods", "Pod", "uppercase", "ns1", "poduid1",
358+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MULTIVERSIONKIND", Name: "mgr1", UID: types.UID("mgruid1")},
359+
)
360+
addObject(t, metadataClient, "v1", "pods", "Pod", "edgecase", "ns1", "poduid1",
361+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MultiversionkinD", Name: "mgr1", UID: types.UID("mgruid1")},
362+
)
363+
addObject(t, metadataClient, "v1", "pods", "Pod", "pluralkind", "ns1", "poduid1",
364+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionkinds", Name: "mgr1", UID: types.UID("mgruid1")},
365+
)
366+
addObject(t, metadataClient, "v1", "pods", "Pod", "pluralresource", "ns1", "poduid1",
367+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionresources", Name: "mgr1", UID: types.UID("mgruid1")},
368+
)
369+
addObject(t, metadataClient, "v1", "pods", "Pod", "singularresource", "ns1", "poduid1",
370+
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionresource", Name: "mgr1", UID: types.UID("mgruid1")},
371+
)
372+
},
373+
expectOut: `
374+
GROUP RESOURCE NAMESPACE NAME OWNER_UID LEVEL MESSAGE
375+
pods ns1 edgecase mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "MultiversionkinD" in version "group1/v1beta1"
376+
pods ns1 pluralkind mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionkinds" in version "group1/v1beta1"
377+
pods ns1 pluralresource mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionresources" in version "group1/v1beta1"
378+
pods ns1 singularresource mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionresource" in version "group1/v1beta1"
379+
pods ns1 uppercase mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "MULTIVERSIONKIND" in version "group1/v1beta1"
380+
`,
381+
expectErr: `
382+
fetching v1, nodes
383+
got 0 items
384+
fetching v1, pods
385+
got 7 items
386+
fetching group1/v1, multiversionresources
387+
got 1 item
388+
5 errors, 0 warnings
389+
`,
390+
},
336391
}
337392

338393
klog.InitFlags(nil)

0 commit comments

Comments
 (0)