From b20c89b226e7adfca7ec3c236685e683751bb39b Mon Sep 17 00:00:00 2001 From: sober Date: Fri, 10 Apr 2020 11:10:56 +0800 Subject: [PATCH 1/3] feat: When kube-eventer write data into InfluxDB will become more detial --- sinks/influxdb/influxdb.go | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/sinks/influxdb/influxdb.go b/sinks/influxdb/influxdb.go index 97d64cd4..d393a677 100755 --- a/sinks/influxdb/influxdb.go +++ b/sinks/influxdb/influxdb.go @@ -28,6 +28,7 @@ import ( influxdb "github.com/influxdata/influxdb/client" kube_api "k8s.io/api/core/v1" "k8s.io/klog" + jsoniter "github.com/json-iterator/go" ) type influxdbSink struct { @@ -37,12 +38,39 @@ type influxdbSink struct { dbExists bool } +// eventStruct influxDB field more detial. +type eventStruct struct{ + kind string + evenType string + nameSpace string + podName string + workloadName string + reason string + message string + firstTimestamp string + lastTimestamp string +} + const ( eventMeasurementName = "log/events" // Event special tags eventUID = "uid" // Value Field name valueField = "value" + + /* ---------------------------------*/ + // Kubernetes Event const + kubeKind = "kind" + evenType = "type" + nameSpace = "nameSpace" + podName = "podName" + workloadName = "workloadName" + eventReason = "reason" + eventMessage = "message" + firstTimestamp = "firstTimestamp" + lastTimestamp = "lastTimestamp" + /* -------------------------------- */ + // Event special tags dbNotFoundError = "database not found" @@ -90,17 +118,52 @@ func eventToPointWithFields(event *kube_api.Event) (*influxdb.Point, error) { return &point, nil } +// getWorkloadName get workload name, deployment is no problem +func getWorkloadName(podName string) string{ + s := strings.Split(podName,"-") + n := len(s) - 2 + return strings.Join(s[:n],"-") +} + +// selectEventData Select kubernetes events data from value +func selectEventData(value string) eventStruct{ + var eventData eventStruct + data := []byte(value) + eventData.evenType = jsoniter.Get(data,"type").ToString() + eventData.kind = jsoniter.Get(data,"involvedObject","kind").ToString() + eventData.nameSpace = jsoniter.Get(data,"involvedObject","namespace").ToString() + eventData.podName = jsoniter.Get(data,"involvedObject","name").ToString() + eventData.workloadName = getWorkloadName(eventData.podName) + eventData.reason = jsoniter.Get(data,"reason").ToString() + eventData.message = jsoniter.Get(data,"message").ToString() + eventData.firstTimestamp = jsoniter.Get(data,"firstTimestamp").ToString() + eventData.lastTimestamp = jsoniter.Get(data,"lastTimestamp").ToString() + return eventData +} + + +// eventToPoint make influxdb point from kubernetes event data func eventToPoint(event *kube_api.Event) (*influxdb.Point, error) { value, err := getEventValue(event) if err != nil { return nil, err } + eventData := selectEventData(value) point := influxdb.Point{ Measurement: eventMeasurementName, Time: event.LastTimestamp.Time.UTC(), Fields: map[string]interface{}{ valueField: value, + kubeKind: eventData.kind, + evenType: eventData.evenType, + nameSpace: eventData.nameSpace, + podName: eventData.podName, + workloadName: eventData.workloadName, + eventReason: eventData.reason, + eventMessage: eventData.message, + firstTimestamp: eventData.firstTimestamp, + lastTimestamp: eventData.lastTimestamp, }, Tags: map[string]string{ eventUID: string(event.UID), From 28b11162056cbb4371590a49bd38bc40a79a75ce Mon Sep 17 00:00:00 2001 From: sober Date: Fri, 10 Apr 2020 12:45:29 +0800 Subject: [PATCH 2/3] docs: Modify notes --- sinks/influxdb/influxdb.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sinks/influxdb/influxdb.go b/sinks/influxdb/influxdb.go index d393a677..f86cf93b 100755 --- a/sinks/influxdb/influxdb.go +++ b/sinks/influxdb/influxdb.go @@ -58,7 +58,6 @@ const ( // Value Field name valueField = "value" - /* ---------------------------------*/ // Kubernetes Event const kubeKind = "kind" evenType = "type" @@ -69,7 +68,6 @@ const ( eventMessage = "message" firstTimestamp = "firstTimestamp" lastTimestamp = "lastTimestamp" - /* -------------------------------- */ // Event special tags dbNotFoundError = "database not found" @@ -120,9 +118,9 @@ func eventToPointWithFields(event *kube_api.Event) (*influxdb.Point, error) { // getWorkloadName get workload name, deployment is no problem func getWorkloadName(podName string) string{ - s := strings.Split(podName,"-") - n := len(s) - 2 - return strings.Join(s[:n],"-") + splPodName := strings.Split(podName,"-") + effectiveLen := len(splPodName) - 2 + return strings.Join(splPodName[:effectiveLen],"-") } // selectEventData Select kubernetes events data from value From 040c22a18c7115c68f6b9902bdac6236184b263f Mon Sep 17 00:00:00 2001 From: sober Date: Fri, 10 Apr 2020 13:44:19 +0800 Subject: [PATCH 3/3] fix: Delete workloadName message --- sinks/influxdb/influxdb.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sinks/influxdb/influxdb.go b/sinks/influxdb/influxdb.go index f86cf93b..ddce8150 100755 --- a/sinks/influxdb/influxdb.go +++ b/sinks/influxdb/influxdb.go @@ -44,7 +44,6 @@ type eventStruct struct{ evenType string nameSpace string podName string - workloadName string reason string message string firstTimestamp string @@ -63,7 +62,6 @@ const ( evenType = "type" nameSpace = "nameSpace" podName = "podName" - workloadName = "workloadName" eventReason = "reason" eventMessage = "message" firstTimestamp = "firstTimestamp" @@ -116,12 +114,6 @@ func eventToPointWithFields(event *kube_api.Event) (*influxdb.Point, error) { return &point, nil } -// getWorkloadName get workload name, deployment is no problem -func getWorkloadName(podName string) string{ - splPodName := strings.Split(podName,"-") - effectiveLen := len(splPodName) - 2 - return strings.Join(splPodName[:effectiveLen],"-") -} // selectEventData Select kubernetes events data from value func selectEventData(value string) eventStruct{ @@ -131,7 +123,6 @@ func selectEventData(value string) eventStruct{ eventData.kind = jsoniter.Get(data,"involvedObject","kind").ToString() eventData.nameSpace = jsoniter.Get(data,"involvedObject","namespace").ToString() eventData.podName = jsoniter.Get(data,"involvedObject","name").ToString() - eventData.workloadName = getWorkloadName(eventData.podName) eventData.reason = jsoniter.Get(data,"reason").ToString() eventData.message = jsoniter.Get(data,"message").ToString() eventData.firstTimestamp = jsoniter.Get(data,"firstTimestamp").ToString() @@ -157,7 +148,6 @@ func eventToPoint(event *kube_api.Event) (*influxdb.Point, error) { evenType: eventData.evenType, nameSpace: eventData.nameSpace, podName: eventData.podName, - workloadName: eventData.workloadName, eventReason: eventData.reason, eventMessage: eventData.message, firstTimestamp: eventData.firstTimestamp,