Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
// Eventing
eventtype.NewController,
// Trigger namespace labeler for default broker.
// Also sets the Status of Triggers that do not have a Broker.
trigger.NewController,

// Flows
Expand Down
8 changes: 8 additions & 0 deletions pkg/reconciler/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ var _ triggerreconciler.Interface = (*Reconciler)(nil)
func (r *Reconciler) ReconcileKind(ctx context.Context, t *v1beta1.Trigger) reconciler.Event {
_, err := r.brokerLister.Brokers(t.Namespace).Get(t.Spec.Broker)
if err != nil && apierrs.IsNotFound(err) {
// https://github.com/knative/eventing/issues/2996
// Ideally we'd default the Status of the Trigger during creation but we currently
// can't, so watch for Triggers that do not have Broker for them and set their status.
// This only addresses one part of the problem (missing the Broker outright, but
// not the problem where the broker is misconfigured (wrong BrokerClass)), and hence
// it still would not get reconciled.
t.Status.MarkBrokerFailed("BrokerDoesNotExist", "Broker %q does not exist or there is no matching BrokerClass for it", t.Spec.Broker)

_, needDefaultBroker := t.GetAnnotations()[v1beta1.InjectionAnnotation]
if t.Spec.Broker == "default" && needDefaultBroker {
if e := r.labelNamespace(ctx, t); e != nil {
Expand Down
61 changes: 61 additions & 0 deletions pkg/reconciler/trigger/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func TestAllCases(t *testing.T) {
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI)),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewTrigger(triggerName, testNS, brokerName,
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "test-broker" does not exist or there is no matching BrokerClass for it`)),
}},
WantErr: false,
}, {
Name: "Default broker not found, with injection annotation enabled",
Expand All @@ -96,6 +102,14 @@ func TestAllCases(t *testing.T) {
reconciletesting.WithNamespaceLabeled(map[string]string{})),
},
WantErr: false,
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewTrigger(triggerName, testNS, "default",
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithInjectionAnnotation(injectionAnnotation),
reconciletesting.WithInitTriggerConditions,
reconciletesting.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "default" does not exist or there is no matching BrokerClass for it`)),
}},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewNamespace(testNS,
reconciletesting.WithNamespaceLabeled(map[string]string{v1beta1.InjectionAnnotation: injectionAnnotation})),
Expand All @@ -120,6 +134,14 @@ func TestAllCases(t *testing.T) {
WantEvents: []string{
Eventf(corev1.EventTypeWarning, "InternalError", "namespace \"test-namespace\" not found"),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewTrigger(triggerName, testNS, "default",
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithInjectionAnnotation(injectionAnnotation),
reconciletesting.WithInitTriggerConditions,
reconciletesting.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "default" does not exist or there is no matching BrokerClass for it`)),
}},
}, {
Name: "Default broker not found, with injection annotation enabled, namespace label fail",
Key: triggerKey,
Expand All @@ -143,6 +165,45 @@ func TestAllCases(t *testing.T) {
Object: reconciletesting.NewNamespace(testNS,
reconciletesting.WithNamespaceLabeled(map[string]string{v1beta1.InjectionAnnotation: injectionAnnotation})),
}},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewTrigger(triggerName, testNS, "default",
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithInjectionAnnotation(injectionAnnotation),
reconciletesting.WithInitTriggerConditions,
reconciletesting.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "default" does not exist or there is no matching BrokerClass for it`)),
}},
}, {
Name: "Default broker not found, with injection annotation enabled, trigger status update fail",
Key: triggerKey,
Objects: []runtime.Object{
reconciletesting.NewTrigger(triggerName, testNS, "default",
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithInitTriggerConditions,
reconciletesting.WithInjectionAnnotation(injectionAnnotation)),
reconciletesting.NewNamespace(testNS,
reconciletesting.WithNamespaceLabeled(map[string]string{})),
},
WantErr: true,
WithReactors: []clientgotesting.ReactionFunc{
InduceFailure("update", "triggers"),
},
WantEvents: []string{
Eventf(corev1.EventTypeWarning, "UpdateFailed", "Failed to update status for \"test-trigger\": inducing failure for update triggers"),
},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewNamespace(testNS,
reconciletesting.WithNamespaceLabeled(map[string]string{v1beta1.InjectionAnnotation: injectionAnnotation})),
}},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: reconciletesting.NewTrigger(triggerName, testNS, "default",
reconciletesting.WithTriggerUID(triggerUID),
reconciletesting.WithTriggerSubscriberURI(subscriberURI),
reconciletesting.WithInjectionAnnotation(injectionAnnotation),
reconciletesting.WithInitTriggerConditions,
reconciletesting.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "default" does not exist or there is no matching BrokerClass for it`)),
}},
}, {
Name: "Default broker found, with injection annotation enabled",
Key: triggerKey,
Expand Down
74 changes: 74 additions & 0 deletions test/e2e/helpers/trigger_no_broker_test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 helpers

import (
"strings"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
)

const (
brokerName = "notdefaultbroker"
)

// TestTriggerNoBroker will create a Trigger with a non-existent broker, then it will ensure
// the Status is correctly reflected as failed with BrokerDoesNotExist. Then it will create
// the broker and ensure that Trigger / Broker will get to Ready state.
func TestTriggerNoBroker(t *testing.T, channel string, brokerCreator BrokerCreator) {
client := lib.Setup(t, true)
defer lib.TearDown(client)
brokerName := strings.ToLower(channel)
subscriberName := name("dumper", "", "", map[string]interface{}{})
pod := resources.EventLoggerPod(subscriberName)
client.CreatePodOrFail(pod, lib.WithService(subscriberName))
client.CreateTriggerOrFailV1Beta1("testtrigger",
resources.WithSubscriberServiceRefForTriggerV1Beta1(subscriberName),
resources.WithBrokerV1Beta1(brokerName),
)

// Then make sure the trigger is marked as not ready since there's no broker.
err := wait.PollImmediate(1*time.Second, 10*time.Second, func() (bool, error) {
trigger, err := client.Eventing.EventingV1beta1().Triggers(client.Namespace).Get("testtrigger", metav1.GetOptions{})
if err != nil {
return false, err
}
if ready := trigger.Status.GetTopLevelCondition(); ready != nil {
if ready.Status == corev1.ConditionFalse && ready.Reason == "BrokerDoesNotExist" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the reason is passed as a parameter this test is a valid conformance test for the control-plane conformance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, we could run multiple tests validations for failures? Yes, good point. I'm down to make that change, but I'd prefer to do that in a followup to make sure we get this error case handled into the release :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the broker conformance spec doesn't force using BrokerDoesNotExist as a reason for the trigger to not become ready if there is no broker;
if the test has the reason as param implementors can reuse this test without changing the reason they're using (maybe they're using BrokerNotFound rather than BrokerDoesNotExist).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that makes sense. As I mentioned above I'm all for it, but would prefer to do that in a followup? Ok with you?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course 😄

return true, nil
}
}
return false, nil
})
if err != nil {
t.Fatalf("Trigger status did not get marked as BrokerDoesNotExist: %s", err)
}

// Then create the Broker and just make sure they both come ready.
if bn := brokerCreator(client); bn != brokerName {
t.Fatalf("Broker created with unexpected name, wanted %q got %q", brokerName, bn)
}

// Wait for all test resources to become ready before sending the events.
client.WaitForAllTestResourcesReadyOrFail()
}
33 changes: 33 additions & 0 deletions test/e2e/trigger_no_broker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build e2e

/*
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 e2e

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/test/e2e/helpers"
"knative.dev/eventing/test/lib"
)

func TestTriggerNoBroker(t *testing.T) {
channelTestRunner.RunTests(t, lib.FeatureBasic, func(t *testing.T, channel metav1.TypeMeta) {
helpers.TestTriggerNoBroker(t, channel.Kind, helpers.ChannelBasedBrokerCreator(channel, brokerClass))
})
}