Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Version independent Ping method for ES #94

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ limitations under the License.

package elasticsearch

import "github.com/go-resty/resty/v2"

type Client struct {
ESClient
}

type ESRestyClient struct {
Client *resty.Client
Config *Config
}
88 changes: 88 additions & 0 deletions elasticsearch/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net"
Expand All @@ -36,6 +37,7 @@ import (
esv7 "github.com/elastic/go-elasticsearch/v7"
esv8 "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/go-resty/resty/v2"
osv1 "github.com/opensearch-project/opensearch-go"
osapiv1 "github.com/opensearch-project/opensearch-go/opensearchapi"
osv2 "github.com/opensearch-project/opensearch-go/v2"
Expand Down Expand Up @@ -412,6 +414,92 @@ func (o *KubeDBClientBuilder) GetElasticClient() (*Client, error) {
return nil, fmt.Errorf("unknown database version: %s", o.db.Spec.Version)
}

type Config struct {
host string
api string
transport *http.Transport
}

func (o *KubeDBClientBuilder) GetElasticRestyClient() (*ESRestyClient, error) {
config := Config{
host: o.ServiceURL(),
api: "/_cluster/health?pretty",
transport: &http.Transport{
IdleConnTimeout: time.Second * 3,
DialContext: (&net.Dialer{
Timeout: time.Second * 30,
}).DialContext,
},
}

var authSecret core.Secret
var username, password string
if !o.db.Spec.DisableSecurity && o.db.Spec.AuthSecret != nil {
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: o.db.Namespace, Name: o.db.Spec.AuthSecret.Name}, &authSecret)
if err != nil {
return nil, errors.Errorf("Failed to get auth secret with %s", err)
}

if value, ok := authSecret.Data[core.BasicAuthUsernameKey]; ok {
username = string(value)
} else {
klog.Errorf("Failed for secret: %s/%s, username is missing", authSecret.Namespace, authSecret.Name)
return nil, errors.New("username is missing")
}

if value, ok := authSecret.Data[core.BasicAuthPasswordKey]; ok {
password = string(value)
} else {
klog.Errorf("Failed for secret: %s/%s, password is missing", authSecret.Namespace, authSecret.Name)
return nil, errors.New("password is missing")
}
}

defaultTlsConfig, err := o.getDefaultTLSConfig()
if err != nil {
klog.Errorf("Failed to get default tls config: %v", err)
}
config.transport.TLSClientConfig = defaultTlsConfig
newClient := resty.New()
newClient.SetTransport(config.transport).SetScheme(o.db.GetConnectionScheme()).SetBaseURL(config.host)
newClient.SetHeader("Accept", "application/json")
newClient.SetBasicAuth(username, password)
newClient.SetTimeout(time.Second * 30)
return &ESRestyClient{
Client: newClient,
Config: &config,
}, nil
}

func (client *ESRestyClient) Ping() (string, error) {
req := client.Client.R().SetDoNotParseResponse(true)
res, err := req.Get(client.Config.api)
if res != nil {
if res.StatusCode() != 200 {
klog.Error("stauscode is not 200")
return "", errors.New("statuscode is not 200")
}
} else if res == nil {
return "", errors.New("response can not be nil")
}
if err != nil {
klog.Error(err, res.StatusCode(), "Failed to send http request")
return "", err
}
body := res.RawBody()
responseBody := make(map[string]interface{})
if err := json.NewDecoder(body).Decode(&responseBody); err != nil {
return "", fmt.Errorf("failed to deserialize the response: %v", err)
}
if val, ok := responseBody["status"]; ok {
if strValue, ok := val.(string); ok {
return strValue, nil
}
return "", errors.New("failed to convert response to string")
}
return "", errors.New("status is missing")
}

func (o *KubeDBClientBuilder) getDefaultTLSConfig() (*tls.Config, error) {
var crt tls.Certificate
var clientCA, rootCA *x509.CertPool
Expand Down
Loading