Repository: opensearch-project/opensearch-go
Pull request #461 (2024-01-19 05:15:25)

fix:Clusters deployed using IPV6 do not support the DiscoverNodes method
### Description
When using the opensearch-go library to access an opensearch cluster deployed with IPV6, an access error will occur when calling other APIs after calling the DiscoverNodes method.

### Issues Resolved
[_List any issues this PR will resolve, e.g. Closes [...]._ ](https://github.com/opensearch-project/opensearch-go/issues/458)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`8d09ba8`)](https://app.codecov.io/gh/opensearch-project/opensearch-go/commit/8d09ba87a37233a1ea7cd2f4ca7e07764f78812a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) 57.25% compared to head [(`b06ce74`)](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) 57.25%.
> Report is 1 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #461   +/-   ##
=======================================
  Coverage   57.25%   57.25%           
=======================================
  Files         315      315           
  Lines        9820     9820           
=======================================
  Hits         5622     5622           
  Misses       2904     2904           
  Partials     1294     1294           
```

| [Flag](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | Coverage Δ | |
|---|---|---|
| [integration](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | `50.82% <0.00%> (ø)` | |
| [unit](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | `12.82% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | Coverage Δ | |
|---|---|---|
| [opensearchtransport/discovery.go](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/461?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#diff-b3BlbnNlYXJjaHRyYW5zcG9ydC9kaXNjb3ZlcnkuZ28=) | `62.20% <100.00%> (ø)` | |

</details>
Jakob3xD: LGTM, only thing missing is the DCO.  Can you please sign your commits.
Afterwards we are good to go from my side.
Zhang-jie-jun: > LGTM, only thing missing is the DCO. Can you please sign your commits. Afterwards we are good to go from my side.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c687a1..102cbd5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 - Test adjusted to new opensearchapi functions and structs ([#421](https://github.com/opensearch-project/opensearch-go/pull/421))
 - Change codecov to comment code coverage to each PR ([#410](https://github.com/opensearch-project/opensearch-go/pull/410))
 - Change module version from v2 to v3 ([#444](https://github.com/opensearch-project/opensearch-go/pull/444))
+- Support IPV6 in the DiscoverNodes method ([#458](https://github.com/opensearch-project/opensearch-go/issues/458))
 
 ### Deprecated
 
diff --git a/opensearchtransport/discovery.go b/opensearchtransport/discovery.go
index f4463f3..93ed61e 100644
--- a/opensearchtransport/discovery.go
+++ b/opensearchtransport/discovery.go
@@ -31,6 +31,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"net"
 	"net/http"
 	"net/url"
 	"strings"
@@ -179,6 +180,7 @@ func (c *Client) getNodeURL(node nodeInfo, scheme string) *url.URL {
 	var (
 		host string
 		port string
+		err  error
 
 		addrs = strings.Split(node.HTTP.PublishAddress, "/")
 		ports = strings.Split(node.HTTP.PublishAddress, ":")
@@ -187,13 +189,15 @@ func (c *Client) getNodeURL(node nodeInfo, scheme string) *url.URL {
 	if len(addrs) > 1 {
 		host = addrs[0]
 	} else {
-		host = strings.Split(addrs[0], ":")[0]
+		host, _, err = net.SplitHostPort(addrs[0])
+		if err != nil {
+			host = strings.Split(addrs[0], ":")[0]
+		}
 	}
-
 	port = ports[len(ports)-1]
 	u := &url.URL{
 		Scheme: scheme,
-		Host:   host + ":" + port,
+		Host:   net.JoinHostPort(host, port),
 	}
 
 	return u


Test patch:
diff --git a/opensearchtransport/discovery_internal_test.go b/opensearchtransport/discovery_internal_test.go
index 41ba462..0a27d5f 100644
--- a/opensearchtransport/discovery_internal_test.go
+++ b/opensearchtransport/discovery_internal_test.go
@@ -82,8 +82,8 @@ func TestDiscovery(t *testing.T) {
 		}
 		fmt.Printf("NodesInfo: %+v\n", nodes)
 
-		if len(nodes) != 3 {
-			t.Errorf("Unexpected number of nodes, want=3, got=%d", len(nodes))
+		if len(nodes) != 4 {
+			t.Errorf("Unexpected number of nodes, want=4, got=%d", len(nodes))
 		}
 
 		for _, node := range nodes {
@@ -100,6 +100,10 @@ func TestDiscovery(t *testing.T) {
 				if node.URL.String() != "http://127.0.0.1:10003" {
 					t.Errorf("Unexpected URL: %s", node.URL.String())
 				}
+			case "es4":
+				if node.URL.String() != "http://[fc99:3528::a04:812c]:10004" {
+					t.Errorf("Unexpected URL: %s", node.URL.String())
+				}
 			}
 		}
 	})
diff --git a/opensearchtransport/testdata/nodes.info.json b/opensearchtransport/testdata/nodes.info.json
index cc24f2f..9bac210 100644
--- a/opensearchtransport/testdata/nodes.info.json
+++ b/opensearchtransport/testdata/nodes.info.json
@@ -63,6 +63,25 @@
         "publish_address": "127.0.0.1:10003",
         "max_content_length_in_bytes": 104857600
       }
+    },
+    "4uJ-108zTz27ISgkmAQgfw": {
+      "name": "es4",
+      "transport_address": "[fc99:3528::a04:812c]:9303",
+      "host": "fc99:3528:0:0:0:0:a04:812c",
+      "ip": "fc99:3528::a04:812c",
+      "version": "7.4.2",
+      "build_flavor": "oss",
+      "build_type": "tar",
+      "build_hash": "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
+      "roles": ["cluster_manager"],
+      "http": {
+        "bound_address": [
+          "[::1]:10004",
+          "[fc99:3528::a04:812c]:10004"
+        ],
+        "publish_address": "[fc99:3528::a04:812c]:10004",
+        "max_content_length_in_bytes": 104857600
+      }
     }
   }
 }