This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
/
elasticsearch.go
151 lines (127 loc) · 3.16 KB
/
elasticsearch.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
package sinks
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
"github.com/rs/zerolog/log"
)
type ElasticsearchConfig struct {
// Connection specific
Hosts []string `yaml:"hosts"`
Username string `yaml:"username"`
Password string `yaml:"password"`
CloudID string `yaml:"cloudID"`
APIKey string `yaml:"apiKey"`
// Indexing preferences
UseEventID bool `yaml:"useEventID"`
// DeDot all labels and annotations in the event. For both the event and the involvedObject
DeDot bool `yaml:"deDot"`
Index string `yaml:"index"`
IndexFormat string `yaml:"indexFormat"`
Type string `yaml:"type"`
TLS TLS `yaml:"tls"`
Layout map[string]interface{} `yaml:"layout"`
}
func NewElasticsearch(cfg *ElasticsearchConfig) (*Elasticsearch, error) {
tlsClientConfig, err := setupTLS(&cfg.TLS)
if err != nil {
return nil, fmt.Errorf("failed to setup TLS: %w", err)
}
client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: cfg.Hosts,
Username: cfg.Username,
Password: cfg.Password,
CloudID: cfg.CloudID,
APIKey: cfg.APIKey,
Transport: &http.Transport{
TLSClientConfig: tlsClientConfig,
},
})
if err != nil {
return nil, err
}
return &Elasticsearch{
client: client,
cfg: cfg,
}, nil
}
type Elasticsearch struct {
client *elasticsearch.Client
cfg *ElasticsearchConfig
}
var regex = regexp.MustCompile(`(?s){(.*)}`)
func formatIndexName(pattern string, when time.Time) string {
m := regex.FindAllStringSubmatchIndex(pattern, -1)
current := 0
var builder strings.Builder
for i := 0; i < len(m); i++ {
pair := m[i]
builder.WriteString(pattern[current:pair[0]])
builder.WriteString(when.Format(pattern[pair[0]+1 : pair[1]-1]))
current = pair[1]
}
builder.WriteString(pattern[current:])
return builder.String()
}
func (e *Elasticsearch) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
var toSend []byte
if e.cfg.DeDot {
de := ev.DeDot()
ev = &de
}
if e.cfg.Layout != nil {
res, err := convertLayoutTemplate(e.cfg.Layout, ev)
if err != nil {
return err
}
toSend, err = json.Marshal(res)
if err != nil {
return err
}
} else {
toSend = ev.ToJSON()
}
var index string
if len(e.cfg.IndexFormat) > 0 {
now := time.Now()
index = formatIndexName(e.cfg.IndexFormat, now)
} else {
index = e.cfg.Index
}
req := esapi.IndexRequest{
Body: bytes.NewBuffer(toSend),
Index: index,
}
// This should not be used for clusters with ES8.0+.
if len(e.cfg.Type) > 0 {
req.DocumentType = e.cfg.Type
}
if e.cfg.UseEventID {
req.DocumentID = string(ev.UID)
}
resp, err := req.Do(ctx, e.client)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
rb, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Error().Msgf("Indexing failed: %s", string(rb))
}
return nil
}
func (e *Elasticsearch) Close() {
// No-op
}