This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
driver.go
116 lines (101 loc) · 3.17 KB
/
driver.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elasticsearch
import (
"net/url"
"sync"
"time"
"encoding/json"
"github.com/golang/glog"
"gopkg.in/olivere/elastic.v3"
esCommon "k8s.io/heapster/common/elasticsearch"
event_core "k8s.io/heapster/events/core"
"k8s.io/heapster/metrics/core"
kube_api "k8s.io/kubernetes/pkg/api"
)
const (
typeName = "events"
)
// LimitFunc is a pluggable function to enforce limits on the object
type SaveDataFunc func(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error
type elasticSearchSink struct {
saveDataFunc SaveDataFunc
esConfig esCommon.ElasticSearchConfig
sync.RWMutex
}
type EsSinkPoint struct {
EventValue interface{}
EventTimestamp time.Time
EventTags map[string]string
}
// Generate point value for event
func getEventValue(event *kube_api.Event) (string, error) {
// TODO: check whether indenting is required.
bytes, err := json.MarshalIndent(event, "", " ")
if err != nil {
return "", err
}
return string(bytes), nil
}
func eventToPoint(event *kube_api.Event) (*EsSinkPoint, error) {
value, err := getEventValue(event)
if err != nil {
return nil, err
}
point := EsSinkPoint{
EventTimestamp: event.LastTimestamp.Time.UTC(),
EventValue: value,
EventTags: map[string]string{
"eventID": string(event.UID),
},
}
if event.InvolvedObject.Kind == "Pod" {
point.EventTags[core.LabelPodId.Key] = string(event.InvolvedObject.UID)
point.EventTags[core.LabelPodName.Key] = event.InvolvedObject.Name
}
point.EventTags[core.LabelHostname.Key] = event.Source.Host
return &point, nil
}
func (sink *elasticSearchSink) ExportEvents(eventBatch *event_core.EventBatch) {
sink.Lock()
defer sink.Unlock()
for _, event := range eventBatch.Events {
point, err := eventToPoint(event)
if err != nil {
glog.Info("Failed to convert event to point: %v", err)
}
err = sink.saveDataFunc(sink.esConfig.EsClient, sink.esConfig.Index, typeName, point)
if err != nil {
glog.V(3).Info("Failed to export data to ElasticSearch sink: ", err)
}
}
}
func (sink *elasticSearchSink) Name() string {
return "ElasticSearch Sink"
}
func (sink *elasticSearchSink) Stop() {
// nothing needs to be done.
}
func NewElasticSearchSink(uri *url.URL) (event_core.EventSink, error) {
var esSink elasticSearchSink
elasticsearchConfig, err := esCommon.CreateElasticSearchConfig(uri)
if err != nil {
glog.V(2).Infof("Failed to config ElasticSearch")
return nil, err
}
esSink.esConfig = *elasticsearchConfig
esSink.saveDataFunc = esCommon.SaveDataIntoES
glog.V(2).Infof("ElasticSearch sink setup successfully")
return &esSink, nil
}