This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
/
driver.go
96 lines (84 loc) · 2.44 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
// 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 kafka
import (
"encoding/json"
"net/url"
"sync"
"time"
"github.com/golang/glog"
kube_api "k8s.io/client-go/pkg/api/v1"
kafka_common "k8s.io/heapster/common/kafka"
event_core "k8s.io/heapster/events/core"
"k8s.io/heapster/metrics/core"
)
type KafkaSinkPoint struct {
EventValue interface{}
EventTimestamp time.Time
EventTags map[string]string
}
type kafkaSink struct {
kafka_common.KafkaClient
sync.RWMutex
}
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) (*KafkaSinkPoint, error) {
value, err := getEventValue(event)
if err != nil {
return nil, err
}
point := KafkaSinkPoint{
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 *kafkaSink) ExportEvents(eventBatch *event_core.EventBatch) {
sink.Lock()
defer sink.Unlock()
for _, event := range eventBatch.Events {
point, err := eventToPoint(event)
if err != nil {
glog.Warningf("Failed to convert event to point: %v", err)
}
err = sink.ProduceKafkaMessage(*point)
if err != nil {
glog.Errorf("Failed to produce event message: %s", err)
}
}
}
func NewKafkaSink(uri *url.URL) (event_core.EventSink, error) {
client, err := kafka_common.NewKafkaClient(uri, kafka_common.EventsTopic)
if err != nil {
return nil, err
}
return &kafkaSink{
KafkaClient: client,
}, nil
}