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
125 lines (111 loc) · 3.68 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
117
118
119
120
121
122
123
124
125
// 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"
"github.com/golang/glog"
kube_api "k8s.io/api/core/v1"
esCommon "k8s.io/heapster/common/elasticsearch"
event_core "k8s.io/heapster/events/core"
"k8s.io/heapster/metrics/core"
)
const (
typeName = "events"
)
// SaveDataFunc is a pluggable function to enforce limits on the object
type SaveDataFunc func(date time.Time, sinkData []interface{}) error
type elasticSearchSink struct {
esSvc esCommon.ElasticSearchService
saveData SaveDataFunc
flushData func() error
sync.RWMutex
}
type EsSinkPoint struct {
Count interface{}
Metadata interface{}
InvolvedObject interface{}
Source interface{}
FirstOccurrenceTimestamp time.Time
LastOccurrenceTimestamp time.Time
Message string
Reason string
Type string
EventTags map[string]string
}
func eventToPoint(event *kube_api.Event, clusterName string) (*EsSinkPoint, error) {
point := EsSinkPoint{
FirstOccurrenceTimestamp: event.FirstTimestamp.Time.UTC(),
LastOccurrenceTimestamp: event.LastTimestamp.Time.UTC(),
Message: event.Message,
Reason: event.Reason,
Type: event.Type,
Count: event.Count,
Metadata: event.ObjectMeta,
InvolvedObject: event.InvolvedObject,
Source: event.Source,
EventTags: map[string]string{
"eventID": string(event.UID),
"cluster_name": clusterName,
},
}
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, sink.esSvc.ClusterName)
if err != nil {
glog.Warningf("Failed to convert event to point: %v", err)
}
err = sink.saveData(point.LastOccurrenceTimestamp, []interface{}{*point})
if err != nil {
glog.Warningf("Failed to export data to ElasticSearch sink: %v", err)
}
}
err := sink.flushData()
if err != nil {
glog.Warningf("Failed to flushing data to ElasticSearch sink: %v", 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
esSvc, err := esCommon.CreateElasticSearchService(uri)
if err != nil {
glog.Warning("Failed to config ElasticSearch")
return nil, err
}
esSink.esSvc = *esSvc
esSink.saveData = func(date time.Time, sinkData []interface{}) error {
return esSvc.SaveData(date, typeName, sinkData)
}
esSink.flushData = func() error {
return esSvc.FlushData()
}
glog.V(2).Info("ElasticSearch sink setup successfully")
return &esSink, nil
}