From 66152495b4640a2f3a060053116d858937390169 Mon Sep 17 00:00:00 2001 From: Grant Rodgers Date: Tue, 21 Apr 2020 01:25:50 -0700 Subject: [PATCH] Fix channel subscribers patching for v1alpha1 channels. (#2986) (#3020) Co-authored-by: Alexey Karnov --- pkg/reconciler/subscription/subscription.go | 17 ++++++++++++++++- .../subscription/subscription_test.go | 11 +++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/reconciler/subscription/subscription.go b/pkg/reconciler/subscription/subscription.go index b9fac72d0d4..eb3c0c98a20 100644 --- a/pkg/reconciler/subscription/subscription.go +++ b/pkg/reconciler/subscription/subscription.go @@ -473,7 +473,7 @@ func (r *Reconciler) updateChannelRemoveSubscription(ctx context.Context, channe return } } - r.updateChannelAddSubscriptionV1Alpha1(ctx, channel, sub) + r.updateChannelRemoveSubscriptionV1Alpha1(ctx, channel, sub) } func (r *Reconciler) updateChannelRemoveSubscriptionV1Beta1(ctx context.Context, channel *eventingduckv1alpha1.ChannelableCombined, sub *v1alpha1.Subscription) { @@ -487,6 +487,21 @@ func (r *Reconciler) updateChannelRemoveSubscriptionV1Beta1(ctx context.Context, } } +func (r *Reconciler) updateChannelRemoveSubscriptionV1Alpha1(ctx context.Context, channel *eventingduckv1alpha1.ChannelableCombined, sub *v1alpha1.Subscription) { + if channel.Spec.Subscribable == nil { + return + } + + for i, v := range channel.Spec.Subscribable.Subscribers { + if v.UID == sub.UID { + channel.Spec.Subscribable.Subscribers = append( + channel.Spec.Subscribable.Subscribers[:i], + channel.Spec.Subscribable.Subscribers[i+1:]...) + return + } + } +} + func (r *Reconciler) updateChannelAddSubscription(ctx context.Context, channel *eventingduckv1alpha1.ChannelableCombined, sub *v1alpha1.Subscription) { if channel.Annotations != nil { if channel.Annotations[messaging.SubscribableDuckVersionAnnotation] == "v1beta1" { diff --git a/pkg/reconciler/subscription/subscription_test.go b/pkg/reconciler/subscription/subscription_test.go index 3cef18edd79..52fea5a4e97 100644 --- a/pkg/reconciler/subscription/subscription_test.go +++ b/pkg/reconciler/subscription/subscription_test.go @@ -957,6 +957,9 @@ func TestAllCases(t *testing.T) { NewInMemoryChannel(channelName, testNS, WithInitInMemoryChannelConditions, WithInMemoryChannelAddress(channelDNS), + WithInMemoryChannelSubscribers([]eventingduck.SubscriberSpec{ + {UID: subscriptionUID, SubscriberURI: subscriberURI}, + }), ), }, Key: testNS + "/" + subscriptionName, @@ -966,9 +969,7 @@ func TestAllCases(t *testing.T) { Eventf(corev1.EventTypeNormal, "SubscriberRemoved", "Subscription was removed from channel \"origin\""), }, WantPatches: []clientgotesting.PatchActionImpl{ - patchSubscribers(testNS, channelName, []eventingduck.SubscriberSpec{ - {UID: subscriptionUID, SubscriberURI: serviceURI}, - }), + patchSubscribers(testNS, channelName, nil), patchRemoveFinalizers(testNS, subscriptionName), }, }, { @@ -1017,9 +1018,7 @@ func TestAllCases(t *testing.T) { ), }}, WantPatches: []clientgotesting.PatchActionImpl{ - patchSubscribers(testNS, channelName, []eventingduck.SubscriberSpec{ - {UID: subscriptionUID, SubscriberURI: serviceURI}, - }), + patchSubscribers(testNS, channelName, nil), }, }, { Name: "subscription deleted - channel does not exists",