Skip to content

Commit

Permalink
Add debug log for HTTP requests to ES (elastic#4958)
Browse files Browse the repository at this point in the history
  • Loading branch information
koneko096 authored and fantapsody committed Jan 3, 2023
1 parent 41eef65 commit e54af81
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
1 change: 1 addition & 0 deletions pkg/controller/autoscaling/elasticsearch/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func newElasticsearchClient(
}
return esclient.NewElasticsearchClient(
dialer,
k8s.ExtractNamespacedName(&es),
url,
esclient.BasicAuth{
Name: user.ControllerUserName,
Expand Down
9 changes: 9 additions & 0 deletions pkg/controller/elasticsearch/client/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
ulog "github.com/elastic/cloud-on-k8s/pkg/utils/log"
"github.com/elastic/cloud-on-k8s/pkg/utils/stringsutil"
"github.com/hashicorp/go-multierror"
"k8s.io/apimachinery/pkg/types"
)

var log = ulog.Log.WithName("elasticsearch-client")
Expand All @@ -24,6 +25,7 @@ type baseClient struct {
User BasicAuth
HTTP *http.Client
Endpoint string
es types.NamespacedName
caCerts []*x509.Certificate
version version.Version
}
Expand Down Expand Up @@ -67,6 +69,13 @@ func (c *baseClient) doRequest(context context.Context, request *http.Request) (
withContext.SetBasicAuth(c.User.Name, c.User.Password)
}

log.V(1).Info(
"Elasticsearch HTTP request",
"method", request.Method,
"url", request.URL.Redacted(),
"namespace", c.es.Namespace,
"es_name", c.es.Name,
)
response, err := c.HTTP.Do(withContext)
if err != nil {
return response, newDecoratedHTTPError(request, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/elasticsearch/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/elastic/cloud-on-k8s/pkg/controller/common/annotation"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/version"
"github.com/elastic/cloud-on-k8s/pkg/utils/net"
"k8s.io/apimachinery/pkg/types"
)

const (
Expand Down Expand Up @@ -121,6 +122,7 @@ func formatAsSeconds(d time.Duration) string {
// If dialer is not nil, it will be used to create new TCP connections
func NewElasticsearchClient(
dialer net.Dialer,
es types.NamespacedName,
esURL string,
esUser BasicAuth,
v version.Version,
Expand All @@ -132,6 +134,7 @@ func NewElasticsearchClient(
User: esUser,
caCerts: caCerts,
HTTP: common.HTTPClient(dialer, caCerts, timeout),
es: es,
}
return versioned(base, v)
}
43 changes: 24 additions & 19 deletions pkg/controller/elasticsearch/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestParseShards(t *testing.T) {
Expand Down Expand Up @@ -350,6 +351,10 @@ func TestGetInfo(t *testing.T) {
func TestClient_Equal(t *testing.T) {
dummyEndpoint := "es-url"
dummyUser := BasicAuth{Name: "user", Password: "password"}
dummyNamespaceName := types.NamespacedName{
Namespace: "ns",
Name: "es",
}
createCert := func() *x509.Certificate {
ca, err := certificates.NewSelfSignedCA(certificates.CABuilderOptions{})
require.NoError(t, err)
Expand All @@ -367,62 +372,62 @@ func TestClient_Equal(t *testing.T) {
}{
{
name: "c1 and c2 equals",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: true,
},
{
name: "c2 nil",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: nil,
want: false,
},
{
name: "different endpoint",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, "another-endpoint", dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, "another-endpoint", dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: false,
},
{
name: "different user",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, BasicAuth{Name: "user", Password: "another-password"}, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, BasicAuth{Name: "user", Password: "another-password"}, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: false,
},
{
name: "different CA cert",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, []*x509.Certificate{createCert()}, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, []*x509.Certificate{createCert()}, Timeout(esv1.Elasticsearch{})),
want: false,
},
{
name: "different CA certs length",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, []*x509.Certificate{createCert(), createCert()}, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, []*x509.Certificate{createCert(), createCert()}, Timeout(esv1.Elasticsearch{})),
want: false,
},
{
name: "different dialers are not taken into consideration",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(portforward.NewForwardingDialer(), dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(portforward.NewForwardingDialer(), dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: true,
},
{
name: "different versions",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: false,
},
{
name: "same versions",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: true,
},
{
name: "one has a version",
c1: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyEndpoint, dummyUser, version.Version{}, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v7, dummyCACerts, Timeout(esv1.Elasticsearch{})),
c2: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, version.Version{}, dummyCACerts, Timeout(esv1.Elasticsearch{})),
want: false,
},
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/controller/elasticsearch/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,15 @@ func (d *defaultDriver) newElasticsearchClient(
caCerts []*x509.Certificate,
) esclient.Client {
url := services.ElasticsearchURL(d.ES, state.CurrentPodsByPhase[corev1.PodRunning])
return esclient.NewElasticsearchClient(d.OperatorParameters.Dialer, url, user, v, caCerts, esclient.Timeout(d.ES))
return esclient.NewElasticsearchClient(
d.OperatorParameters.Dialer,
k8s.ExtractNamespacedName(&d.ES),
url,
user,
v,
caCerts,
esclient.Timeout(d.ES),
)
}

// warnUnsupportedDistro sends an event of type warning if the Elasticsearch Docker image is not a supported
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/test/elasticsearch/checks_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ func CheckHTTPConnectivityWithCA(es esv1.Elasticsearch, k *test.K8sClient, caCer

for _, p := range reconcile.AvailableElasticsearchNodes(pods) {
url := services.ElasticsearchPodURL(p)
esClient := client.NewElasticsearchClient(dialer, url, user, v, caCert, client.Timeout(es))
esClient := client.NewElasticsearchClient(
dialer,
k8s.ExtractNamespacedName(&es),
url,
user,
v,
caCert,
client.Timeout(es),
)
_, err := esClient.GetClusterInfo(context.Background())
if err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/test/elasticsearch/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func NewElasticsearchClientWithUser(es esv1.Elasticsearch, k *test.K8sClient, us
if err != nil {
return nil, err
}
esClient := client.NewElasticsearchClient(dialer, inClusterURL, user, v, caCert, client.Timeout(es))
esClient := client.NewElasticsearchClient(
dialer,
k8s.ExtractNamespacedName(&es),
inClusterURL,
user,
v,
caCert,
client.Timeout(es),
)
return esClient, nil
}

0 comments on commit e54af81

Please sign in to comment.