Skip to content

Commit ae1d35b

Browse files
committed
Channels working
Signed-off-by: Ali Ok <aliok@redhat.com>
1 parent 4ff75fb commit ae1d35b

File tree

7 files changed

+106
-79
lines changed

7 files changed

+106
-79
lines changed

backends/pkg/eventmesh/v1/api.gen.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backends/pkg/eventmesh/v1/builder.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func BuildEventMesh(ctx context.Context, clientset versioned.Interface, dynamicC
5353
// map key: "<namespace>/<name>"
5454
brokerMap := make(map[string]*Broker)
5555
for _, cbr := range convertedBrokers {
56-
brokerMap[cbr.GetNamespacedName()] = cbr
56+
key := util.GKNamespacedName("eventing.knative.dev", "Broker", cbr.Namespace, cbr.Name)
57+
brokerMap[key] = cbr
5758
}
5859

5960
subscribableMap := make(map[string]*Subscribable)
@@ -69,31 +70,35 @@ func BuildEventMesh(ctx context.Context, clientset versioned.Interface, dynamicC
6970
return EventMesh{}, err
7071
}
7172

72-
// register the event types in the brokers
73+
// register the event types in the brokers and channels
7374
for _, et := range convertedEventTypes {
7475
if et.Reference != nil {
7576
if br, ok := brokerMap[*et.Reference]; ok {
7677
br.ProvidedEventTypes = append(br.ProvidedEventTypes, et.NamespacedName())
78+
} else if subscribable, ok := subscribableMap[*et.Reference]; ok {
79+
subscribable.ProvidedEventTypes = append(subscribable.ProvidedEventTypes, et.NamespacedName())
80+
} else {
81+
logger.Infow("Event type reference not found", "eventType", et.NamespacedName(), "reference", *et.Reference)
7782
}
7883
}
7984
}
8085

81-
// fetch the triggers we will process them later
82-
triggers, err := clientset.EventingV1().Triggers(metav1.NamespaceAll).List(context.Background(), metav1.ListOptions{})
83-
if err != nil {
84-
logger.Errorw("Error listing triggers", "error", err)
85-
return EventMesh{}, err
86-
}
87-
8886
// build a map for easier access to the ETs by their namespaced name.
8987
// we need this map when processing the triggers to find out ET definitions for the ET references
90-
// brokers provide.
88+
// brokers and channels provide.
9189
// map key: "<namespace>/<eventType.name>"
9290
etByNamespacedName := make(map[string]*EventType)
9391
for _, et := range convertedEventTypes {
9492
etByNamespacedName[et.NamespacedName()] = et
9593
}
9694

95+
// fetch the triggers we will process them later
96+
triggers, err := clientset.EventingV1().Triggers(metav1.NamespaceAll).List(context.Background(), metav1.ListOptions{})
97+
if err != nil {
98+
logger.Errorw("Error listing triggers", "error", err)
99+
return EventMesh{}, err
100+
}
101+
97102
for _, trigger := range triggers.Items {
98103
err := processTrigger(ctx, &trigger, brokerMap, etByNamespacedName, dynamicClient, logger)
99104
if err != nil {
@@ -110,7 +115,7 @@ func BuildEventMesh(ctx context.Context, clientset versioned.Interface, dynamicC
110115
}
111116

112117
for _, subscription := range subscriptions.Items {
113-
err := processSubscription(ctx, &subscription, subscribableMap, dynamicClient, logger)
118+
err := processSubscription(ctx, &subscription, subscribableMap, etByNamespacedName, dynamicClient, logger)
114119
if err != nil {
115120
logger.Errorw("Error processing subscription", "error", err)
116121
// do not stop the Backstage plugin from rendering the rest of the data, e.g. because
@@ -166,7 +171,7 @@ func processTrigger(ctx context.Context, trigger *eventingv1.Trigger, brokerMap
166171
logger.Errorw("Trigger has no broker", "namespace", trigger.Namespace, "trigger", trigger.Name)
167172
return nil
168173
}
169-
brokerRef := util.NamespacedName(trigger.Namespace, trigger.Spec.Broker)
174+
brokerRef := util.GKNamespacedName("eventing.knative.dev", "Broker", trigger.Namespace, trigger.Spec.Broker)
170175
if _, ok := brokerMap[brokerRef]; !ok {
171176
logger.Infow("Broker not found", "namespace", trigger.Namespace, "trigger", trigger.Name, "broker", trigger.Spec.Broker)
172177
return nil
@@ -182,7 +187,7 @@ func processTrigger(ctx context.Context, trigger *eventingv1.Trigger, brokerMap
182187
return nil
183188
}
184189

185-
func processSubscription(ctx context.Context, subscription *v1.Subscription, subscribableMap map[string]*Subscribable, dynamicClient dynamic.Interface, logger *zap.SugaredLogger) error {
190+
func processSubscription(ctx context.Context, subscription *v1.Subscription, subscribableMap map[string]*Subscribable, etByNamespacedName map[string]*EventType, dynamicClient dynamic.Interface, logger *zap.SugaredLogger) error {
186191
// if the subscription has no subscriber, we can skip it, there's no relation to show on Backstage side
187192
if subscription.Spec.Subscriber.Ref == nil {
188193
logger.Debugw("Subscription has no subscriber ref; cannot process this subscription", "namespace", subscription.Namespace, "subscription", subscription.Name)
@@ -209,6 +214,16 @@ func processSubscription(ctx context.Context, subscription *v1.Subscription, sub
209214
return nil
210215
}
211216

217+
eventTypes := subscribableMap[channelRef].ProvidedEventTypes
218+
logger.Infow("Collected provided event types", "namespace", subscription.Namespace, "subscription", subscription.Name, "channel", channel.Name, "eventTypes", eventTypes)
219+
220+
for _, eventType := range eventTypes {
221+
key := util.NamespacedName(subscription.Namespace, eventType)
222+
if et, ok := etByNamespacedName[key]; ok {
223+
et.ConsumedBy = append(et.ConsumedBy, subscriberBackstageId)
224+
}
225+
}
226+
212227
return nil
213228
}
214229

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package v1
22

33
import (
4-
"knative.dev/eventing/pkg/apis/eventing/v1beta2"
5-
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
6-
74
"knative.dev/backstage-plugins/backends/pkg/util"
5+
"knative.dev/eventing/pkg/apis/eventing/v1beta2"
86
)
97

108
// NamespacedName returns the name and namespace of the event type in the format "<namespace>/<name>"
@@ -21,6 +19,10 @@ func (et EventType) NamespacedType() string {
2119
// convertEventType converts a Knative Eventing EventType to a simplified representation that is easier to consume by the Backstage plugin.
2220
// see EventType.
2321
func convertEventType(et *v1beta2.EventType) EventType {
22+
reference := ""
23+
if et.Spec.Reference != nil {
24+
reference = util.GKNamespacedName(util.APIVersionToGroup(et.Spec.Reference.APIVersion), et.Spec.Reference.Kind, et.Namespace, et.Spec.Reference.Name)
25+
}
2426
return EventType{
2527
Name: et.Name,
2628
Namespace: et.Namespace,
@@ -31,39 +33,8 @@ func convertEventType(et *v1beta2.EventType) EventType {
3133
SchemaURL: util.ToStrPtrOrNil(et.Spec.Schema.String()),
3234
Labels: et.Labels,
3335
Annotations: util.FilterAnnotations(et.Annotations),
34-
Reference: util.ToStrPtrOrNil(util.NamespacedRefName(et.Spec.Reference)),
35-
// this field will be populated later on, when we have process the triggers
36-
ConsumedBy: make([]string, 0),
37-
}
38-
}
39-
40-
// convertEventType converts a Knative Eventing EventType to a simplified representation that is easier to consume by the Backstage plugin.
41-
// see EventType.
42-
func convertEventTypev1beta3(et *v1beta3.EventType) EventType {
43-
cet := EventType{
44-
Name: et.Name,
45-
Namespace: et.Namespace,
46-
Uid: string(et.UID),
47-
Description: util.ToStrPtrOrNil(et.Spec.Description),
48-
Labels: et.Labels,
49-
Annotations: util.FilterAnnotations(et.Annotations),
50-
Reference: util.ToStrPtrOrNil(util.NamespacedRefName(et.Spec.Reference)),
36+
Reference: util.ToStrPtrOrNil(reference),
5137
// this field will be populated later on, when we have process the triggers
5238
ConsumedBy: make([]string, 0),
5339
}
54-
55-
if len(et.Spec.Attributes) == 0 {
56-
return cet
57-
}
58-
59-
for _, attr := range et.Spec.Attributes {
60-
switch attr.Name {
61-
case "type": // TODO: any CE constant for these?
62-
cet.Type = attr.Value
63-
case "schemadata":
64-
cet.SchemaURL = util.ToStrPtrOrNil(attr.Value)
65-
}
66-
}
67-
68-
return cet
6940
}

backends/pkg/eventmesh/v1/server.gen.go

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backends/pkg/eventmesh/v1/subscribable.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ func convertSubscribable(gvr schema.GroupVersionResource, u *unstructured.Unstru
1515
Labels: u.GetLabels(),
1616
Group: gvr.Group,
1717
Kind: u.GetKind(),
18+
// this field will be populated later on
19+
ProvidedEventTypes: []string{},
1820
}
1921
}

backstage/plugins/knative-event-mesh-backend/src/providers/knativeEventMeshProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export type Subscribable = {
5252
annotations?:Record<string, string>;
5353
group:string;
5454
kind:string;
55+
providedEventTypes?:string[];
5556
};
5657

5758
type EventMesh = {
@@ -306,6 +307,7 @@ export class KnativeEventMeshProvider implements EntityProvider {
306307
lifecycle: this.env,
307308
system: SystemKnative,
308309
owner: OwnerKnative,
310+
providesApis: !subscribable.providedEventTypes ? [] : subscribable.providedEventTypes.map((eventType:string) => `api:${eventType}`),
309311
}
310312
}
311313
}

specs/event-mesh-v1.yaml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ components:
128128
description: Kubernetes API kind of the subscribable.
129129
format: string
130130
example: InMemoryChannel
131+
providedEventTypes:
132+
type: array
133+
items:
134+
type: string
135+
format: string
136+
description: List of event types provided by the subscribable.
137+
example: [ "my-namespace/my-event-type" ]
131138
required:
132139
- namespace
133140
- name
@@ -136,6 +143,7 @@ components:
136143
- annotations
137144
- group
138145
- kind
146+
- providedEventTypes
139147
Source:
140148
type: object
141149
description: Source is a simplified representation of a Knative Eventing Source that is easier to consume by the Backstage plugin.
@@ -251,10 +259,7 @@ components:
251259
description: Annotations of the event type. These are passed as is, except that are filtered out by the `FilterAnnotations` function.
252260
example: { "key": "value" }
253261
reference:
254-
type: string
255-
format: string
256-
description: Reference is the EventTypes's reference to a resource like a broker or a channel. It is in the format `<namespace>/<name>`.
257-
example: my-namespace/my-broker
262+
$ref: '#/components/schemas/GroupKindNamespacedName'
258263
consumedBy:
259264
type: array
260265
items:
@@ -271,6 +276,35 @@ components:
271276
- labels
272277
- annotations
273278
- consumedBy
279+
GroupKindNamespacedName:
280+
type: object
281+
description: GroupKindNamespacedName is a struct that holds the group, kind, namespace, and name of a Kubernetes resource.
282+
properties:
283+
group:
284+
type: string
285+
description: Kubernetes API group of the resource, without the version.
286+
format: string
287+
example: eventing.knative.dev
288+
kind:
289+
type: string
290+
description: Kubernetes API kind of the resource.
291+
format: string
292+
example: Broker
293+
namespace:
294+
type: string
295+
description: Namespace of the resource.
296+
format: string
297+
example: my-namespace
298+
name:
299+
type: string
300+
description: Name of the resource.
301+
format: string
302+
example: my-broker
303+
required:
304+
- group
305+
- kind
306+
- namespace
307+
- name
274308
EventMesh:
275309
type: object
276310
description: EventMesh is the top-level struct that holds the event mesh data. It's the struct that's serialized and sent to the Backstage plugin.

0 commit comments

Comments
 (0)