Repository: m3db/m3db-operator
Pull request #256 (2020-12-16 02:07:35)

Add configurable port to health client
We can't assume the server is listening on port 80

Thanks for contributing to the M3DB Operator! We'd love to accept your contribution, but we require that most issues
outside of trivial changes such as typo corrections have an issue associated with the pull request. So please [open an
issue](https://github.com/m3db/m3db-operator/issues/new) first!


codecov[bot]: # [Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=h1) Report
> Merging [#256](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=desc) (0aad7f5) into [master](https://codecov.io/gh/m3db/m3db-operator/commit/65e1cd8b820c0c1daf5e92bc781aea6aa8d7b99f?el=desc) (65e1cd8) will **increase** coverage by `0.08%`.
> The diff coverage is `100.00%`.


[![Impacted file tree graph](https://codecov.io/gh/m3db/m3db-operator/pull/256/graphs/tree.svg?width=650&height=150&src=pr&token=jtHDyWhsto)](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #256      +/-   ##
==========================================
+ Coverage   76.37%   76.45%   +0.08%     
==========================================
  Files          31       31              
  Lines        2277     2285       +8     
==========================================
+ Hits         1739     1747       +8     
  Misses        395      395              
  Partials      143      143              
```


------

[Continue to review full report at Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=footer). Last update [65e1cd8...93f036d](https://codecov.io/gh/m3db/m3db-operator/pull/256?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/m3admin/health/client.go b/pkg/m3admin/health/client.go
index d70e988..91818b9 100644
--- a/pkg/m3admin/health/client.go
+++ b/pkg/m3admin/health/client.go
@@ -25,6 +25,7 @@ import (
 	"net/http"
 	"net/url"
 
+	"github.com/m3db/m3db-operator/pkg/k8sops/m3db"
 	"github.com/m3db/m3db-operator/pkg/m3admin"
 )
 
@@ -36,6 +37,7 @@ const (
 type healthClient struct {
 	client m3admin.Client
 	url    string
+	port   int
 }
 
 // Option provides an interface that can be used for setter options with the
@@ -50,7 +52,7 @@ func (fn optionFn) execute(h *healthClient) error {
 	return fn(h)
 }
 
-// WithURL is a setter to override the default base url for a node.
+// WithURL is a setter to override the default base url for a node. This overrides the WithPort option.
 func WithURL(u string) Option {
 	return optionFn(func(h *healthClient) error {
 		if u != "" {
@@ -71,6 +73,14 @@ func WithClient(cl m3admin.Client) Option {
 	})
 }
 
+// WithPort overrides the default port (m3db.PortM3DBHTTPNode). This does nothing if WithURL is used.
+func WithPort(port int) Option {
+	return optionFn(func(h *healthClient) error {
+		h.port = port
+		return nil
+	})
+}
+
 // NewClient constructs a new health client.
 func NewClient(opts ...Option) (Client, error) {
 	hc := &healthClient{
@@ -85,9 +95,13 @@ func NewClient(opts ...Option) (Client, error) {
 }
 
 func (h *healthClient) Bootstrapped(namespace string, podName string) (bool, error) {
+	port := h.port
+	if port == 0 {
+		port = m3db.PortM3DBHTTPNode
+	}
 	url := h.url
 	if url == "" {
-		url = getPodURL(namespace, podName)
+		url = getPodURL(namespace, podName, port)
 	}
 
 	_, err := h.client.DoHTTPRequest(http.MethodGet, url+bootstrappedPath, nil)
@@ -98,6 +112,6 @@ func (h *healthClient) Bootstrapped(namespace string, podName string) (bool, err
 	return true, nil
 }
 
-func getPodURL(namespace string, podName string) string {
-	return fmt.Sprintf("http://%s.%s.%s", podName, m3dbServiceName, namespace)
+func getPodURL(namespace string, podName string, port int) string {
+	return fmt.Sprintf("http://%s.%s.%s:%d", podName, m3dbServiceName, namespace, port)
 }


Test patch:
diff --git a/pkg/m3admin/health/client_test.go b/pkg/m3admin/health/client_test.go
index 3b9c777..d69d0ac 100644
--- a/pkg/m3admin/health/client_test.go
+++ b/pkg/m3admin/health/client_test.go
@@ -56,7 +56,8 @@ func TestBootstrapped_ValidateURL(t *testing.T) {
 	defer ctrl.Finish()
 
 	adminClient := m3admin.NewMockClient(ctrl)
-	adminClient.EXPECT().DoHTTPRequest("GET", "http://bar.m3dbnode-m3-cluster-short.foo/bootstrapped", nil).
+	adminClient.EXPECT().DoHTTPRequest("GET", "http://bar.m3dbnode-m3-cluster-short.foo:9002/bootstrapped",
+		nil).
 		Return(&http.Response{}, nil)
 
 	client := newHealthClient(t, adminClient)
@@ -67,6 +68,27 @@ func TestBootstrapped_ValidateURL(t *testing.T) {
 	require.True(t, bootstrapped)
 }
 
+func TestBootstrapped_WithPort(t *testing.T) {
+	ctrl := gomock.NewController(t)
+	defer ctrl.Finish()
+
+	adminClient := m3admin.NewMockClient(ctrl)
+	adminClient.EXPECT().DoHTTPRequest("GET", "http://bar.m3dbnode-m3-cluster-short.foo:8088/bootstrapped",
+		nil).
+		Return(&http.Response{}, nil)
+
+	client, err := NewClient(
+		WithClient(adminClient),
+		WithPort(8088),
+	)
+	require.NoError(t, err)
+
+	bootstrapped, err := client.Bootstrapped("foo", "bar")
+
+	require.NoError(t, err)
+	require.True(t, bootstrapped)
+}
+
 func TestBootstrapped_NotReady(t *testing.T) {
 	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		_, err := ioutil.ReadAll(r.Body)