Skip to content

Commit

Permalink
cephfs: fix fetchIP to support more formats
Browse files Browse the repository at this point in the history
Signed-off-by: Riya Singhal <[email protected]>
(cherry picked from commit bee77f7)
(cherry picked from commit 0308748)
  • Loading branch information
riya-singhal31 authored and nixpanic committed Dec 20, 2023
1 parent 186a7ff commit 69ff7ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
24 changes: 19 additions & 5 deletions internal/csi-addons/networkfence/fencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"net"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -210,11 +211,24 @@ 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
// the regular expression aims to match either a complete IPv6
// address or a complete IPv4 address follows by any prefix (v1 or v2)
// if exists
// (?:v[0-9]+:): this allows for an optional prefix starting with "v"
// followed by one or more digits and a colon.
// The ? outside the group makes the entire prefix section optional.
// (?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}: this allows to check for
// standard IPv6 address.
// |: Alternation operator to allow matching either the IPv6 pattern
// with a prefix or the IPv4 pattern.
// '(?:\d+\.){3}\d+: This part matches a standard IPv4 address.
re := regexp.MustCompile(`(?:v[0-9]+:)?([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}|(?:\d+\.){3}\d+)`)
ipMatches := re.FindStringSubmatch(clientInfo)

if len(ipMatches) > 0 {
ip := net.ParseIP(ipMatches[1])
if ip != nil {
return ip.String(), nil
}
Expand Down
5 changes: 5 additions & 0 deletions internal/csi-addons/networkfence/fencing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down

0 comments on commit 69ff7ce

Please sign in to comment.