-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* port over from v1alpha1 * add gen types * change uris from strings to api.URLvalidation, tests * oops... forgot this one
- Loading branch information
Showing
20 changed files
with
1,298 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,29 @@ | ||
/* | ||
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 v1beta1 | ||
|
||
import "context" | ||
|
||
func (et *EventType) SetDefaults(ctx context.Context) { | ||
et.Spec.SetDefaults(ctx) | ||
} | ||
|
||
func (ets *EventTypeSpec) SetDefaults(ctx context.Context) { | ||
if ets.Broker == "" { | ||
ets.Broker = "default" | ||
} | ||
} |
This file contains 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,87 @@ | ||
/* | ||
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 v1beta1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"knative.dev/pkg/apis" | ||
) | ||
|
||
func TestEventTypeDefaults(t *testing.T) { | ||
testSource := apis.HTTP("test-source") | ||
testSchema := apis.HTTP("test-schema") | ||
testCases := map[string]struct { | ||
initial EventType | ||
expected EventType | ||
}{ | ||
"nil spec": { | ||
initial: EventType{}, | ||
expected: EventType{ | ||
Spec: EventTypeSpec{ | ||
Broker: "default", | ||
}, | ||
}, | ||
}, | ||
"broker empty": { | ||
initial: EventType{ | ||
Spec: EventTypeSpec{ | ||
Type: "test-type", | ||
Source: *testSource, | ||
Broker: "", | ||
Schema: testSchema, | ||
}, | ||
}, | ||
expected: EventType{ | ||
Spec: EventTypeSpec{ | ||
Type: "test-type", | ||
Source: *testSource, | ||
Broker: "default", | ||
Schema: testSchema, | ||
}, | ||
}, | ||
}, | ||
"broker not set": { | ||
initial: EventType{ | ||
Spec: EventTypeSpec{ | ||
Type: "test-type", | ||
Source: *testSource, | ||
Schema: testSchema, | ||
}, | ||
}, | ||
expected: EventType{ | ||
Spec: EventTypeSpec{ | ||
Type: "test-type", | ||
Source: *testSource, | ||
Broker: "default", | ||
Schema: testSchema, | ||
}, | ||
}, | ||
}, | ||
} | ||
for n, tc := range testCases { | ||
t.Run(n, func(t *testing.T) { | ||
tc.initial.SetDefaults(context.TODO()) | ||
if diff := cmp.Diff(tc.expected, tc.initial); diff != "" { | ||
t.Fatalf("Unexpected defaults (-want, +got): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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,101 @@ | ||
/* | ||
* 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 v1beta1 | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"knative.dev/pkg/apis" | ||
duckv1 "knative.dev/pkg/apis/duck/v1" | ||
"knative.dev/pkg/kmeta" | ||
) | ||
|
||
// +genclient | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
type EventType struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
// Spec defines the desired state of the EventType. | ||
Spec EventTypeSpec `json:"spec,omitempty"` | ||
|
||
// Status represents the current state of the EventType. | ||
// This data may be out of date. | ||
// +optional | ||
Status EventTypeStatus `json:"status,omitempty"` | ||
} | ||
|
||
var ( | ||
// Check that EventType can be validated, can be defaulted, and has immutable fields. | ||
_ apis.Validatable = (*EventType)(nil) | ||
_ apis.Defaultable = (*EventType)(nil) | ||
|
||
// Check that EventType can return its spec untyped. | ||
_ apis.HasSpec = (*EventType)(nil) | ||
|
||
_ runtime.Object = (*EventType)(nil) | ||
|
||
// Check that we can create OwnerReferences to an EventType. | ||
_ kmeta.OwnerRefable = (*EventType)(nil) | ||
) | ||
|
||
type EventTypeSpec struct { | ||
// Type represents the CloudEvents type. It is authoritative. | ||
Type string `json:"type"` | ||
// Source is a URI, it represents the CloudEvents source. | ||
Source apis.URL `json:"source"` | ||
// Schema is a URI, it represents the CloudEvents schemaurl extension attribute. | ||
// It may be a JSON schema, a protobuf schema, etc. It is optional. | ||
// +optional | ||
Schema *apis.URL `json:"schema,omitempty"` | ||
// Broker refers to the Broker that can provide the EventType. | ||
Broker string `json:"broker"` | ||
// Description is an optional field used to describe the EventType, in any meaningful way. | ||
// +optional | ||
Description string `json:"description,omitempty"` | ||
} | ||
|
||
// EventTypeStatus represents the current state of a EventType. | ||
type EventTypeStatus struct { | ||
// inherits duck/v1 Status, which currently provides: | ||
// * ObservedGeneration - the 'Generation' of the Service that was last processed by the controller. | ||
// * Conditions - the latest available observations of a resource's current state. | ||
duckv1.Status `json:",inline"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// EventTypeList is a collection of EventTypes. | ||
type EventTypeList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []EventType `json:"items"` | ||
} | ||
|
||
// GetGroupVersionKind returns GroupVersionKind for EventType | ||
func (p *EventType) GetGroupVersionKind() schema.GroupVersionKind { | ||
return SchemeGroupVersion.WithKind("EventType") | ||
} | ||
|
||
// GetUntypedSpec returns the spec of the EventType. | ||
func (e *EventType) GetUntypedSpec() interface{} { | ||
return e.Spec | ||
} |
This file contains 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,72 @@ | ||
/* | ||
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 v1beta1 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"knative.dev/pkg/apis" | ||
"knative.dev/pkg/kmp" | ||
) | ||
|
||
func (et *EventType) Validate(ctx context.Context) *apis.FieldError { | ||
return et.Spec.Validate(ctx).ViaField("spec") | ||
} | ||
|
||
func (ets *EventTypeSpec) Validate(ctx context.Context) *apis.FieldError { | ||
var errs *apis.FieldError | ||
if ets.Type == "" { | ||
fe := apis.ErrMissingField("type") | ||
errs = errs.Also(fe) | ||
} | ||
if ets.Source.IsEmpty() { | ||
// TODO validate is a valid URI. | ||
fe := apis.ErrMissingField("source") | ||
errs = errs.Also(fe) | ||
} | ||
if ets.Broker == "" { | ||
fe := apis.ErrMissingField("broker") | ||
errs = errs.Also(fe) | ||
} | ||
// TODO validate Schema is a valid URI. | ||
return errs | ||
} | ||
|
||
func (et *EventType) CheckImmutableFields(ctx context.Context, original *EventType) *apis.FieldError { | ||
if original == nil { | ||
return nil | ||
} | ||
|
||
// All but Description field immutable. | ||
ignoreArguments := cmpopts.IgnoreFields(EventTypeSpec{}, "Description") | ||
if diff, err := kmp.ShortDiff(original.Spec, et.Spec, ignoreArguments); err != nil { | ||
return &apis.FieldError{ | ||
Message: "Failed to diff EventType", | ||
Paths: []string{"spec"}, | ||
Details: err.Error(), | ||
} | ||
} else if diff != "" { | ||
return &apis.FieldError{ | ||
Message: "Immutable fields changed (-old +new)", | ||
Paths: []string{"spec"}, | ||
Details: diff, | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.