From 40d12033689341e823bb1f8b7128f4ffef52247e Mon Sep 17 00:00:00 2001 From: Ville Aikas Date: Fri, 31 Jan 2020 12:15:24 -0800 Subject: [PATCH 1/2] add KnativeReference --- apis/duck/v1/destination_test.go | 1 + apis/duck/v1/knative_reference.go | 66 ++++++++++++++++ apis/duck/v1/knative_reference_test.go | 103 +++++++++++++++++++++++++ apis/duck/v1/zz_generated.deepcopy.go | 16 ++++ 4 files changed, 186 insertions(+) create mode 100644 apis/duck/v1/knative_reference.go create mode 100644 apis/duck/v1/knative_reference_test.go diff --git a/apis/duck/v1/destination_test.go b/apis/duck/v1/destination_test.go index 3bb6bc2f96..d575814f32 100644 --- a/apis/duck/v1/destination_test.go +++ b/apis/duck/v1/destination_test.go @@ -29,6 +29,7 @@ const ( kind = "SomeKind" apiVersion = "v1mega1" name = "a-name" + namespace = "b-namespace" ) func TestValidateDestination(t *testing.T) { diff --git a/apis/duck/v1/knative_reference.go b/apis/duck/v1/knative_reference.go new file mode 100644 index 0000000000..84677a22a3 --- /dev/null +++ b/apis/duck/v1/knative_reference.go @@ -0,0 +1,66 @@ +/* +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 v1 + +import ( + "context" + + "knative.dev/pkg/apis" +) + +// KnativeReference contains enough information to refer to another object. +// It's a trimmed down version of corev1.ObjectReference. +type KnativeReference struct { + // Kind of the referent. + // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + Kind string `json:"kind,omitempty"` + + // Namespace of the referent. + // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/ + Namespace string `json:"namespace,omitempty"` + + // Name of the referent. + // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + Name string `json:"name,omitempty"` + + // API version of the referent. + APIVersion string `json:"apiVersion,omitempty"` +} + +func (kr *KnativeReference) Validate(ctx context.Context) *apis.FieldError { + var errs *apis.FieldError + if kr == nil { + errs = errs.Also(apis.ErrMissingField("namespace")) + errs = errs.Also(apis.ErrMissingField("name")) + errs = errs.Also(apis.ErrMissingField("apiVersion")) + errs = errs.Also(apis.ErrMissingField("kind")) + return errs + } + if kr.Namespace == "" { + errs = errs.Also(apis.ErrMissingField("namespace")) + } + if kr.Name == "" { + errs = errs.Also(apis.ErrMissingField("name")) + } + if kr.APIVersion == "" { + errs = errs.Also(apis.ErrMissingField("apiVersion")) + } + if kr.Kind == "" { + errs = errs.Also(apis.ErrMissingField("kind")) + } + return errs +} diff --git a/apis/duck/v1/knative_reference_test.go b/apis/duck/v1/knative_reference_test.go new file mode 100644 index 0000000000..6c906dfbbe --- /dev/null +++ b/apis/duck/v1/knative_reference_test.go @@ -0,0 +1,103 @@ +/* +Copyright 2019 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 + +import ( + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "knative.dev/pkg/apis" +) + +func TestValidate(t *testing.T) { + ctx := context.TODO() + + validRef := KnativeReference{ + Kind: kind, + APIVersion: apiVersion, + Name: name, + Namespace: namespace, + } + + tests := map[string]struct { + ref *KnativeReference + want *apis.FieldError + }{ + "nil valid": { + ref: nil, + want: func() *apis.FieldError { + fe := apis.ErrMissingField("name", "namespace", "kind", "apiVersion") + return fe + }(), + }, + "valid ref": { + ref: &validRef, + want: nil, + }, + "invalid ref, empty": { + ref: &KnativeReference{}, + want: apis.ErrMissingField("name", "namespace", "kind", "apiVersion"), + }, + "invalid ref, missing namespace": { + ref: &KnativeReference{ + Name: name, + Kind: kind, + APIVersion: apiVersion, + }, + want: func() *apis.FieldError { + fe := apis.ErrMissingField("namespace") + return fe + }(), + }, + "invalid ref, missing kind": { + ref: &KnativeReference{ + Namespace: namespace, + Name: name, + APIVersion: apiVersion, + }, + want: func() *apis.FieldError { + fe := apis.ErrMissingField("kind") + return fe + }(), + }, + "invalid ref, missing api version": { + ref: &KnativeReference{ + Namespace: namespace, + Name: name, + Kind: kind, + }, + want: func() *apis.FieldError { + return apis.ErrMissingField("apiVersion") + }(), + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + gotErr := tc.ref.Validate(ctx) + + if tc.want != nil { + if diff := cmp.Diff(tc.want.Error(), gotErr.Error()); diff != "" { + t.Errorf("%s: got: %v wanted %v", name, gotErr, tc.want) + } + } else if gotErr != nil { + t.Errorf("%s: Validate() = %v, wanted nil", name, gotErr) + } + }) + } +} diff --git a/apis/duck/v1/zz_generated.deepcopy.go b/apis/duck/v1/zz_generated.deepcopy.go index fd54ec6038..31b6a5679a 100644 --- a/apis/duck/v1/zz_generated.deepcopy.go +++ b/apis/duck/v1/zz_generated.deepcopy.go @@ -259,6 +259,22 @@ func (in *KResourceList) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *KnativeReference) DeepCopyInto(out *KnativeReference) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KnativeReference. +func (in *KnativeReference) DeepCopy() *KnativeReference { + if in == nil { + return nil + } + out := new(KnativeReference) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PodSpecable) DeepCopyInto(out *PodSpecable) { *out = *in From aa90cf363721ed3ed0f763c42a0d19227a1666fa Mon Sep 17 00:00:00 2001 From: Ville Aikas Date: Fri, 31 Jan 2020 12:29:11 -0800 Subject: [PATCH 2/2] fix copyright year --- apis/duck/v1/knative_reference_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/duck/v1/knative_reference_test.go b/apis/duck/v1/knative_reference_test.go index 6c906dfbbe..d377295ec9 100644 --- a/apis/duck/v1/knative_reference_test.go +++ b/apis/duck/v1/knative_reference_test.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Knative Authors +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.