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]>
  • Loading branch information
riya-singhal31 committed Dec 15, 2023
1 parent e998330 commit cc9b043
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 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 @@ -206,15 +207,32 @@ func isIPInCIDR(ctx context.Context, ip, cidr string) bool {
return ipCidr.Contains(ipAddress)
}

// 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)
// if ip != nil {
// return ip.String(), nil
// }
// }

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

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
}
Expand Down

0 comments on commit cc9b043

Please sign in to comment.