Skip to content

Commit

Permalink
cephfs: update fetchIP to support ipv6 addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Riya Singhal <[email protected]>
  • Loading branch information
riya-singhal31 committed Nov 6, 2023
1 parent 348959a commit 54c269d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 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 @@ -212,9 +213,13 @@ func (ac *activeClient) fetchIP() (string, error) {
clientInfo := ac.Inst
parts := strings.Fields(clientInfo)
if len(parts) >= 2 {
ip := strings.Split(parts[1], ":")[0]

return ip, nil
// use a regular expression to find an IPv4 or IPv6 address in the string
ipRegex := `([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(\d{1,3}\.){3}\d{1,3}`
re := regexp.MustCompile(ipRegex)
matches := re.FindStringSubmatch(parts[1])
if len(matches) > 0 {
return matches[0], nil
}
}

return "", fmt.Errorf("failed to extract IP address, incorrect format: %s", clientInfo)
Expand Down

0 comments on commit 54c269d

Please sign in to comment.