github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/elasticsearch/driver.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package elasticsearch
    16  
    17  import (
    18  	"net/url"
    19  	"sync"
    20  	"time"
    21  
    22  	"github.com/golang/glog"
    23  	kube_api "k8s.io/api/core/v1"
    24  	esCommon "k8s.io/heapster/common/elasticsearch"
    25  	event_core "k8s.io/heapster/events/core"
    26  	"k8s.io/heapster/metrics/core"
    27  )
    28  
    29  const (
    30  	typeName = "events"
    31  )
    32  
    33  // SaveDataFunc is a pluggable function to enforce limits on the object
    34  type SaveDataFunc func(date time.Time, sinkData []interface{}) error
    35  
    36  type elasticSearchSink struct {
    37  	esSvc     esCommon.ElasticSearchService
    38  	saveData  SaveDataFunc
    39  	flushData func() error
    40  	sync.RWMutex
    41  }
    42  
    43  type EsSinkPoint struct {
    44  	Count                    interface{}
    45  	Metadata                 interface{}
    46  	InvolvedObject           interface{}
    47  	Source                   interface{}
    48  	FirstOccurrenceTimestamp time.Time
    49  	LastOccurrenceTimestamp  time.Time
    50  	Message                  string
    51  	Reason                   string
    52  	Type                     string
    53  	EventTags                map[string]string
    54  }
    55  
    56  func eventToPoint(event *kube_api.Event, clusterName string) (*EsSinkPoint, error) {
    57  	point := EsSinkPoint{
    58  		FirstOccurrenceTimestamp: event.FirstTimestamp.Time.UTC(),
    59  		LastOccurrenceTimestamp:  event.LastTimestamp.Time.UTC(),
    60  		Message:                  event.Message,
    61  		Reason:                   event.Reason,
    62  		Type:                     event.Type,
    63  		Count:                    event.Count,
    64  		Metadata:                 event.ObjectMeta,
    65  		InvolvedObject:           event.InvolvedObject,
    66  		Source:                   event.Source,
    67  		EventTags: map[string]string{
    68  			"eventID":      string(event.UID),
    69  			"cluster_name": clusterName,
    70  		},
    71  	}
    72  	if event.InvolvedObject.Kind == "Pod" {
    73  		point.EventTags[core.LabelPodId.Key] = string(event.InvolvedObject.UID)
    74  		point.EventTags[core.LabelPodName.Key] = event.InvolvedObject.Name
    75  	}
    76  	point.EventTags[core.LabelHostname.Key] = event.Source.Host
    77  	return &point, nil
    78  }
    79  
    80  func (sink *elasticSearchSink) ExportEvents(eventBatch *event_core.EventBatch) {
    81  	sink.Lock()
    82  	defer sink.Unlock()
    83  	for _, event := range eventBatch.Events {
    84  		point, err := eventToPoint(event, sink.esSvc.ClusterName)
    85  		if err != nil {
    86  			glog.Warningf("Failed to convert event to point: %v", err)
    87  		}
    88  		err = sink.saveData(point.LastOccurrenceTimestamp, []interface{}{*point})
    89  		if err != nil {
    90  			glog.Warningf("Failed to export data to ElasticSearch sink: %v", err)
    91  		}
    92  	}
    93  	err := sink.flushData()
    94  	if err != nil {
    95  		glog.Warningf("Failed to flushing data to ElasticSearch sink: %v", err)
    96  	}
    97  }
    98  
    99  func (sink *elasticSearchSink) Name() string {
   100  	return "ElasticSearch Sink"
   101  }
   102  
   103  func (sink *elasticSearchSink) Stop() {
   104  	// nothing needs to be done.
   105  }
   106  
   107  func NewElasticSearchSink(uri *url.URL) (event_core.EventSink, error) {
   108  	var esSink elasticSearchSink
   109  	esSvc, err := esCommon.CreateElasticSearchService(uri)
   110  	if err != nil {
   111  		glog.Warning("Failed to config ElasticSearch")
   112  		return nil, err
   113  	}
   114  
   115  	esSink.esSvc = *esSvc
   116  	esSink.saveData = func(date time.Time, sinkData []interface{}) error {
   117  		return esSvc.SaveData(date, typeName, sinkData)
   118  	}
   119  	esSink.flushData = func() error {
   120  		return esSvc.FlushData()
   121  	}
   122  
   123  	glog.V(2).Info("ElasticSearch sink setup successfully")
   124  	return &esSink, nil
   125  }