Skip to content

Commit

Permalink
fetch ip address fort NIC when IPAddress is null (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang0 authored Aug 21, 2023
1 parent 2296d88 commit 118c174
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ linters:
enable:
- bodyclose
- deadcode
# - depguard
# - depguard
- dogsled
- gochecknoinits
- goconst
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.8.2
github.com/urfave/cli/v2 v2.25.1
github.com/vishvananda/netns v0.0.4
go.uber.org/automaxprocs v1.5.2
golang.org/x/sys v0.8.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBn
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down
49 changes: 48 additions & 1 deletion runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"io"
"math"
"net"
"net/http/httputil"
"os"
"runtime"
"strings"
"time"

Expand All @@ -16,6 +18,7 @@ import (
"github.com/projecteru2/agent/utils"
"github.com/projecteru2/core/cluster"
coreutils "github.com/projecteru2/core/utils"
"github.com/vishvananda/netns"

enginetypes "github.com/docker/docker/api/types"
enginecontainer "github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -169,6 +172,36 @@ func (d *Docker) checkHostname(env []string) bool {
return false
}

func getAddrsFromNS(cid string, ifname string) ([]net.Addr, error) {
// Lock the OS Thread so we don't accidentally switch namespaces
runtime.LockOSThread()
defer runtime.UnlockOSThread()

// Save the current network namespace
origns, _ := netns.Get()
defer origns.Close()
defer netns.Set(origns) //nolint:errcheck

containerNS, err := netns.GetFromDocker(cid)
if err != nil {
return nil, err
}
defer containerNS.Close()

if err := netns.Set(containerNS); err != nil {
return nil, err
}
eth0, err := net.InterfaceByName(ifname)
if err != nil {
return nil, err
}
addrs, err := eth0.Addrs()
if err != nil {
return nil, err
}
return addrs, nil
}

// detectWorkload detect a container by ID
func (d *Docker) detectWorkload(ctx context.Context, ID string) (*Container, error) {
// 标准化为 inspect 的数据
Expand Down Expand Up @@ -205,7 +238,7 @@ func (d *Docker) detectWorkload(ctx context.Context, ID string) (*Container, err
container.Memory = d.memory
}
// 活着才有发布必要
if c.NetworkSettings != nil && container.Running {
if c.NetworkSettings != nil && container.Running { //nolint:nestif
networks := map[string]string{}
for name, endpoint := range c.NetworkSettings.Networks {
networkmode := enginecontainer.NetworkMode(name)
Expand All @@ -216,6 +249,20 @@ func (d *Docker) detectWorkload(ctx context.Context, ID string) (*Container, err
container.LocalIP = endpoint.IPAddress
networks[name] = endpoint.IPAddress
}
if networks[name] == "" {
addrs, err := getAddrsFromNS(c.ID, "eth0")
if err != nil {
log.Error(ctx, err, "failed to get eth0 addrs")
}
if len(addrs) > 0 {
ip, _, err := net.ParseCIDR(addrs[0].String())
if err == nil {
networks[name] = ip.String()
} else {
log.Error(ctx, err, "failed to parse cidr %s", addrs[0].String())
}
}
}
break
}
container.Networks = networks
Expand Down

0 comments on commit 118c174

Please sign in to comment.