37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
elasticsearch.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package utils
import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"path"
"time"
"github.com/pkg/errors"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
)
type elasticsearchTestWrap struct {
*v3.ElasticsearchConfig
}
func (w *elasticsearchTestWrap) TestReachable(dial dialer.Dialer, includeSendTestLog bool) error {
url, err := url.Parse(w.Endpoint)
if err != nil {
return errors.Wrapf(err, "couldn't parse url %s", w.Endpoint)
}
isTLS := url.Scheme == "https"
var tlsConfig *tls.Config
if isTLS {
tlsConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, w.ClientKeyPass, w.SSLVersion, url.Hostname(), w.SSLVerify)
if err != nil {
return err
}
}
if !includeSendTestLog {
conn, err := newTCPConn(dial, url.Host, tlsConfig, true)
if err != nil {
return err
}
conn.Close()
return nil
}
index := getIndex(w.DateFormat, w.IndexPrefix)
url.Path = path.Join(url.Path, index, "/container_log")
req, err := http.NewRequest(http.MethodPost, url.String(), bytes.NewReader(httpTestData))
if err != nil {
return errors.Wrap(err, "create request failed")
}
req.Header.Set("Content-Type", "application/json")
if w.AuthUserName != "" && w.AuthPassword != "" {
req.SetBasicAuth(w.AuthUserName, w.AuthPassword)
}
return testReachableHTTP(dial, req, tlsConfig)
}
func getIndex(dateFormat, prefix string) string {
var index string
today := time.Now()
switch dateFormat {
case "YYYY":
index = fmt.Sprintf("%s-%04d", prefix, today.Year())
case "YYYY-MM":
index = fmt.Sprintf("%s-%04d-%02d", prefix, today.Year(), today.Month())
case "YYYY-MM-DD":
index = fmt.Sprintf("%s-%04d-%02d-%02d", prefix, today.Year(), today.Month(), today.Day())
}
return index
}
func GetDateFormat(dateformat string) string {
ToRealMap := map[string]string{
"YYYY-MM-DD": "%Y-%m-%d",
"YYYY-MM": "%Y-%m",
"YYYY": "%Y",
}
if _, ok := ToRealMap[dateformat]; ok {
return ToRealMap[dateformat]
}
return "%Y-%m-%d"
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.5-rc7

搜索帮助

0d507c66 1850385 C8b1a773 1850385