|
| 1 | +package sinks |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + opensearch "github.com/opensearch-project/opensearch-go" |
| 9 | + "github.com/opsgenie/kubernetes-event-exporter/pkg/kube" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "io/ioutil" |
| 12 | + "net/http" |
| 13 | + "regexp" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +type OpenSearchConfig struct { |
| 19 | + // Connection specific |
| 20 | + Hosts []string `yaml:"hosts"` |
| 21 | + Username string `yaml:"username"` |
| 22 | + Password string `yaml:"password"` |
| 23 | + // Indexing preferences |
| 24 | + UseEventID bool `yaml:"useEventID"` |
| 25 | + // DeDot all labels and annotations in the event. For both the event and the involvedObject |
| 26 | + DeDot bool `yaml:"deDot"` |
| 27 | + Index string `yaml:"index"` |
| 28 | + IndexFormat string `yaml:"indexFormat"` |
| 29 | + Type string `yaml:"type"` |
| 30 | + TLS TLS `yaml:"tls"` |
| 31 | + Layout map[string]interface{} `yaml:"layout"` |
| 32 | +} |
| 33 | + |
| 34 | +func NewOpenSearch(cfg *OpenSearchConfig) (*OpenSearch, error) { |
| 35 | + |
| 36 | + tlsClientConfig, err := setupTLS(&cfg.TLS) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to setup TLS: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + client, err := opensearch.NewClient(opensearch.Config{ |
| 42 | + Addresses: cfg.Hosts, |
| 43 | + Username: cfg.Username, |
| 44 | + Password: cfg.Password, |
| 45 | + Transport: &http.Transport{ |
| 46 | + TLSClientConfig: tlsClientConfig, |
| 47 | + }, |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return &OpenSearch{ |
| 54 | + client: client, |
| 55 | + cfg: cfg, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +type OpenSearch struct { |
| 60 | + client *opensearch.Client |
| 61 | + cfg *OpenSearchConfig |
| 62 | +} |
| 63 | + |
| 64 | +var osRegex = regexp.MustCompile(`(?s){(.*)}`) |
| 65 | + |
| 66 | +func osFormatIndexName(pattern string, when time.Time) string { |
| 67 | + m := osRegex.FindAllStringSubmatchIndex(pattern, -1) |
| 68 | + current := 0 |
| 69 | + var builder strings.Builder |
| 70 | + |
| 71 | + for i := 0; i < len(m); i++ { |
| 72 | + pair := m[i] |
| 73 | + |
| 74 | + builder.WriteString(pattern[current:pair[0]]) |
| 75 | + builder.WriteString(when.Format(pattern[pair[0]+1 : pair[1]-1])) |
| 76 | + current = pair[1] |
| 77 | + } |
| 78 | + |
| 79 | + builder.WriteString(pattern[current:]) |
| 80 | + |
| 81 | + return builder.String() |
| 82 | +} |
| 83 | + |
| 84 | +func (e *OpenSearch) Send(ctx context.Context, ev *kube.EnhancedEvent) error { |
| 85 | + var toSend []byte |
| 86 | + |
| 87 | + if e.cfg.DeDot { |
| 88 | + de := ev.DeDot() |
| 89 | + ev = &de |
| 90 | + } |
| 91 | + if e.cfg.Layout != nil { |
| 92 | + res, err := convertLayoutTemplate(e.cfg.Layout, ev) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + toSend, err = json.Marshal(res) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + } else { |
| 102 | + toSend = ev.ToJSON() |
| 103 | + } |
| 104 | + |
| 105 | + var index string |
| 106 | + if len(e.cfg.IndexFormat) > 0 { |
| 107 | + now := time.Now() |
| 108 | + index = osFormatIndexName(e.cfg.IndexFormat, now) |
| 109 | + } else { |
| 110 | + index = e.cfg.Index |
| 111 | + } |
| 112 | + |
| 113 | + req := esapi.IndexRequest{ |
| 114 | + Body: bytes.NewBuffer(toSend), |
| 115 | + Index: index, |
| 116 | + } |
| 117 | + |
| 118 | + // This should not be used for clusters with ES8.0+. |
| 119 | + if len(e.cfg.Type) > 0 { |
| 120 | + req.DocumentType = e.cfg.Type |
| 121 | + } |
| 122 | + |
| 123 | + if e.cfg.UseEventID { |
| 124 | + req.DocumentID = string(ev.UID) |
| 125 | + } |
| 126 | + |
| 127 | + resp, err := req.Do(ctx, e.client) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + defer resp.Body.Close() |
| 133 | + if resp.StatusCode > 399 { |
| 134 | + rb, err := ioutil.ReadAll(resp.Body) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + log.Error().Msgf("Indexing failed: %s", string(rb)) |
| 139 | + } |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func (e *OpenSearch) Close() { |
| 144 | + // No-op |
| 145 | +} |
0 commit comments