Skip to content

Commit

Permalink
Map subscription to graph edges (#7880)
Browse files Browse the repository at this point in the history
* feat: add the function AddSubscription

Signed-off-by: Leo Li <leoli@redhat.com>

* feat: add the unit test for addSubscription

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: mistakenly changed the name, now change it back

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: instead of using the hardcoded value for APIVersion, use it dynamically

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: Update the unit test to pass in the apiversion to the subscription and channel

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: the new edge should be from subscriber to the reply

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: the new edge should be from subscriber to the reply

Signed-off-by: Leo Li <leoli@redhat.com>

* fix: find the problem and correct the unit tests

Signed-off-by: Leo Li <leoli@redhat.com>

---------

Signed-off-by: Leo Li <leoli@redhat.com>
  • Loading branch information
Leo6Leo authored May 10, 2024
1 parent 72585c9 commit 1572967
Show file tree
Hide file tree
Showing 2 changed files with 1,221 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/graph/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,44 @@ func (g *Graph) AddTrigger(trigger eventingv1.Trigger) error {
return nil

}
func (g *Graph) AddSubscription(subscription messagingv1.Subscription) error {
channelRef := &duckv1.KReference{
Name: subscription.Spec.Channel.Name,
Namespace: subscription.Namespace,
APIVersion: subscription.Spec.Channel.APIVersion,
Kind: subscription.Spec.Channel.Kind,
}
channelDest := &duckv1.Destination{Ref: channelRef}
channel, ok := g.vertices[makeComparableDestination(channelDest)]

if !ok {
return fmt.Errorf("subscription refers to a non existent channel, can't add it to the graph")
}

subscriptionRef := &duckv1.KReference{
Name: subscription.Name,
Namespace: subscription.Namespace,
APIVersion: subscription.APIVersion,
Kind: "Subscription",
}
subscriptionDest := &duckv1.Destination{Ref: subscriptionRef}

to := g.getOrCreateVertex(subscription.Spec.Subscriber)
channel.AddEdge(to, subscriptionDest, NoTransform{}, false)

// If the subscription has a reply field set, there should be another Edge struct.
if subscription.Spec.Reply != nil {
reply := g.getOrCreateVertex(subscription.Spec.Reply)
to.AddEdge(reply, subscriptionDest, NoTransform{}, false)
}

// If the subscription has the deadLetterSink property set on the delivery field, then another Edge should be constructed.
if subscription.Spec.Delivery == nil || subscription.Spec.Delivery.DeadLetterSink == nil {
return nil
}
dls := g.getOrCreateVertex(subscription.Spec.Delivery.DeadLetterSink)
channel.AddEdge(dls, subscriptionDest, NoTransform{}, true)

return nil

}
Loading

0 comments on commit 1572967

Please sign in to comment.