/
helpers.go
188 lines (162 loc) · 6.23 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package subscribed
import (
"fmt"
"strings"
eventingv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/rest"
eventingv1alpha1 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha1"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/informers"
)
var (
GVR = schema.GroupVersionResource{
Version: eventingv1alpha2.GroupVersion.Version,
Group: eventingv1alpha2.GroupVersion.Group,
Resource: "subscriptions",
}
GVRV1alpha1 = schema.GroupVersionResource{
Version: eventingv1alpha1.GroupVersion.Version,
Group: eventingv1alpha1.GroupVersion.Group,
Resource: "subscriptions",
}
)
// ConvertRuntimeObjToSubscriptionV1alpha1 converts a runtime.Object to a v1alpha1 version of Subscription object
// by converting to unstructured in between.
func ConvertRuntimeObjToSubscriptionV1alpha1(sObj runtime.Object) (*eventingv1alpha1.Subscription, error) {
sub := &eventingv1alpha1.Subscription{}
if subUnstructured, ok := sObj.(*unstructured.Unstructured); ok {
err := runtime.DefaultUnstructuredConverter.FromUnstructured(subUnstructured.Object, sub)
if err != nil {
return nil, err
}
}
return sub, nil
}
// ConvertRuntimeObjToSubscription converts a runtime.Object to a Subscription object
// by converting to unstructured in between.
func ConvertRuntimeObjToSubscription(sObj runtime.Object) (*eventingv1alpha2.Subscription, error) {
sub := &eventingv1alpha2.Subscription{}
if subUnstructured, ok := sObj.(*unstructured.Unstructured); ok {
err := runtime.DefaultUnstructuredConverter.FromUnstructured(subUnstructured.Object, sub)
if err != nil {
return nil, err
}
}
return sub, nil
}
// GenerateSubscriptionInfFactory generates DynamicSharedInformerFactory for Subscription.
func GenerateSubscriptionInfFactory(k8sConfig *rest.Config) dynamicinformer.DynamicSharedInformerFactory {
subDynamicClient := dynamic.NewForConfigOrDie(k8sConfig)
dFilteredSharedInfFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(subDynamicClient,
informers.DefaultResyncPeriod,
v1.NamespaceAll,
nil,
)
dFilteredSharedInfFactory.ForResource(GVR)
return dFilteredSharedInfFactory
}
// ConvertEventsMapToSlice converts a map of Events to a slice of Events.
func ConvertEventsMapToSlice(eventsMap map[Event]bool) []Event {
result := make([]Event, 0)
for k := range eventsMap {
result = append(result, k)
}
return result
}
// AddUniqueEventsToResult returns a map of unique Events which also contains the events eventsSubSet.
func AddUniqueEventsToResult(eventsSubSet []Event, uniqEvents map[Event]bool) map[Event]bool {
if len(uniqEvents) == 0 {
uniqEvents = make(map[Event]bool)
}
for _, event := range eventsSubSet {
if !uniqEvents[event] {
uniqEvents[event] = true
}
}
return uniqEvents
}
// FilterEventTypeVersions returns a slice of Events:
// if the event source matches the appName for typeMatching standard
// if the <eventTypePrefix>.<appName> is present in the eventType for typeMatching exact.
func FilterEventTypeVersions(eventTypePrefix, appName string, subscription *eventingv1alpha2.Subscription) []Event {
events := make([]Event, 0)
prefixAndAppName := fmt.Sprintf("%s.%s.", eventTypePrefix, appName)
for _, eventType := range subscription.Spec.Types {
if subscription.Spec.TypeMatching == eventingv1alpha2.TypeMatchingExact {
// in case of type matching exact, we have app name as a part of event type
if strings.HasPrefix(eventType, prefixAndAppName) {
eventTypeVersion := strings.ReplaceAll(eventType, prefixAndAppName, "")
event := buildEvent(eventTypeVersion)
events = append(events, event)
}
} else {
// in case of type matching standard, the source must be app name
if appName == subscription.Spec.Source {
event := buildEvent(eventType)
events = append(events, event)
}
}
}
return events
}
// it receives event and type version, e.g. order.created.v1 and returns `{Name: order.created, Version: v1}`.
func buildEvent(eventTypeAndVersion string) Event {
lastDotIndex := strings.LastIndex(eventTypeAndVersion, ".")
eventName := eventTypeAndVersion[:lastDotIndex]
eventVersion := eventTypeAndVersion[lastDotIndex+1:]
return Event{
Name: eventName,
Version: eventVersion,
}
}
// FilterEventTypeVersionsV1alpha1 returns a slice of Events for v1alpha1 version of Subscription resource:
// 1. if the eventType matches the format: <eventTypePrefix><appName>.<event-name>.<version>
// E.g. sap.kyma.custom.varkes.order.created.v0
// 2. if the eventSource matches BEBNamespace name.
func FilterEventTypeVersionsV1alpha1(eventTypePrefix, bebNs, appName string, filters *eventingv1alpha1.BEBFilters) []Event {
events := make([]Event, 0)
if filters == nil {
return events
}
for _, filter := range filters.Filters {
if filter == nil {
continue
}
prefix := preparePrefix(eventTypePrefix, appName)
if filter.EventSource == nil || filter.EventType == nil {
continue
}
// TODO revisit the filtration logic as part of https://github.com/kyma-project/kyma/issues/10761
// filter by event-source if exists
if len(strings.TrimSpace(filter.EventSource.Value)) > 0 && !strings.EqualFold(filter.EventSource.Value, bebNs) {
continue
}
if !strings.HasPrefix(filter.EventType.Value, prefix) {
continue
}
// remove the prefix
eventTypeVersion := strings.TrimPrefix(filter.EventType.Value, prefix)
eventTypeVersionArr := strings.Split(eventTypeVersion, ".")
// join all segments except the last to form the eventType
eventType := strings.Join(eventTypeVersionArr[:len(eventTypeVersionArr)-1], ".")
// the last segment is the version
version := eventTypeVersionArr[len(eventTypeVersionArr)-1]
event := Event{
Name: eventType,
Version: version,
}
events = append(events, event)
}
return events
}
func preparePrefix(eventTypePrefix string, appName string) string {
if len(strings.TrimSpace(eventTypePrefix)) == 0 {
return strings.ToLower(fmt.Sprintf("%s.", appName))
}
return strings.ToLower(fmt.Sprintf("%s.%s.", eventTypePrefix, appName))
}