From 19556c88455bd28c6541febe59b425cd5f8ebc6b Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Fri, 15 Dec 2023 18:37:49 +0530 Subject: [PATCH] cephfs: fix fetchIP to support more formats Signed-off-by: Riya Singhal --- internal/csi-addons/networkfence/fencing.go | 13 ++++++++----- internal/csi-addons/networkfence/fencing_test.go | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/csi-addons/networkfence/fencing.go b/internal/csi-addons/networkfence/fencing.go index d8361e55c2ba..ffbc9ffdd85c 100644 --- a/internal/csi-addons/networkfence/fencing.go +++ b/internal/csi-addons/networkfence/fencing.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "net" + "regexp" "strconv" "strings" "time" @@ -210,11 +211,13 @@ func (ac *activeClient) fetchIP() (string, error) { // example: "inst": "client.4305 172.21.9.34:0/422650892", // then returning value will be 172.21.9.34 clientInfo := ac.Inst - parts := strings.Fields(clientInfo) - if len(parts) >= 2 { - lastColonIndex := strings.LastIndex(parts[1], ":") - firstPart := parts[1][:lastColonIndex] - ip := net.ParseIP(firstPart) + + // Attempt to extract the IP address using a regular expression + re := regexp.MustCompile(`(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:\d+\.){3}\d+`) + ipMatches := re.FindStringSubmatch(clientInfo) + + if len(ipMatches) > 0 { + ip := net.ParseIP(ipMatches[0]) if ip != nil { return ip.String(), nil } diff --git a/internal/csi-addons/networkfence/fencing_test.go b/internal/csi-addons/networkfence/fencing_test.go index 40242cf79075..26aab00f6064 100644 --- a/internal/csi-addons/networkfence/fencing_test.go +++ b/internal/csi-addons/networkfence/fencing_test.go @@ -73,6 +73,11 @@ func TestFetchIP(t *testing.T) { expectedIP: "2001:db8:85a3::8a2e:370:7334", expectedErr: false, }, + { + clientInfo: "client.24152 v1:100.64.0.7:0/3658550259", + expectedIP: "100.64.0.7", + expectedErr: false, + }, { clientInfo: "", expectedIP: "",