From 3e6bfc7597448fa3f672eb7620309117acc26e3f Mon Sep 17 00:00:00 2001 From: Adam Harwayne Date: Thu, 7 Jan 2021 14:58:36 -0800 Subject: [PATCH] Check if spec.retry is nil before dereferencing it. (#2052) --- pkg/reconciler/trigger/trigger.go | 9 +- pkg/reconciler/trigger/trigger_test.go | 136 +++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/pkg/reconciler/trigger/trigger.go b/pkg/reconciler/trigger/trigger.go index 1912a9ce0e..ebc93983cf 100644 --- a/pkg/reconciler/trigger/trigger.go +++ b/pkg/reconciler/trigger/trigger.go @@ -293,10 +293,13 @@ func getPubsubDeadLetterPolicy(projectID string, spec *eventingduckv1beta1.Deliv return nil } // Translate to the pubsub dead letter policy format. - return &pubsub.DeadLetterPolicy{ - MaxDeliveryAttempts: int(*spec.Retry), - DeadLetterTopic: fmt.Sprintf("projects/%s/topics/%s", projectID, spec.DeadLetterSink.URI.Host), + dlp := &pubsub.DeadLetterPolicy{ + DeadLetterTopic: fmt.Sprintf("projects/%s/topics/%s", projectID, spec.DeadLetterSink.URI.Host), } + if spec.Retry != nil { + dlp.MaxDeliveryAttempts = int(*spec.Retry) + } + return dlp } func (r *Reconciler) deleteRetryTopicAndSubscription(ctx context.Context, trig *brokerv1beta1.Trigger) error { diff --git a/pkg/reconciler/trigger/trigger_test.go b/pkg/reconciler/trigger/trigger_test.go index d664e8aba9..a9221f7e56 100644 --- a/pkg/reconciler/trigger/trigger_test.go +++ b/pkg/reconciler/trigger/trigger_test.go @@ -96,6 +96,16 @@ var ( }, }, } + brokerDeliverySpecWithoutRetry = &eventingduckv1beta1.DeliverySpec{ + BackoffDelay: &backoffDelay, + BackoffPolicy: &backoffPolicy, + DeadLetterSink: &duckv1.Destination{ + URI: &apis.URL{ + Scheme: "pubsub", + Host: deadLetterTopicID, + }, + }, + } ) func init() { @@ -385,6 +395,132 @@ func TestAllCasesTrigger(t *testing.T) { }), }, }, + { + Name: "Sub already exists, update config", + Key: testKey, + Objects: []runtime.Object{ + NewBroker(brokerName, testNS, + WithBrokerClass(brokerv1beta1.BrokerClass), + WithInitBrokerConditions, + WithBrokerReady("url"), + WithBrokerDeliverySpec(brokerDeliverySpec), + WithBrokerSetDefaults, + ), + makeSubscriberAddressableAsUnstructured(), + NewTrigger(triggerName, testNS, brokerName, + WithTriggerUID(testUID), + WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), + WithTriggerSetDefaults), + }, + WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ + Object: NewTrigger(triggerName, testNS, brokerName, + WithTriggerUID(testUID), + WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), + WithTriggerBrokerReady, + WithTriggerSubscriptionReady, + WithTriggerTopicReady, + WithTriggerDependencyReady, + WithTriggerSubscriberResolvedSucceeded, + WithTriggerStatusSubscriberURI(subscriberURI), + WithTriggerSetDefaults, + ), + }}, + WantEvents: []string{ + triggerFinalizerUpdatedEvent, + subscriptionConfigUpdatedEvent, + triggerReconciledEvent, + }, + WantPatches: []clientgotesting.PatchActionImpl{ + patchFinalizers(testNS, triggerName, finalizerName), + }, + OtherTestData: map[string]interface{}{ + "pre": []PubsubAction{ + TopicAndSub("cre-tgr_testnamespace_test-trigger_abc123", "cre-tgr_testnamespace_test-trigger_abc123"), + Topic("test-dead-letter-topic-id"), + }, + }, + PostConditions: []func(*testing.T, *TableRow){ + OnlyTopics("cre-tgr_testnamespace_test-trigger_abc123", "test-dead-letter-topic-id"), + OnlySubscriptions("cre-tgr_testnamespace_test-trigger_abc123"), + SubscriptionHasRetryPolicy("cre-tgr_testnamespace_test-trigger_abc123", + &pubsub.RetryPolicy{ + MaximumBackoff: 5 * time.Second, + MinimumBackoff: 5 * time.Second, + }), + SubscriptionHasDeadLetterPolicy("cre-tgr_testnamespace_test-trigger_abc123", + &pubsub.DeadLetterPolicy{ + MaxDeliveryAttempts: 3, + DeadLetterTopic: "projects/test-project-id/topics/test-dead-letter-topic-id", + }), + }, + }, + { + Name: "Check topic config and labels - broker without spec.delivery.retry", + Key: testKey, + Objects: []runtime.Object{ + NewBroker(brokerName, testNS, + WithBrokerClass(brokerv1beta1.BrokerClass), + WithInitBrokerConditions, + WithBrokerReady("url"), + WithBrokerDeliverySpec(brokerDeliverySpecWithoutRetry), + WithBrokerSetDefaults, + ), + makeSubscriberAddressableAsUnstructured(), + NewTrigger(triggerName, testNS, brokerName, + WithTriggerUID(testUID), + WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), + WithTriggerSetDefaults), + }, + WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ + Object: NewTrigger(triggerName, testNS, brokerName, + WithTriggerUID(testUID), + WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), + WithTriggerBrokerReady, + WithTriggerSubscriptionReady, + WithTriggerTopicReady, + WithTriggerDependencyReady, + WithTriggerSubscriberResolvedSucceeded, + WithTriggerStatusSubscriberURI(subscriberURI), + WithTriggerSetDefaults, + ), + }}, + WantEvents: []string{ + triggerFinalizerUpdatedEvent, + topicCreatedEvent, + subscriptionCreatedEvent, + triggerReconciledEvent, + }, + WantPatches: []clientgotesting.PatchActionImpl{ + patchFinalizers(testNS, triggerName, finalizerName), + }, + OtherTestData: map[string]interface{}{ + "pre": []PubsubAction{ + Topic("test-dead-letter-topic-id"), + }, + "dataResidencyConfigMap": NewDataresidencyConfigMapFromRegions([]string{"us-east1"}), + }, + PostConditions: []func(*testing.T, *TableRow){ + OnlyTopics("cre-tgr_testnamespace_test-trigger_abc123", "test-dead-letter-topic-id"), + OnlySubscriptions("cre-tgr_testnamespace_test-trigger_abc123"), + SubscriptionHasRetryPolicy("cre-tgr_testnamespace_test-trigger_abc123", + &pubsub.RetryPolicy{ + MaximumBackoff: 5 * time.Second, + MinimumBackoff: 5 * time.Second, + }), + SubscriptionHasDeadLetterPolicy("cre-tgr_testnamespace_test-trigger_abc123", + &pubsub.DeadLetterPolicy{ + DeadLetterTopic: "projects/test-project-id/topics/test-dead-letter-topic-id", + }), + TopicExistsWithConfig("cre-tgr_testnamespace_test-trigger_abc123", &pubsub.TopicConfig{ + MessageStoragePolicy: pubsub.MessageStoragePolicy{ + AllowedPersistenceRegions: []string{"us-east1"}, + }, + Labels: map[string]string{ + "name": "test-trigger", "namespace": "testnamespace", "resource": "triggers", + }, + }), + }, + }, { Name: "Check topic config and labels", Key: testKey,