Skip to content

Commit 723f2c3

Browse files
committed
Remove old notification infra code
Signed-off-by: Johanan Liebermann <jliebermann@microsoft.com>
1 parent e1fdc67 commit 723f2c3

2 files changed

Lines changed: 0 additions & 216 deletions

File tree

pkg/i2gw/notifications/notifications.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ limitations under the License.
1717
package notifications
1818

1919
import (
20-
"fmt"
2120
"strings"
22-
"sync"
2321

2422
"sigs.k8s.io/controller-runtime/pkg/client"
25-
26-
"github.com/olekukonko/tablewriter"
2723
)
2824

29-
func init() {
30-
NotificationAggr = NotificationAggregator{Notifications: map[string][]Notification{}}
31-
}
32-
3325
const (
3426
InfoNotification MessageType = "INFO"
3527
WarningNotification MessageType = "WARNING"
@@ -44,49 +36,6 @@ type Notification struct {
4436
CallingObjects []client.Object
4537
}
4638

47-
type NotificationAggregator struct {
48-
mutex sync.Mutex
49-
Notifications map[string][]Notification
50-
}
51-
52-
var NotificationAggr NotificationAggregator
53-
54-
// DispatchNotification is used to send a notification to the NotificationAggregator
55-
func (na *NotificationAggregator) DispatchNotification(notification Notification, ProviderName string) {
56-
na.mutex.Lock()
57-
na.Notifications[ProviderName] = append(na.Notifications[ProviderName], notification)
58-
na.mutex.Unlock()
59-
}
60-
61-
// CreateNotificationTables takes all generated notifications and returns a map[string]string
62-
// that displays the notifications in a tabular format based on provider
63-
func (na *NotificationAggregator) CreateNotificationTables() map[string]string {
64-
notificationTablesMap := make(map[string]string)
65-
66-
na.mutex.Lock()
67-
defer na.mutex.Unlock()
68-
69-
for provider, msgs := range na.Notifications {
70-
providerTable := strings.Builder{}
71-
72-
t := tablewriter.NewWriter(&providerTable)
73-
t.SetHeader([]string{"Message Type", "Notification", "Calling Object"})
74-
t.SetColWidth(200)
75-
t.SetRowLine(true)
76-
77-
for _, n := range msgs {
78-
row := []string{string(n.Type), n.Message, convertObjectsToStr(n.CallingObjects)}
79-
t.Append(row)
80-
}
81-
82-
providerTable.WriteString(fmt.Sprintf("Notifications from %v:\n", strings.ToUpper(provider)))
83-
t.Render()
84-
notificationTablesMap[provider] = providerTable.String()
85-
}
86-
87-
return notificationTablesMap
88-
}
89-
9039
func convertObjectsToStr(ob []client.Object) string {
9140
var sb strings.Builder
9241

pkg/i2gw/notifications/notifications_test.go

Lines changed: 0 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -17,181 +17,16 @@ limitations under the License.
1717
package notifications
1818

1919
import (
20-
"sync"
2120
"testing"
2221

2322
"github.com/stretchr/testify/assert"
2423

25-
istioclientv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
2624
networkingv1 "k8s.io/api/networking/v1"
2725
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2826
"sigs.k8s.io/controller-runtime/pkg/client"
2927
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3028
)
3129

32-
func TestCreateNotificationsTables(t *testing.T) {
33-
testCases := []struct {
34-
name string
35-
notifications map[string][]Notification
36-
wantedTables map[string]string
37-
}{
38-
{
39-
name: "no notifications",
40-
notifications: map[string][]Notification{},
41-
wantedTables: map[string]string{},
42-
},
43-
{
44-
name: "single provider with one notification",
45-
notifications: map[string][]Notification{
46-
"provider1": {
47-
{
48-
Type: WarningNotification,
49-
Message: "warning message",
50-
},
51-
},
52-
},
53-
wantedTables: map[string]string{
54-
"provider1": `Notifications from PROVIDER1:
55-
+--------------+-----------------+----------------+
56-
| MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
57-
+--------------+-----------------+----------------+
58-
| WARNING | warning message | |
59-
+--------------+-----------------+----------------+
60-
`,
61-
},
62-
},
63-
{
64-
name: "two providers each with two notifications",
65-
notifications: map[string][]Notification{
66-
"istio": {
67-
{
68-
Type: WarningNotification,
69-
Message: "a very long warning notification generated by VirtualService ns/test from the provider istio",
70-
CallingObjects: []client.Object{
71-
&istioclientv1beta1.VirtualService{
72-
TypeMeta: metav1.TypeMeta{
73-
Kind: "VirtualService",
74-
},
75-
ObjectMeta: metav1.ObjectMeta{
76-
Name: "test",
77-
Namespace: "ns",
78-
},
79-
},
80-
},
81-
},
82-
{
83-
Type: InfoNotification,
84-
Message: "successfully converted VirtualService ns/test to HTTPRoute",
85-
CallingObjects: []client.Object{
86-
&istioclientv1beta1.VirtualService{
87-
TypeMeta: metav1.TypeMeta{
88-
Kind: "VirtualService",
89-
},
90-
ObjectMeta: metav1.ObjectMeta{
91-
Name: "test",
92-
Namespace: "ns",
93-
},
94-
},
95-
},
96-
},
97-
},
98-
"kong": {
99-
{
100-
Type: InfoNotification,
101-
Message: "informational notification from kong provider",
102-
CallingObjects: []client.Object{
103-
&networkingv1.Ingress{
104-
TypeMeta: metav1.TypeMeta{
105-
Kind: "Ingress",
106-
},
107-
ObjectMeta: metav1.ObjectMeta{
108-
Name: "ingress-kong",
109-
Namespace: "default",
110-
},
111-
},
112-
},
113-
},
114-
{
115-
Type: ErrorNotification,
116-
Message: "error notification genereated by kong",
117-
},
118-
},
119-
},
120-
wantedTables: map[string]string{
121-
"istio": `Notifications from ISTIO:
122-
+--------------+----------------------------------------------------------------------------------------------+-------------------------+
123-
| MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
124-
+--------------+----------------------------------------------------------------------------------------------+-------------------------+
125-
| WARNING | a very long warning notification generated by VirtualService ns/test from the provider istio | VirtualService: ns/test |
126-
+--------------+----------------------------------------------------------------------------------------------+-------------------------+
127-
| INFO | successfully converted VirtualService ns/test to HTTPRoute | VirtualService: ns/test |
128-
+--------------+----------------------------------------------------------------------------------------------+-------------------------+
129-
`,
130-
"kong": `Notifications from KONG:
131-
+--------------+-----------------------------------------------+-------------------------------+
132-
| MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
133-
+--------------+-----------------------------------------------+-------------------------------+
134-
| INFO | informational notification from kong provider | Ingress: default/ingress-kong |
135-
+--------------+-----------------------------------------------+-------------------------------+
136-
| ERROR | error notification genereated by kong | |
137-
+--------------+-----------------------------------------------+-------------------------------+
138-
`,
139-
},
140-
},
141-
}
142-
143-
for _, tc := range testCases {
144-
tc := tc
145-
t.Run(tc.name, func(t *testing.T) {
146-
na := NotificationAggregator{
147-
Notifications: tc.notifications,
148-
}
149-
result := na.CreateNotificationTables()
150-
for provider, table := range result {
151-
assert.Equal(t, tc.wantedTables[provider], table)
152-
}
153-
})
154-
}
155-
}
156-
157-
// TestNotificationAggregatorRace checks for data races in NotificationAggregator when accessed
158-
// concurrently.
159-
func TestNotificationAggregatorRace(_ *testing.T) {
160-
na := NotificationAggregator{Notifications: map[string][]Notification{}}
161-
162-
providers := []string{"provider1", "provider2", "provider3", "provider4"}
163-
var wg sync.WaitGroup
164-
165-
// Start multiple goroutines that dispatch notifications concurrently.
166-
for _, provider := range providers {
167-
wg.Add(1)
168-
go func(p string) {
169-
for range 100 {
170-
na.DispatchNotification(
171-
Notification{
172-
Type: WarningNotification,
173-
Message: "concurrent warning",
174-
},
175-
p,
176-
)
177-
}
178-
wg.Done()
179-
}(provider)
180-
}
181-
182-
// Concurrently read from the aggregator while writes are happening.
183-
wg.Add(1)
184-
go func() {
185-
for range 100 {
186-
_ = na.CreateNotificationTables()
187-
}
188-
wg.Done()
189-
}()
190-
191-
// Wait for all goroutines to complete.
192-
wg.Wait()
193-
}
194-
19530
func TestConvertObjectsToStr(t *testing.T) {
19631
testCases := []struct {
19732
name string

0 commit comments

Comments
 (0)