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

Use Elasticsearch readiness port #7847

Merged
merged 24 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
430 changes: 427 additions & 3 deletions NOTICE.txt

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion docs/reference/dependencies.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This page lists the third-party dependencies used to build {n}.
| link:https://github.com/davecgh/go-spew[$$github.com/davecgh/go-spew$$] | v1.1.2-0.20180830191138-d8f796af33cc | ISC
| link:https://github.com/elastic/go-ucfg[$$github.com/elastic/go-ucfg$$] | v0.8.8 | Apache-2.0
| link:https://github.com/ghodss/yaml[$$github.com/ghodss/yaml$$] | v1.0.0 | MIT
| link:https://github.com/gkampitakis/go-snaps[$$github.com/gkampitakis/go-snaps$$] | v0.5.4 | MIT
| link:https://github.com/go-logr/logr[$$github.com/go-logr/logr$$] | v1.4.1 | Apache-2.0
| link:https://github.com/go-test/deep[$$github.com/go-test/deep$$] | v1.1.0 | MIT
| link:https://github.com/gobuffalo/flect[$$github.com/gobuffalo/flect$$] | v1.0.2 | MIT
Expand Down Expand Up @@ -93,6 +94,8 @@ This page lists the third-party dependencies used to build {n}.
| link:https://github.com/fatih/color[$$github.com/fatih/color$$] | v1.16.0 | MIT
| link:https://github.com/frankban/quicktest[$$github.com/frankban/quicktest$$] | v1.14.6 | MIT
| link:https://github.com/fsnotify/fsnotify[$$github.com/fsnotify/fsnotify$$] | v1.7.0 | BSD-3-Clause
| link:https://github.com/gkampitakis/ciinfo[$$github.com/gkampitakis/ciinfo$$] | v0.3.0 | MIT
| link:https://github.com/gkampitakis/go-diff[$$github.com/gkampitakis/go-diff$$] | v1.3.2 | MIT
| link:https://github.com/go-jose/go-jose[$$github.com/go-jose/go-jose/v4$$] | v4.0.1 | Apache-2.0
| link:https://github.com/go-logr/zapr[$$github.com/go-logr/zapr$$] | v1.3.0 | Apache-2.0
| link:https://github.com/go-openapi/jsonpointer[$$github.com/go-openapi/jsonpointer$$] | v0.21.0 | Apache-2.0
Expand Down Expand Up @@ -126,6 +129,7 @@ This page lists the third-party dependencies used to build {n}.
| link:https://github.com/kr/pretty[$$github.com/kr/pretty$$] | v0.3.1 | MIT
| link:https://github.com/kr/text[$$github.com/kr/text$$] | v0.2.0 | MIT
| link:https://github.com/mailru/easyjson[$$github.com/mailru/easyjson$$] | v0.7.7 | MIT
| link:https://github.com/maruel/natural[$$github.com/maruel/natural$$] | v1.1.1 | Apache-2.0
| link:https://github.com/mattn/go-colorable[$$github.com/mattn/go-colorable$$] | v0.1.13 | MIT
| link:https://github.com/mattn/go-isatty[$$github.com/mattn/go-isatty$$] | v0.0.20 | MIT
| link:https://github.com/mitchellh/copystructure[$$github.com/mitchellh/copystructure$$] | v1.0.0 | MIT
Expand All @@ -147,7 +151,7 @@ This page lists the third-party dependencies used to build {n}.
| link:https://github.com/prashantv/gostub[$$github.com/prashantv/gostub$$] | v1.1.0 | MIT
| link:https://github.com/prometheus/client_model[$$github.com/prometheus/client_model$$] | v0.6.1 | Apache-2.0
| link:https://github.com/prometheus/procfs[$$github.com/prometheus/procfs$$] | v0.14.0 | Apache-2.0
| link:https://github.com/rogpeppe/go-internal[$$github.com/rogpeppe/go-internal$$] | v1.11.0 | BSD-3-Clause
| link:https://github.com/rogpeppe/go-internal[$$github.com/rogpeppe/go-internal$$] | v1.12.0 | BSD-3-Clause
| link:https://github.com/ryanuber/go-glob[$$github.com/ryanuber/go-glob$$] | v1.0.0 | MIT
| link:https://github.com/sagikazarmark/locafero[$$github.com/sagikazarmark/locafero$$] | v0.4.0 | MIT
| link:https://github.com/sagikazarmark/slog-shim[$$github.com/sagikazarmark/slog-shim$$] | v0.1.0 | BSD-3-Clause
Expand All @@ -157,6 +161,10 @@ This page lists the third-party dependencies used to build {n}.
| link:https://github.com/spf13/afero[$$github.com/spf13/afero$$] | v1.11.0 | Apache-2.0
| link:https://github.com/spf13/cast[$$github.com/spf13/cast$$] | v1.6.0 | MIT
| link:https://github.com/subosito/gotenv[$$github.com/subosito/gotenv$$] | v1.6.0 | MIT
| link:https://github.com/tidwall/gjson[$$github.com/tidwall/gjson$$] | v1.17.0 | MIT
| link:https://github.com/tidwall/match[$$github.com/tidwall/match$$] | v1.1.1 | MIT
| link:https://github.com/tidwall/pretty[$$github.com/tidwall/pretty$$] | v1.2.1 | MIT
| link:https://github.com/tidwall/sjson[$$github.com/tidwall/sjson$$] | v1.2.5 | MIT
| link:https://github.com/vbatts/tar-split[$$github.com/vbatts/tar-split$$] | v0.11.3 | BSD-3-Clause
| link:https://go.elastic.co/fastjson[$$go.elastic.co/fastjson$$] | v1.3.0 | MIT
| link:https://go.uber.org/goleak[$$go.uber.org/goleak$$] | v1.3.0 | MIT
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/elastic/go-ucfg v0.8.8
github.com/ghodss/yaml v1.0.0
github.com/gkampitakis/go-snaps v0.5.4
github.com/go-logr/logr v1.4.1
github.com/go-test/deep v1.1.0
github.com/gobuffalo/flect v1.0.2
Expand Down Expand Up @@ -66,6 +67,8 @@ require (
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gkampitakis/ciinfo v0.3.0 // indirect
github.com/gkampitakis/go-diff v1.3.2 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
Expand All @@ -92,7 +95,10 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/maruel/natural v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
Expand All @@ -109,6 +115,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -118,6 +125,10 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
go.elastic.co/fastjson v1.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
25 changes: 23 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSk
github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -64,6 +65,12 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gkampitakis/ciinfo v0.3.0 h1:gWZlOC2+RYYttL0hBqcoQhM7h1qNkVqvRCV1fOvpAv8=
github.com/gkampitakis/ciinfo v0.3.0/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/gkampitakis/go-snaps v0.5.4 h1:GX+dkKmVsRenz7SoTbdIEL4KQARZctkMiZ8ZKprRwT8=
github.com/gkampitakis/go-snaps v0.5.4/go.mod h1:ZABkO14uCuVxBHAXAfKG+bqNz+aa1bGPAg8jkI0Nk8Y=
github.com/go-jose/go-jose/v4 v4.0.1 h1:QVEPDE3OluqXBQZDcnNvQrInro2h0e4eqNbnZSWqS6U=
github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
Expand Down Expand Up @@ -164,6 +171,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
github.com/maruel/natural v1.1.1/go.mod h1:v+Rfd79xlw1AgVBjbO0BEQmptqb5HvL/k9GRHB7ZKEg=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand Down Expand Up @@ -208,6 +217,7 @@ github.com/opencontainers/image-spec v1.1.0-rc3 h1:fzg1mXZFj8YdPeNkRXMg+zb88BFV0
github.com/opencontainers/image-spec v1.1.0-rc3/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -225,8 +235,9 @@ github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+a
github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U=
github.com/prometheus/procfs v0.14.0 h1:Lw4VdGGoKEZilJsayHf0B+9YgLGREba2C6xr+Fdfq6s=
github.com/prometheus/procfs v0.14.0/go.mod h1:XL+Iwz8k8ZabyZfMFHPiilCniixqQarAy5Mu67pHlNQ=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
Expand Down Expand Up @@ -271,6 +282,16 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/vbatts/tar-split v0.11.3 h1:hLFqsOLQ1SsppQNTMpkpPXClLDfC2A3Zgy9OUU+RVck=
github.com/vbatts/tar-split v0.11.3/go.mod h1:9QlHN18E+fEH7RdG+QAJJcuya3rqT7eXSTY7wGrAokY=
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/elasticsearch/v1/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

package v1

import "github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/version"

// as of 8.2.0 a simplified unauthenticated readiness port is available which takes cluster membership into account
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html#readiness-tcp-port
var MinReadinessPortVersion = version.MinFor(8, 2, 0)

const (
ClusterName = "cluster.name"

Expand All @@ -14,6 +20,8 @@ const (
DiscoverySeedProviders = "discovery.seed_providers" // ES >= 7.X
DiscoverySeedHosts = "discovery.seed_hosts" // ES >= 7.X

ReadinessPort = "readiness.port" // ES >= 8.2.0

NetworkHost = "network.host"
NetworkPublishHost = "network.publish_host"
HTTPPublishHost = "http.publish_host"
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/common/esclient/esclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func NewClient(
es esv1.Elasticsearch,
) (esclient.Client, error) {
defer tracing.Span(&ctx)()
url := services.ExternalServiceURL(es)
v, err := version.Parse(es.Spec.Version)
if err != nil {
return nil, err
Expand Down Expand Up @@ -68,10 +67,11 @@ func NewClient(
if err != nil {
return nil, err
}

return esclient.NewElasticsearchClient(
dialer,
k8s.ExtractNamespacedName(&es),
url,
services.NewElasticsearchURLProvider(ctx, es, c),
esclient.BasicAuth{
Name: user.ControllerUserName,
Password: string(password),
Expand Down
26 changes: 11 additions & 15 deletions pkg/controller/elasticsearch/client/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
)

type baseClient struct {
User BasicAuth
HTTP *http.Client
Endpoint string
es types.NamespacedName
caCerts []*x509.Certificate
version version.Version
debug bool
User BasicAuth
HTTP *http.Client
URLProvider URLProvider
es types.NamespacedName
caCerts []*x509.Certificate
version version.Version
debug bool
}

// Close idle connections in the underlying http client.
Expand Down Expand Up @@ -57,8 +57,8 @@ func (c *baseClient) equal(c2 *baseClient) bool {
return false
}
}
// compare endpoint and user creds
return c.Endpoint == c2.Endpoint &&
// compare endpoint svc url and user creds. Service URL acts purely as an identifier here.
return c.URLProvider.ServiceURL() == c2.URLProvider.ServiceURL() &&
c.User == c2.User
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (c *baseClient) request(
body = bytes.NewBuffer(outData)
}

request, err := http.NewRequest(method, stringsutil.Concat(c.Endpoint, pathWithQuery), body) //nolint:noctx
request, err := http.NewRequest(method, stringsutil.Concat(c.URLProvider.PodURL(), pathWithQuery), body) //nolint:noctx
if err != nil {
return err
}
Expand Down Expand Up @@ -188,10 +188,6 @@ func versioned(b *baseClient, v version.Version) Client {
}
}

func (c *baseClient) URL() string {
return c.Endpoint
}

func (c *baseClient) HasProperties(version version.Version, user BasicAuth, url string, caCerts []*x509.Certificate) bool {
if len(c.caCerts) != len(caCerts) {
return false
Expand All @@ -201,5 +197,5 @@ func (c *baseClient) HasProperties(version version.Version, user BasicAuth, url
return false
}
}
return c.version.Equals(version) && c.User == user && c.Endpoint == url
return c.version.Equals(version) && c.User == user && c.URLProvider.ServiceURL() == url
}
16 changes: 7 additions & 9 deletions pkg/controller/elasticsearch/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ type Client interface {
// Version returns the Elasticsearch version this client is constructed for which should equal the minimal version
// in the cluster.
Version() version.Version
// URL returns the Elasticsearch URL configured for this client
URL() string
// HasProperties checks whether this client has the indicated properties.
HasProperties(version version.Version, user BasicAuth, url string, caCerts []*x509.Certificate) bool
}
Expand All @@ -148,7 +146,7 @@ func formatAsSeconds(d time.Duration) string {
func NewElasticsearchClient(
dialer net.Dialer,
es types.NamespacedName,
esURL string,
esURL URLProvider,
esUser BasicAuth,
v version.Version,
caCerts []*x509.Certificate,
Expand All @@ -158,12 +156,12 @@ func NewElasticsearchClient(
client := commonhttp.Client(dialer, caCerts, timeout)
client.Transport = apmelasticsearch.WrapRoundTripper(client.Transport)
base := &baseClient{
Endpoint: esURL,
User: esUser,
caCerts: caCerts,
HTTP: client,
es: es,
debug: debug,
URLProvider: esURL,
User: esUser,
caCerts: caCerts,
HTTP: client,
es: es,
debug: debug,
}
return versioned(base, v)
}
9 changes: 5 additions & 4 deletions pkg/controller/elasticsearch/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestClient_request(t *testing.T) {
assert.Equal(t, "cloud", req.Header.Get("x-elastic-product-origin"))
}),
},
Endpoint: "http://example.com",
URLProvider: NewStaticURLProvider("http://example.com"),
}
requests := []func() (string, error){
func() (string, error) {
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestGetInfo(t *testing.T) {
}

func TestClient_Equal(t *testing.T) {
dummyEndpoint := "es-url"
dummyEndpoint := NewStaticURLProvider("es-url")
dummyUser := BasicAuth{Name: "user", Password: "password"}
dummyNamespaceName := types.NamespacedName{
Namespace: "ns",
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestClient_Equal(t *testing.T) {
{
name: "different endpoint",
c1: NewElasticsearchClient(nil, dummyNamespaceName, dummyEndpoint, dummyUser, v6, dummyCACerts, timeout, false),
c2: NewElasticsearchClient(nil, dummyNamespaceName, "another-endpoint", dummyUser, v6, dummyCACerts, timeout, false),
c2: NewElasticsearchClient(nil, dummyNamespaceName, NewStaticURLProvider("another-endpoint"), dummyUser, v6, dummyCACerts, timeout, false),
want: false,
},
{
Expand Down Expand Up @@ -836,11 +836,12 @@ func Test_HasProperties(t *testing.T) {
defaultVersion := version.MustParse("8.6.1")
defaultUser := BasicAuth{Name: "foo", Password: "bar"}
defaultURL := "https://foo.bar"
defaultURLProvider := NewStaticURLProvider(defaultURL)
defaultCaCerts := []*x509.Certificate{{Raw: []byte("foo")}}
defaultEsClient := NewElasticsearchClient(
nil,
types.NamespacedName{Namespace: "ns", Name: "es"},
defaultURL,
defaultURLProvider,
defaultUser,
defaultVersion,
defaultCaCerts,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/elasticsearch/client/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewMockClientWithUser(v version.Version, u BasicAuth, fn RoundTripFunc) Cli
HTTP: &http.Client{
Transport: fn,
},
Endpoint: "http://example.com",
User: u,
URLProvider: NewStaticURLProvider("http://example.com"),
User: u,
}
return versioned(baseClient, v)
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/controller/elasticsearch/client/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package client

type URLProvider interface {
// PodURL is a url for a random pod (falls back to ServiceURL).
PodURL() string
// ServiceURL is the url for the Kubernetes service related to the Pod URLs provided.
ServiceURL() string
pebrc marked this conversation as resolved.
Show resolved Hide resolved

HasEndpoints() bool
}

func NewStaticURLProvider(url string) URLProvider {
return &staticURLProvider{
url: url,
}
}

type staticURLProvider struct {
url string
}

// PodURL implements URLProvider.
func (s *staticURLProvider) PodURL() string {
return s.url
}

// ServiceURL implements URLProvider.
func (s *staticURLProvider) ServiceURL() string {
return s.url
}

func (s *staticURLProvider) HasEndpoints() bool {
return true
}

var _ URLProvider = &staticURLProvider{}
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/client/v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c *clientV6) GetClusterState(_ context.Context) (ClusterState, error) {
}

func (c *clientV6) Request(ctx context.Context, r *http.Request) (*http.Response, error) {
newURL, err := url.Parse(stringsutil.Concat(c.Endpoint, r.URL.String()))
newURL, err := url.Parse(stringsutil.Concat(c.URLProvider.PodURL(), r.URL.String()))
if err != nil {
return nil, err
}
Expand Down
Loading