-
Notifications
You must be signed in to change notification settings - Fork 342
Add KReference.Group field and ResolveGroup function #2127
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
Merged
knative-prow-robot
merged 9 commits into
knative:main
from
slinkydeveloper:kreference_group
Jun 15, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69738db
Add Group field and ResolveGroup function
slinkydeveloper 8433bab
Remove core special case
slinkydeveloper 90c4222
Copyright
slinkydeveloper 2168ef4
Added validation code
slinkydeveloper 4a139d9
Fix comment
slinkydeveloper ab610a5
Add omitempty
slinkydeveloper ee55b44
Moved ResolveGroup code to kref
slinkydeveloper 7303d0b
New type KReferenceResolver
slinkydeveloper 57ab67e
Added +optional as suggested
slinkydeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| /* | ||
| Copyright 2020 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package kref | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| apiextensionsv1lister "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| duckv1 "knative.dev/pkg/apis/duck/v1" | ||
| ) | ||
|
|
||
| // KReferenceResolver is an object that resolves the KReference.Group field | ||
| // Note: This API is EXPERIMENTAL and might break anytime. For more details: https://github.com/knative/eventing/issues/5086 | ||
| type KReferenceResolver struct { | ||
| crdLister apiextensionsv1lister.CustomResourceDefinitionLister | ||
| } | ||
|
|
||
| // NewKReferenceResolver creates a new KReferenceResolver from a crdLister | ||
| // Note: This API is EXPERIMENTAL and might break anytime. For more details: https://github.com/knative/eventing/issues/5086 | ||
| func NewKReferenceResolver(crdLister apiextensionsv1lister.CustomResourceDefinitionLister) *KReferenceResolver { | ||
| return &KReferenceResolver{crdLister: crdLister} | ||
| } | ||
|
|
||
| // ResolveGroup resolves the APIVersion of a KReference starting from the Group. | ||
| // In order to execute this method, you need RBAC to read the CRD of the Resource referred in this KReference. | ||
| // Note: This API is EXPERIMENTAL and might break anytime. For more details: https://github.com/knative/eventing/issues/5086 | ||
| func (resolver *KReferenceResolver) ResolveGroup(kr *duckv1.KReference) (*duckv1.KReference, error) { | ||
| if kr.Group == "" { | ||
| // Nothing to do here | ||
| return kr, nil | ||
| } | ||
|
|
||
| kr = kr.DeepCopy() | ||
|
|
||
| actualGvk := schema.GroupVersionKind{Group: kr.Group, Kind: kr.Kind} | ||
| pluralGvk, _ := meta.UnsafeGuessKindToResource(actualGvk) | ||
| crd, err := resolver.crdLister.Get(pluralGvk.GroupResource().String()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| actualGvk.Version, err = findCRDStorageVersion(crd) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| kr.APIVersion, kr.Kind = actualGvk.ToAPIVersionAndKind() | ||
|
|
||
| return kr, nil | ||
| } | ||
|
|
||
| // This function runs under the assumption that there must be exactly one "storage" version | ||
| func findCRDStorageVersion(crd *apiextensionsv1.CustomResourceDefinition) (string, error) { | ||
| for _, version := range crd.Spec.Versions { | ||
| if version.Storage { | ||
| return version.Name, nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("this CRD %s doesn't have a storage version! Kubernetes, you're drunk, go home", crd.Name) | ||
| } |
This file contains hidden or 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,127 @@ | ||
| /* | ||
| Copyright 2021 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package kref | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/rest" | ||
|
|
||
| . "knative.dev/pkg/apis/duck/v1" | ||
| customresourcedefinitioninformer "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1/customresourcedefinition/fake" | ||
| "knative.dev/pkg/injection" | ||
| ) | ||
|
|
||
| func TestResolveGroup(t *testing.T) { | ||
| const crdGroup = "messaging.knative.dev" | ||
| const crdName = "inmemorychannels." + crdGroup | ||
|
|
||
| ctx, _ := injection.Fake.SetupInformers(context.TODO(), &rest.Config{}) | ||
|
|
||
| fakeCrdInformer := customresourcedefinitioninformer.Get(ctx) | ||
| fakeCrdInformer.Informer().GetIndexer().Add( | ||
| &apiextensionsv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: crdName, | ||
| }, | ||
| Spec: apiextensionsv1.CustomResourceDefinitionSpec{ | ||
| Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{ | ||
| Name: "v1beta1", | ||
| Storage: false, | ||
| }, { | ||
| Name: "v1", | ||
| Storage: true, | ||
| }}, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| tests := map[string]struct { | ||
| input *KReference | ||
| output *KReference | ||
| wantErr bool | ||
| }{ | ||
| "No group": { | ||
| input: &KReference{ | ||
| Kind: "Abc", | ||
| Name: "123", | ||
| APIVersion: "something/v1", | ||
| }, | ||
| output: &KReference{ | ||
| Kind: "Abc", | ||
| Name: "123", | ||
| APIVersion: "something/v1", | ||
| }, | ||
| }, | ||
| "No group nor api version": { | ||
| input: &KReference{ | ||
| Kind: "Abc", | ||
| Name: "123", | ||
| }, | ||
| output: &KReference{ | ||
| Kind: "Abc", | ||
| Name: "123", | ||
| }, | ||
| }, | ||
| "imc channel": { | ||
| input: &KReference{ | ||
| Kind: "InMemoryChannel", | ||
| Name: "my-cool-channel", | ||
| Group: crdGroup, | ||
| }, | ||
| output: &KReference{ | ||
| Kind: "InMemoryChannel", | ||
| Name: "my-cool-channel", | ||
| Group: crdGroup, | ||
| APIVersion: crdGroup + "/v1", | ||
| }, | ||
| }, | ||
| "unknown CRD": { | ||
| input: &KReference{ | ||
| Kind: "MyChannel", | ||
| Name: "my-cool-channel", | ||
| Group: crdGroup, | ||
| }, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| kr, err := NewKReferenceResolver(fakeCrdInformer.Lister()).ResolveGroup(tc.input) | ||
| if err != nil { | ||
| if !tc.wantErr { | ||
| t.Error("ResolveGroup() =", err) | ||
| } | ||
| return | ||
| } else if tc.wantErr { | ||
| t.Errorf("ResolveGroup() = %v, wanted error", err) | ||
| return | ||
| } | ||
|
|
||
| if tc.output != nil { | ||
| if !cmp.Equal(tc.output, kr) { | ||
| t.Errorf("ResolveGroup diff: (-want, +got) =\n%s", cmp.Diff(tc.input, tc.output)) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.