-
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.
* v1beta1 Broker type initial cut * first cut of broker that supports alternate impls not tied to messaging constructs * address pr comments * add validation * validate + test * brokerClassAnnotationKey -> BrokerClassAnnotationKey
- Loading branch information
Showing
20 changed files
with
1,308 additions
and
0 deletions.
There are no files selected for viewing
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
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,64 @@ | ||
/* | ||
Copyright 2020 The Knative Authors. All Rights Reserved. | ||
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" | ||
duckv1 "knative.dev/pkg/apis/duck/v1" | ||
) | ||
|
||
func TestDeliverySpecValidation(t *testing.T) { | ||
invalidString := "invalid time" | ||
bop := BackoffPolicyExponential | ||
tests := []struct { | ||
name string | ||
spec *DeliverySpec | ||
want *apis.FieldError | ||
}{{ | ||
name: "nil is valid", | ||
spec: nil, | ||
want: nil, | ||
}, { | ||
name: "invalid time format", | ||
spec: &DeliverySpec{BackoffDelay: &invalidString}, | ||
want: func() *apis.FieldError { | ||
return apis.ErrInvalidValue(invalidString, "backoffDelay") | ||
}(), | ||
}, { | ||
name: "invalid deadLetterSink", | ||
spec: &DeliverySpec{DeadLetterSink: &duckv1.Destination{}}, | ||
want: func() *apis.FieldError { | ||
return apis.ErrGeneric("expected at least one, got none", "ref", "uri").ViaField("deadLetterSink") | ||
}(), | ||
}, { | ||
name: "valid backoffPolicy", | ||
spec: &DeliverySpec{BackoffPolicy: &bop}, | ||
want: nil, | ||
}} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := test.spec.Validate(context.TODO()) | ||
if diff := cmp.Diff(test.want.Error(), got.Error()); diff != "" { | ||
t.Errorf("DeliverySpec.Validate (-want, +got) = %v", 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,30 @@ | ||
/* | ||
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 (b *Broker) SetDefaults(ctx context.Context) { | ||
// TODO(vaikas): Set the default class annotation if not specified | ||
b.Spec.SetDefaults(ctx) | ||
} | ||
|
||
func (bs *BrokerSpec) SetDefaults(ctx context.Context) { | ||
// None | ||
} |
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,111 @@ | ||
/* | ||
* 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 ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
eventingduckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" | ||
"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 | ||
|
||
// Broker collects a pool of events that are consumable using Triggers. Brokers | ||
// provide a well-known endpoint for event delivery that senders can use with | ||
// minimal knowledge of the event routing strategy. Receivers use Triggers to | ||
// request delivery of events from a Broker's pool to a specific URL or | ||
// Addressable endpoint. | ||
type Broker struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
// Spec defines the desired state of the Broker. | ||
Spec BrokerSpec `json:"spec,omitempty"` | ||
|
||
// Status represents the current state of the Broker. This data may be out of | ||
// date. | ||
// +optional | ||
Status BrokerStatus `json:"status,omitempty"` | ||
} | ||
|
||
var ( | ||
// Check that Broker can be validated, can be defaulted, and has immutable fields. | ||
_ apis.Validatable = (*Broker)(nil) | ||
_ apis.Defaultable = (*Broker)(nil) | ||
|
||
// Check that Broker can return its spec untyped. | ||
_ apis.HasSpec = (*Broker)(nil) | ||
|
||
_ runtime.Object = (*Broker)(nil) | ||
|
||
// Check that we can create OwnerReferences to a Broker. | ||
_ kmeta.OwnerRefable = (*Broker)(nil) | ||
) | ||
|
||
type BrokerSpec struct { | ||
// Config is an ObjectReference to the configuration that specifies | ||
// configuration options for this Broker. For example, this could be | ||
// a pointer to a ConfigMap. | ||
// +optional | ||
Config *corev1.ObjectReference `json:"config,omitempty"` | ||
|
||
// Delivery is the delivery specification for Events within the Broker mesh. | ||
// This includes things like retries, DLQ, etc. | ||
// +optional | ||
Delivery *eventingduckv1beta1.DeliverySpec `json:"delivery,omitempty"` | ||
} | ||
|
||
// BrokerStatus represents the current state of a Broker. | ||
type BrokerStatus 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"` | ||
|
||
// Broker is Addressable. It exposes the endpoint as an URI to get events | ||
// delivered into the Broker mesh. | ||
Address duckv1.Addressable `json:"address,omitempty"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// BrokerList is a collection of Brokers. | ||
type BrokerList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// +optional | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
|
||
Items []Broker `json:"items"` | ||
} | ||
|
||
// GetGroupVersionKind returns GroupVersionKind for Brokers | ||
func (t *Broker) GetGroupVersionKind() schema.GroupVersionKind { | ||
return SchemeGroupVersion.WithKind("Broker") | ||
} | ||
|
||
// GetUntypedSpec returns the spec of the Broker. | ||
func (b *Broker) GetUntypedSpec() interface{} { | ||
return b.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,98 @@ | ||
/* | ||
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" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"knative.dev/pkg/apis" | ||
"knative.dev/pkg/kmp" | ||
) | ||
|
||
const ( | ||
BrokerClassAnnotationKey = "eventing.knative.dev/broker.class" | ||
) | ||
|
||
func (b *Broker) Validate(ctx context.Context) *apis.FieldError { | ||
return b.Spec.Validate(ctx).ViaField("spec") | ||
} | ||
|
||
func (bs *BrokerSpec) Validate(ctx context.Context) *apis.FieldError { | ||
var errs *apis.FieldError | ||
|
||
// Validate the Config | ||
if bs.Config != nil { | ||
if ce := isValidConfig(bs.Config); ce != nil { | ||
errs = errs.Also(ce.ViaField("config")) | ||
} | ||
} | ||
|
||
if bs.Delivery != nil { | ||
if de := bs.Delivery.Validate(ctx); de != nil { | ||
errs = errs.Also(de.ViaField("delivery")) | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
func isValidConfig(ref *corev1.ObjectReference) *apis.FieldError { | ||
if ref == nil { | ||
return nil | ||
} | ||
// Check the object. | ||
var errs *apis.FieldError | ||
if ref.Name == "" { | ||
errs = errs.Also(apis.ErrMissingField("name")) | ||
} | ||
if ref.Namespace == "" { | ||
errs = errs.Also(apis.ErrMissingField("namespace")) | ||
} | ||
if ref.APIVersion == "" { | ||
errs = errs.Also(apis.ErrMissingField("apiVersion")) | ||
} | ||
if ref.Kind == "" { | ||
errs = errs.Also(apis.ErrMissingField("kind")) | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func (b *Broker) CheckImmutableFields(ctx context.Context, original *Broker) *apis.FieldError { | ||
if original == nil { | ||
return nil | ||
} | ||
|
||
// Make sure you can't change the class annotation. | ||
diff, err := kmp.ShortDiff(original.GetAnnotations()[BrokerClassAnnotationKey], b.GetAnnotations()[BrokerClassAnnotationKey]) | ||
|
||
if err != nil { | ||
return &apis.FieldError{ | ||
Message: "couldn't diff the Broker objects", | ||
Details: err.Error(), | ||
} | ||
} | ||
|
||
if diff != "" { | ||
return &apis.FieldError{ | ||
Message: "Immutable fields changed (-old +new)", | ||
Paths: []string{"annotations"}, | ||
Details: diff, | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.