Skip to content
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

Add KReference.Group field and ResolveGroup function #2127

Merged
merged 9 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions apis/duck/v1/knative_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"context"
"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"

"knative.dev/pkg/apis"
)

Expand All @@ -42,6 +47,11 @@ type KReference struct {

// API version of the referent.
APIVersion string `json:"apiVersion"`

slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
// Group of the API, without the version of the group. This can be used as an alternative to the APIVersion, and then resolved using ResolveGroup.
// Note: This API is EXPERIMENTAL and might break anytime. For more details: https://github.com/knative/eventing/issues/5086
// +optional
Group string `json:"group,omitempty"`
}

func (kr *KReference) Validate(ctx context.Context) *apis.FieldError {
Expand All @@ -54,8 +64,25 @@ func (kr *KReference) Validate(ctx context.Context) *apis.FieldError {
if kr.Name == "" {
errs = errs.Also(apis.ErrMissingField("name"))
}
if kr.APIVersion == "" {
errs = errs.Also(apis.ErrMissingField("apiVersion"))
if isKReferenceGroupAllowed(ctx) {
if kr.APIVersion == "" && kr.Group == "" {
errs = errs.Also(apis.ErrMissingField("apiVersion")).
Also(apis.ErrMissingField("group"))
}
if kr.APIVersion != "" && kr.Group != "" {
errs = errs.Also(&apis.FieldError{
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
Message: "both apiVersion and group are specified",
Paths: []string{"apiVersion", "group"},
Details: "Only one of them must be specified",
})
}
} else {
if kr.Group != "" {
errs = errs.Also(apis.ErrDisallowedFields("group"))
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
}
if kr.APIVersion == "" {
errs = errs.Also(apis.ErrMissingField("apiVersion"))
}
}
if kr.Kind == "" {
errs = errs.Also(apis.ErrMissingField("kind"))
Expand Down Expand Up @@ -86,3 +113,53 @@ func (kr *KReference) SetDefaults(ctx context.Context) {
kr.Namespace = apis.ParentMeta(ctx).Namespace
}
}

// 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 (kr *KReference) ResolveGroup(crdLister apiextensionsv1lister.CustomResourceDefinitionLister) error {
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
if kr.Group == "" {
// Nothing to do here
return nil
}

actualGvk := schema.GroupVersionKind{Group: kr.Group, Kind: kr.Kind}
pluralGvk, _ := meta.UnsafeGuessKindToResource(actualGvk)
crd, err := crdLister.Get(pluralGvk.GroupResource().String())
if err != nil {
return err
}

actualGvk.Version, err = findCRDStorageVersion(crd)
if err != nil {
return err
}

kr.APIVersion, kr.Kind = actualGvk.ToAPIVersionAndKind()
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

// This function runs under the assumption that there must be exactly one "storage" version
func findCRDStorageVersion(crd *apiextensionsv1.CustomResourceDefinition) (string, error) {
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
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)
}

type isGroupAllowed struct{}

func isKReferenceGroupAllowed(ctx context.Context) bool {
return ctx.Value(isGroupAllowed{}) != nil
}

// KReferenceGroupAllowed notes on the context that further validation
// should allow the KReference.Group, which is disabled by default.
// Note: This API is EXPERIMENTAL and may disappear once the KReference.Group feature will stabilize.
// For more details: https://github.com/knative/eventing/issues/5086
func KReferenceGroupAllowed(ctx context.Context) context.Context {
return context.WithValue(ctx, isGroupAllowed{}, struct{}{})
}
127 changes: 127 additions & 0 deletions apis/duck/v1/knative_reference_resolve_group_test.go
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 v1_test

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) {
err := tc.input.ResolveGroup(fakeCrdInformer.Lister())
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, tc.input) {
t.Errorf("ResolveGroup diff: (-want, +got) =\n%s", cmp.Diff(tc.input, tc.output))
}
}
})
}
}
58 changes: 58 additions & 0 deletions apis/duck/v1/knative_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"knative.dev/pkg/apis"
)

const (
group = "group.my"
)

func TestValidate(t *testing.T) {
ctx := context.Background()

Expand Down Expand Up @@ -87,6 +91,60 @@ func TestValidate(t *testing.T) {
Details: `parent namespace: "diffns" does not match ref: "b-namespace"`,
},
},
"invalid ref, disallowed group": {
ref: &KReference{
Namespace: namespace,
Name: name,
Kind: kind,
Group: group,
},
ctx: ctx,
want: apis.ErrMissingField("apiVersion").Also(apis.ErrDisallowedFields("group")),
},
"invalid ref, group allowed buth both api version and group are configured": {
ref: &KReference{
Namespace: namespace,
Name: name,
Kind: kind,
Group: group,
APIVersion: apiVersion,
},
ctx: KReferenceGroupAllowed(ctx),
want: &apis.FieldError{
Message: "both apiVersion and group are specified",
Paths: []string{"apiVersion", "group"},
Details: "Only one of them must be specified",
},
},
"valid ref, group enabled and both apiVersion and group missing": {
ref: &KReference{
Namespace: namespace,
Name: name,
Kind: kind,
},
ctx: KReferenceGroupAllowed(ctx),
want: apis.ErrMissingField("apiVersion").Also(apis.ErrMissingField("group")),
},
"valid ref, group enabled and configured": {
ref: &KReference{
Namespace: namespace,
Name: name,
Kind: kind,
Group: group,
},
ctx: KReferenceGroupAllowed(ctx),
want: nil,
},
"valid ref, group enabled and but apiVersion configured": {
ref: &KReference{
Namespace: namespace,
Name: name,
Kind: kind,
APIVersion: apiVersion,
},
ctx: KReferenceGroupAllowed(ctx),
want: nil,
},
"valid ref, mismatched namespaces, but overridden": {
ref: &KReference{
Namespace: namespace,
Expand Down