Skip to content

Commit

Permalink
fix(download): provide pod ip to longhorn manager when download backi…
Browse files Browse the repository at this point in the history
…ng image to local

ref: longhorn/longhorn 7236

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin authored and David Ko committed Dec 18, 2023
1 parent 11af596 commit 8f1909a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/manager/service.go
Expand Up @@ -500,9 +500,9 @@ func (m *Manager) PrepareDownload(ctx context.Context, req *rpc.PrepareDownloadR
return nil, status.Errorf(codes.FailedPrecondition, "invalid backing image state %v for the download", bi.Status.State)
}

address, err := util.ConvertToStorageAddress(m.syncAddress)
address, err := util.GetSyncServiceAddressWithPodIP(m.syncAddress)
if err != nil {
return nil, status.Errorf(codes.Unknown, "failed to detect the storage address of the sync server for the download: %v", err)
return nil, status.Errorf(codes.Internal, "failed to get sync service address: %v", err)
}
srcFilePath := types.GetBackingImageFilePath(m.diskPath, req.Name, req.Uuid)

Expand Down
30 changes: 4 additions & 26 deletions pkg/util/http_util.go
Expand Up @@ -96,33 +96,11 @@ func GetLocalIPv4fromInterface(name string) (ip string, err error) {
return ipv4.String(), nil
}

func IsLoopbackHost(host string) bool {
if host == "localhost" || host == "127.0.0.1" || host == "0.0.0.0" || host == "::1" || host == "" {
return true
}
// Check for loopback network.
ips, err := net.LookupIP(host)
if err != nil {
return false
}
for _, ip := range ips {
if !ip.IsLoopback() {
return false
}
}
return true
}

func ConvertToStorageAddress(address string) (string, error) {
host, port, err := net.SplitHostPort(address)
func GetSyncServiceAddressWithPodIP(address string) (string, error) {
_, port, err := net.SplitHostPort(address)
if err != nil {
return "", err
}
if IsLoopbackHost(host) {
host, err = GetIPForPod()
if err != nil {
return "", err
}
}
return net.JoinHostPort(host, port), nil
podIP := os.Getenv(EnvPodIP)
return net.JoinHostPort(podIP, port), nil
}

0 comments on commit 8f1909a

Please sign in to comment.