github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/common/elasticsearch/elasticsearch.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  package elasticsearch
    15  
    16  import (
    17  	"fmt"
    18  	"net/url"
    19  
    20  	"github.com/golang/glog"
    21  	"github.com/olivere/elastic"
    22  	"github.com/pborman/uuid"
    23  )
    24  
    25  const (
    26  	ESIndex = "heapster"
    27  )
    28  
    29  // SaveDataFunc is a pluggable function to enforce limits on the object
    30  type SaveDataFunc func(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error
    31  
    32  type ElasticSearchConfig struct {
    33  	EsClient     *elastic.Client
    34  	Index        string
    35  	NeedAuthen   bool
    36  	EsUserName   string
    37  	EsUserSecret string
    38  	EsNodes      []string
    39  }
    40  
    41  // SaveDataIntoES save metrics and events to ES by using ES client
    42  func SaveDataIntoES(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error {
    43  	if indexName == "" || typeName == "" || sinkData == nil {
    44  		return nil
    45  	}
    46  	// Use the IndexExists service to check if a specified index exists.
    47  	exists, err := esClient.IndexExists(indexName).Do()
    48  	if err != nil {
    49  		return err
    50  	}
    51  	if !exists {
    52  		// Create a new index.
    53  		createIndex, err := esClient.CreateIndex(indexName).Do()
    54  		if err != nil {
    55  			return err
    56  		}
    57  		if !createIndex.Acknowledged {
    58  			return fmt.Errorf("failed to create Index in ES cluster: %s", err)
    59  		}
    60  	}
    61  	indexID := uuid.NewUUID()
    62  	_, err = esClient.Index().
    63  		Index(indexName).
    64  		Type(typeName).
    65  		Id(string(indexID)).
    66  		BodyJson(sinkData).
    67  		Do()
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return nil
    72  }
    73  
    74  func CreateElasticSearchConfig(uri *url.URL) (*ElasticSearchConfig, error) {
    75  
    76  	var esConfig ElasticSearchConfig
    77  	opts, err := url.ParseQuery(uri.RawQuery)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("failed to parser url's query string: %s", err)
    80  	}
    81  
    82  	// set the index for es,the default value is "heapster"
    83  	esConfig.Index = ESIndex
    84  	if len(opts["index"]) > 0 {
    85  		esConfig.Index = opts["index"][0]
    86  	}
    87  
    88  	// If the ES cluster needs authentication, the username and secret
    89  	// should be set in sink config.Else, set the Authenticate flag to false
    90  	esConfig.NeedAuthen = false
    91  	if len(opts["esUserName"]) > 0 && len(opts["esUserSecret"]) > 0 {
    92  		esConfig.EsUserName = opts["esUserName"][0]
    93  		esConfig.NeedAuthen = true
    94  	}
    95  
    96  	// set the URL endpoints of the ES's nodes. Notice that
    97  	// when sniffing is enabled, these URLs are used to initially sniff the
    98  	// cluster on startup.
    99  	if len(opts["nodes"]) < 1 {
   100  		return nil, fmt.Errorf("There is no node assigned for connecting ES cluster")
   101  	}
   102  	esConfig.EsNodes = append(esConfig.EsNodes, opts["nodes"]...)
   103  	glog.V(2).Infof("configing elasticsearch sink with ES's nodes - %v", esConfig.EsNodes)
   104  
   105  	var client *(elastic.Client)
   106  	if esConfig.NeedAuthen == false {
   107  		client, err = elastic.NewClient(elastic.SetURL(esConfig.EsNodes...))
   108  	} else {
   109  		client, err = elastic.NewClient(elastic.SetBasicAuth(esConfig.EsUserName, esConfig.EsUserSecret), elastic.SetURL(esConfig.EsNodes...))
   110  	}
   111  
   112  	if err != nil {
   113  		return nil, fmt.Errorf("failed to create ElasticSearch client: %v", err)
   114  	}
   115  	esConfig.EsClient = client
   116  	glog.V(2).Infof("elasticsearch sink configure successfully")
   117  	return &esConfig, nil
   118  }