Skip to content

Commit

Permalink
Merge pull request #4 from liggitt/kind-case-insensitive
Browse files Browse the repository at this point in the history
Tolerate lowercase Kind field in ownerReference
  • Loading branch information
k8s-ci-robot authored Mar 11, 2021
2 parents d8eb8cb + f598d55 commit d93af8d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-versions: [1.15.x]
go-versions: [1.15.x, 1.16.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.PHONY: default install build clean test fmt vet lint

all: build test fmt vet lint

default: build

build: check_go_version
Expand All @@ -10,8 +12,10 @@ build-release: check_go_version
mkdir -p ./bin/darwin/amd64
mkdir -p ./bin/linux/amd64
GOOS=darwin GOARCH=amd64 go build -trimpath -o ./bin/darwin/amd64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
GOOS=darwin GOARCH=arm64 go build -trimpath -o ./bin/darwin/arm64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
GOOS=linux GOARCH=amd64 go build -trimpath -o ./bin/linux/amd64/kubectl-check-ownerreferences $(shell ./build/print-ldflags.sh) ./
tar -cvzf ./bin/kubectl-check-ownerreferences-darwin-amd64.tar.gz LICENSE -C ./bin/darwin/amd64 kubectl-check-ownerreferences
tar -cvzf ./bin/kubectl-check-ownerreferences-darwin-arm64.tar.gz LICENSE -C ./bin/darwin/arm64 kubectl-check-ownerreferences
tar -cvzf ./bin/kubectl-check-ownerreferences-linux-amd64.tar.gz LICENSE -C ./bin/linux/amd64 kubectl-check-ownerreferences

install: check_go_version
Expand Down Expand Up @@ -51,9 +55,8 @@ lint:
check_go_version:
@OUTPUT=`go version`; \
case "$$OUTPUT" in \
*"go1.13"*);; \
*"go1.14"*);; \
*"go1.15"*);; \
*"go1.16"*);; \
*"devel"*);; \
*) \
echo "Expected: go version go1.13.*, go1.14.*, go1.15.*, or devel"; \
Expand Down
19 changes: 19 additions & 0 deletions pkg/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/client-go/tools/pager"
)

// VerifyGCOptions contains options controlling how the verify task is run
type VerifyGCOptions struct {
DiscoveryClient discovery.DiscoveryInterface
MetadataClient metadata.Interface
Expand All @@ -47,13 +48,27 @@ type VerifyGCOptions struct {
Stdout io.Writer
}

// Validate ensures the specified options are valid
func (v *VerifyGCOptions) Validate() error {
if v.DiscoveryClient == nil {
return fmt.Errorf("discovery client is required")
}
if v.MetadataClient == nil {
return fmt.Errorf("metadata client is required")
}
if v.Stderr == nil {
return fmt.Errorf("stderr is required")
}
if v.Stdout == nil {
return fmt.Errorf("stdout is required")
}
if v.Output != "" && v.Output != "json" {
return fmt.Errorf("invalid output format, only '' and 'json' are supported: %v", v.Output)
}
return nil
}

// Run executes the verify operation
func (v *VerifyGCOptions) Run() error {
errorCount := 0
warningCount := 0
Expand Down Expand Up @@ -252,6 +267,10 @@ func (v *VerifyGCOptions) Run() error {
actualOwnerGV, _ := schema.ParseGroupVersion(actualOwner.APIVersion)
if actualOwner.Kind == ownerRef.Kind && actualOwnerGV.Group == ownerGV.Group {
groupKindOk = true
} else if strings.ToLower(actualOwner.Kind) == ownerRef.Kind && actualOwnerGV.Group == ownerGV.Group {
// RESTMapper tolerates an all-lowercase kind as input to the lookup
// https://github.com/kubernetes/kubernetes/blob/release-1.20/staging/src/k8s.io/client-go/restmapper/discovery.go#L114
groupKindOk = true
} else {
actualGVK = actualOwnerGV.WithKind(actualOwner.Kind)
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,61 @@ func TestVerify(t *testing.T) {
No invalid ownerReferences found
`,
},
{
name: "case-different references",
resources: []*metav1.APIResourceList{
v1Resources,
{
GroupVersion: "group1/v1",
APIResources: []metav1.APIResource{{Name: "multiversionresources", SingularName: "multiversionresource", Namespaced: true, Kind: "MultiVersionKind", Verbs: gcVerbs}},
},
{
GroupVersion: "group1/v1beta1",
APIResources: []metav1.APIResource{{Name: "multiversionresources", SingularName: "multiversionresource", Namespaced: true, Kind: "MultiVersionKind", Verbs: gcVerbs}},
},
},
adjustMetadataClient: func(metadataClient *metadatafake.FakeMetadataClient) {
addObject(t, metadataClient, "group1/v1", "multiversionresources", "MultiVersionKind", "mgr1", "ns1", "mgruid1")
addObject(t, metadataClient, "v1", "pods", "Pod", "exact", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MultiVersionKind", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "lowercase", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionkind", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "uppercase", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MULTIVERSIONKIND", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "edgecase", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "MultiversionkinD", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "pluralkind", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionkinds", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "pluralresource", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionresources", Name: "mgr1", UID: types.UID("mgruid1")},
)
addObject(t, metadataClient, "v1", "pods", "Pod", "singularresource", "ns1", "poduid1",
metav1.OwnerReference{APIVersion: "group1/v1beta1", Kind: "multiversionresource", Name: "mgr1", UID: types.UID("mgruid1")},
)
},
expectOut: `
GROUP RESOURCE NAMESPACE NAME OWNER_UID LEVEL MESSAGE
pods ns1 edgecase mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "MultiversionkinD" in version "group1/v1beta1"
pods ns1 pluralkind mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionkinds" in version "group1/v1beta1"
pods ns1 pluralresource mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionresources" in version "group1/v1beta1"
pods ns1 singularresource mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "multiversionresource" in version "group1/v1beta1"
pods ns1 uppercase mgruid1 Error cannot resolve owner apiVersion/kind: no matches for kind "MULTIVERSIONKIND" in version "group1/v1beta1"
`,
expectErr: `
fetching v1, nodes
got 0 items
fetching v1, pods
got 7 items
fetching group1/v1, multiversionresources
got 1 item
5 errors, 0 warnings
`,
},
}

klog.InitFlags(nil)
Expand Down

0 comments on commit d93af8d

Please sign in to comment.