Skip to content

Commit 0308748

Browse files
riya-singhal31mergify[bot]
authored andcommitted
cephfs: fix fetchIP to support more formats
Signed-off-by: Riya Singhal <[email protected]> (cherry picked from commit bee77f7)
1 parent 0d66c35 commit 0308748

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

internal/csi-addons/networkfence/fencing.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"net"
25+
"regexp"
2526
"strconv"
2627
"strings"
2728
"time"
@@ -210,11 +211,24 @@ func (ac *activeClient) fetchIP() (string, error) {
210211
// example: "inst": "client.4305 172.21.9.34:0/422650892",
211212
// then returning value will be 172.21.9.34
212213
clientInfo := ac.Inst
213-
parts := strings.Fields(clientInfo)
214-
if len(parts) >= 2 {
215-
lastColonIndex := strings.LastIndex(parts[1], ":")
216-
firstPart := parts[1][:lastColonIndex]
217-
ip := net.ParseIP(firstPart)
214+
215+
// Attempt to extract the IP address using a regular expression
216+
// the regular expression aims to match either a complete IPv6
217+
// address or a complete IPv4 address follows by any prefix (v1 or v2)
218+
// if exists
219+
// (?:v[0-9]+:): this allows for an optional prefix starting with "v"
220+
// followed by one or more digits and a colon.
221+
// The ? outside the group makes the entire prefix section optional.
222+
// (?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}: this allows to check for
223+
// standard IPv6 address.
224+
// |: Alternation operator to allow matching either the IPv6 pattern
225+
// with a prefix or the IPv4 pattern.
226+
// '(?:\d+\.){3}\d+: This part matches a standard IPv4 address.
227+
re := regexp.MustCompile(`(?:v[0-9]+:)?([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}|(?:\d+\.){3}\d+)`)
228+
ipMatches := re.FindStringSubmatch(clientInfo)
229+
230+
if len(ipMatches) > 0 {
231+
ip := net.ParseIP(ipMatches[1])
218232
if ip != nil {
219233
return ip.String(), nil
220234
}

internal/csi-addons/networkfence/fencing_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func TestFetchIP(t *testing.T) {
7373
expectedIP: "2001:db8:85a3::8a2e:370:7334",
7474
expectedErr: false,
7575
},
76+
{
77+
clientInfo: "client.24152 v1:100.64.0.7:0/3658550259",
78+
expectedIP: "100.64.0.7",
79+
expectedErr: false,
80+
},
7681
{
7782
clientInfo: "",
7883
expectedIP: "",

0 commit comments

Comments
 (0)