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 logging for call to ES #4958

Merged
merged 3 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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,
key,
koneko096 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
40 changes: 21 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,7 @@ func TestGetInfo(t *testing.T) {
func TestClient_Equal(t *testing.T) {
dummyEndpoint := "es-url"
dummyUser := BasicAuth{Name: "user", Password: "password"}
dummyNamespaceName := types.NamespacedName{}
koneko096 marked this conversation as resolved.
Show resolved Hide resolved
createCert := func() *x509.Certificate {
ca, err := certificates.NewSelfSignedCA(certificates.CABuilderOptions{})
require.NoError(t, err)
Expand All @@ -367,62 +369,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
7 changes: 6 additions & 1 deletion pkg/controller/elasticsearch/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
controller "sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -283,7 +284,11 @@ 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))
namespaceName := types.NamespacedName{
Name: d.ES.Name,
Namespace: d.ES.Namespace,
}
return esclient.NewElasticsearchClient(d.OperatorParameters.Dialer, namespaceName, url, user, v, caCerts, esclient.Timeout(d.ES))
koneko096 marked this conversation as resolved.
Show resolved Hide resolved
}

// warnUnsupportedDistro sends an event of type warning if the Elasticsearch Docker image is not a supported
Expand Down