-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (114 loc) · 3.94 KB
/
main.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
package main
//dependencies
import (
"context"
"encoding/json"
"log"
"os"
"strconv"
"time"
"github.com/dapr/go-sdk/client"
)
// code
var (
PUBSUB_NAME = "pubsub"
TOPIC_NAME = "audit"
BROKERNAME = os.Getenv("BROKERNAME")
)
type Client struct {
client client.Client
}
// Dapr represents driver for interacting with pub sub using dapr.
type Dapr struct {
// Array of clients to talk to different endpoints
client []Client
}
type PubsubMsg struct {
Key string `json:"key,omitempty"`
ID string `json:"id,omitempty"`
Details interface{} `json:"details,omitempty"`
EventType string `json:"eventType,omitempty"`
Group string `json:"group,omitempty"`
Version string `json:"version,omitempty"`
Kind string `json:"kind,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Message string `json:"message,omitempty"`
EnforcementAction string `json:"enforcementAction,omitempty"`
ConstraintAnnotations map[string]string `json:"constraintAnnotations,omitempty"`
ResourceGroup string `json:"resourceGroup,omitempty"`
ResourceAPIVersion string `json:"resourceAPIVersion,omitempty"`
ResourceKind string `json:"resourceKind,omitempty"`
ResourceNamespace string `json:"resourceNamespace,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
ResourceLabels map[string]string `json:"resourceLabels,omitempty"`
// Additional Metadata for benchmarking
BrokerName string `json:"brokerName,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}
func main() {
r := &Dapr{}
tmpc, err := client.NewClient()
if err != nil {
panic(err)
}
newClient := Client{
client: tmpc,
}
r.client = append(r.client, newClient)
log.Println("client initialized")
r.Send()
time.Sleep(300 * time.Hour)
}
func getObj(i string) interface{} {
now := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
return PubsubMsg{
Key: i,
ID: now,
Details: map[string]interface{}{"missing_labels": []interface{}{"test"}},
EventType: "violation_audited",
Group: "constraints.gatekeeper.sh",
Version: "v1beta1",
Kind: "K8sRequiredLabels",
Name: "pod-must-have-test",
Namespace: "",
Message: "you must provide labels: {\"test\"}",
EnforcementAction: "deny",
ConstraintAnnotations: map[string]string(nil),
ResourceGroup: "",
ResourceAPIVersion: "v1",
ResourceKind: "Pod",
ResourceNamespace: "nginx",
ResourceName: "dywuperf-deployment-10kpods-69bd64c867-h2wdx",
ResourceLabels: map[string]string{"app": "dywuperf-app-100kpods", "pod-template-hash": "69bd64c867"},
Timestamp: now,
BrokerName: BROKERNAME}
}
func (r *Dapr) Send() {
value := os.Getenv("NUMBER")
log.Printf("sending %s messages", value)
total, err := strconv.Atoi(value)
if err != nil {
log.Fatalf("error getting number: %v", err)
total = 100000
}
ctx := context.Background()
start_time := time.Now()
log.Println("starting publish")
for i := 0; i < total; i++ {
test := getObj(strconv.Itoa(i))
jsonData, err := json.Marshal(test)
if err != nil {
log.Fatalf("error marshaling data: %v", err)
}
for _, c := range r.client {
//Using Dapr SDK to publish a topic
// log.Println("Published data: " + string(jsonData))
if err := c.client.PublishEvent(ctx, PUBSUB_NAME, TOPIC_NAME, jsonData); err != nil {
panic(err)
}
}
}
end_time := time.Now()
log.Printf("total time it took %v", end_time.Sub(start_time))
}