func CreateElasticSearchConfig(uri *url.URL) (*ElasticSearchConfig, error) { var esConfig ElasticSearchConfig opts, err := url.ParseQuery(uri.RawQuery) if err != nil { return nil, fmt.Errorf("failed to parser url's query string: %s", err) } // set the index for es,the default value is "heapster" esConfig.Index = ESIndex if len(opts["index"]) > 0 { esConfig.Index = opts["index"][0] } // If the ES cluster needs authentication, the username and secret // should be set in sink config.Else, set the Authenticate flag to false esConfig.NeedAuthen = false if len(opts["esUserName"]) > 0 && len(opts["esUserSecret"]) > 0 { esConfig.EsUserName = opts["esUserName"][0] esConfig.NeedAuthen = true } // set the URL endpoints of the ES's nodes. Notice that // when sniffing is enabled, these URLs are used to initially sniff the // cluster on startup. if len(opts["nodes"]) < 1 { return nil, fmt.Errorf("There is no node assigned for connecting ES cluster") } esConfig.EsNodes = append(esConfig.EsNodes, opts["nodes"]...) glog.V(2).Infof("configing elasticsearch sink with ES's nodes - %v", esConfig.EsNodes) var client *(elastic.Client) if esConfig.NeedAuthen == false { client, err = elastic.NewClient(elastic.SetURL(esConfig.EsNodes...)) } else { client, err = elastic.NewClient(elastic.SetBasicAuth(esConfig.EsUserName, esConfig.EsUserSecret), elastic.SetURL(esConfig.EsNodes...)) } if err != nil { return nil, fmt.Errorf("failed to create ElasticSearch client: %v", err) } esConfig.EsClient = client glog.V(2).Infof("elasticsearch sink configure successfully") return &esConfig, nil }
func TestCreateElasticSearchConfig(t *testing.T) { url, err := url.Parse("?nodes=https://foo.com:20468&nodes=https://bar.com:20468&esUserName=test&esUserSecret=password&maxRetries=10&startupHealthcheckTimeout=30&sniff=false&healthCheck=false") if err != nil { t.Fatalf("Error when parsing URL: %s", err.Error()) } config, err := CreateElasticSearchConfig(url) if err != nil { t.Fatalf("Error when creating config: %s", err.Error()) } expectedClient, err := elastic.NewClient( elastic.SetURL("https://foo.com:20468", "https://bar.com:20468"), elastic.SetBasicAuth("test", "password"), elastic.SetMaxRetries(10), elastic.SetHealthcheckTimeoutStartup(30*time.Second), elastic.SetSniff(false), elastic.SetHealthcheck(false)) if err != nil { t.Fatalf("Error when creating client: %s", err.Error()) } actualClientRefl := reflect.ValueOf(config.EsClient).Elem() expectedClientRefl := reflect.ValueOf(expectedClient).Elem() if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() { t.Fatalf("basicAuthUsername is not equal") } if actualClientRefl.FieldByName("basicAuthUsername").String() != expectedClientRefl.FieldByName("basicAuthUsername").String() { t.Fatalf("basicAuthUsername is not equal") } if actualClientRefl.FieldByName("maxRetries").Int() != expectedClientRefl.FieldByName("maxRetries").Int() { t.Fatalf("maxRetries is not equal") } if actualClientRefl.FieldByName("healthcheckTimeoutStartup").Int() != expectedClientRefl.FieldByName("healthcheckTimeoutStartup").Int() { t.Fatalf("healthcheckTimeoutStartup is not equal") } if actualClientRefl.FieldByName("snifferEnabled").Bool() != expectedClientRefl.FieldByName("snifferEnabled").Bool() { t.Fatalf("snifferEnabled is not equal") } if actualClientRefl.FieldByName("healthcheckEnabled").Bool() != expectedClientRefl.FieldByName("healthcheckEnabled").Bool() { t.Fatalf("healthcheckEnabled is not equal") } }
// CreateElasticSearchConfig creates an ElasticSearch configuration struct // which contains an ElasticSearch client for later use func CreateElasticSearchConfig(uri *url.URL) (*ElasticSearchConfig, error) { var esConfig ElasticSearchConfig opts, err := url.ParseQuery(uri.RawQuery) if err != nil { return nil, fmt.Errorf("failed to parser url's query string: %s", err) } // set the index for es,the default value is "heapster" esConfig.Index = ESIndex if len(opts["index"]) > 0 { esConfig.Index = opts["index"][0] } // Set the URL endpoints of the ES's nodes. Notice that when sniffing is // enabled, these URLs are used to initially sniff the cluster on startup. if len(opts["nodes"]) < 1 { return nil, fmt.Errorf("There is no node assigned for connecting ES cluster") } startupFns := []elastic.ClientOptionFunc{elastic.SetURL(opts["nodes"]...)} // If the ES cluster needs authentication, the username and secret // should be set in sink config.Else, set the Authenticate flag to false if len(opts["esUserName"]) > 0 && len(opts["esUserSecret"]) > 0 { startupFns = append(startupFns, elastic.SetBasicAuth(opts["esUserName"][0], opts["esUserSecret"][0])) } if len(opts["maxRetries"]) > 0 { maxRetries, err := strconv.Atoi(opts["maxRetries"][0]) if err != nil { return nil, fmt.Errorf("Failed to parse URL's maxRetries value into an int") } startupFns = append(startupFns, elastic.SetMaxRetries(maxRetries)) } if len(opts["healthCheck"]) > 0 { healthCheck, err := strconv.ParseBool(opts["healthCheck"][0]) if err != nil { return nil, fmt.Errorf("Failed to parse URL's healthCheck value into a bool") } startupFns = append(startupFns, elastic.SetHealthcheck(healthCheck)) } if len(opts["sniff"]) > 0 { sniff, err := strconv.ParseBool(opts["sniff"][0]) if err != nil { return nil, fmt.Errorf("Failed to parse URL's sniff value into a bool") } startupFns = append(startupFns, elastic.SetSniff(sniff)) } if len(opts["startupHealthcheckTimeout"]) > 0 { timeout, err := time.ParseDuration(opts["startupHealthcheckTimeout"][0] + "s") if err != nil { return nil, fmt.Errorf("Failed to parse URL's startupHealthcheckTimeout: %s", err.Error()) } startupFns = append(startupFns, elastic.SetHealthcheckTimeoutStartup(timeout)) } esConfig.EsClient, err = elastic.NewClient(startupFns...) if err != nil { return nil, fmt.Errorf("failed to create ElasticSearch client: %v", err) } glog.V(2).Infof("elasticsearch sink configure successfully") return &esConfig, nil }