Repository: rook/rook
Pull request #12001 (2023-03-29 15:16:14)

csi: IPv6 compatibility for requiring msgr2 (backport #11992)
This is an automatic backport of pull request #11992 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>


Patch:
diff --git a/pkg/operator/ceph/csi/cluster_config.go b/pkg/operator/ceph/csi/cluster_config.go
index 043b094e6..a9d62aa0e 100644
--- a/pkg/operator/ceph/csi/cluster_config.go
+++ b/pkg/operator/ceph/csi/cluster_config.go
@@ -108,13 +108,10 @@ func MonEndpoints(mons map[string]*cephclient.MonInfo, requireMsgr2 bool) []stri
 		endpoint := m.Endpoint
 		if requireMsgr2 {
 			logger.Debugf("evaluating mon %q for msgr1 on endpoint %q", m.Name, m.Endpoint)
-			if strings.HasSuffix(m.Endpoint, fmt.Sprintf(":%d", client.Msgr1port)) {
-				parts := strings.Split(m.Endpoint, ":")
-				if len(parts) != 2 {
-					logger.Errorf("endpoint %q does not contain two parts to extract the port", m.Endpoint)
-					continue
-				}
-				endpoint = fmt.Sprintf("%s:%d", parts[0], client.Msgr2port)
+			msgr1Suffix := fmt.Sprintf(":%d", client.Msgr1port)
+			if strings.HasSuffix(m.Endpoint, msgr1Suffix) {
+				address := m.Endpoint[0:strings.LastIndex(m.Endpoint, msgr1Suffix)]
+				endpoint = fmt.Sprintf("%s:%d", address, client.Msgr2port)
 				logger.Debugf("mon %q will use the msgrv2 port: %q", m.Name, endpoint)
 			}
 		}


Test patch:
diff --git a/pkg/operator/ceph/csi/cluster_config_test.go b/pkg/operator/ceph/csi/cluster_config_test.go
index dc64ed60f..5f778a16d 100644
--- a/pkg/operator/ceph/csi/cluster_config_test.go
+++ b/pkg/operator/ceph/csi/cluster_config_test.go
@@ -369,15 +369,20 @@ func TestMonEndpoints(t *testing.T) {
 		verifyEndpointPort(t, endpoints, "3300")
 	})
 
-	t.Run("bad endpoint", func(t *testing.T) {
+	t.Run("ipv6 endpoint conversion", func(t *testing.T) {
 		monInfo := map[string]*cephclient.MonInfo{
-			"a": {Name: "a", Endpoint: "1.2.3.4:5:6789"},
-			"b": {Name: "b", Endpoint: "1.2.3.5:6789"},
-			"c": {Name: "c", Endpoint: "1.2.3.6:6789"},
+			"a": {Name: "a", Endpoint: "[fd07:aaaa:bbbb:cccc::11]:6789"},
+			"b": {Name: "a", Endpoint: "[1234:6789:bbbb:cccc::11]:6789"},
 		}
 		endpoints := MonEndpoints(monInfo, true)
 		assert.Equal(t, 2, len(endpoints))
 		verifyEndpointPort(t, endpoints, "3300")
+		for _, endpoint := range endpoints {
+			// Verify that the v1 port inside the ipv6 address will not be replaced
+			if strings.HasPrefix(endpoint, "[1234") {
+				assert.True(t, strings.HasPrefix(endpoint, "[1234:6789"))
+			}
+		}
 	})
 }