Skip to content

Commit

Permalink
adds reporting of ndmz pub4 and pub6 ifaces, #632 (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVerstraete authored May 11, 2020
1 parent d900da9 commit e1a8692
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
59 changes: 58 additions & 1 deletion cmds/networkd/addr_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"context"
"fmt"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/rs/zerolog/log"

"time"

"github.com/cenkalti/backoff/v3"
"github.com/threefoldtech/tfexplorer/client"
"github.com/threefoldtech/tfexplorer/models/generated/directory"
"github.com/threefoldtech/tfexplorer/schema"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/network/ifaceutil"
"github.com/threefoldtech/zos/pkg/network/namespace"
"github.com/threefoldtech/zos/pkg/network/ndmz"
"github.com/threefoldtech/zos/pkg/network/types"
"github.com/threefoldtech/tfexplorer/schema"
"github.com/vishvananda/netlink"
)

Expand Down Expand Up @@ -56,6 +59,13 @@ func (w WatchedLinks) callBack(update netlink.AddrUpdate) error {
return err
}

ndmzIfaces, err := getNdmzInterfaces()
if err != nil {
return err
}

ifaces = append(ifaces, ndmzIfaces...)

return publishIfaces(ifaces, w.nodeID, w.dir)
}

Expand Down Expand Up @@ -96,6 +106,53 @@ func (w WatchedLinks) Forever(ctx context.Context) error {
}
}

func getNdmzInterfaces() ([]types.IfaceInfo, error) {
var output []types.IfaceInfo

f := func(_ ns.NetNS) error {
links, err := netlink.LinkList()
if err != nil {
log.Error().Err(err).Msgf("failed to list interfaces")
return err
}
for _, link := range links {
if link.Attrs().Name == ndmz.DMZPub4 || link.Attrs().Name == ndmz.DMZPub6 {
addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return err
}

info := types.IfaceInfo{
Name: link.Attrs().Name,
Addrs: make([]types.IPNet, len(addrs)),
MacAddress: schema.MacAddress{link.Attrs().HardwareAddr},
}
for i, addr := range addrs {
info.Addrs[i] = types.NewIPNet(addr.IPNet)
}

output = append(output, info)
}

}
return nil
}

// get the ndmz network namespace
ndmz, err := namespace.GetByName(ndmz.NetNSNDMZ)
if err != nil {
return nil, err
}
defer ndmz.Close()

err = ndmz.Do(f)
if err != nil {
return nil, err
}

return output, nil
}

func getLocalInterfaces() ([]types.IfaceInfo, error) {
var output []types.IfaceInfo

Expand Down
24 changes: 16 additions & 8 deletions cmds/networkd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ func main() {
log.Info().Msg("shutting down")
})

ifaces, err := getLocalInterfaces()
if err != nil {
log.Fatal().Err(err).Msg("failed to read local network interfaces")
}
if err := publishIfaces(ifaces, nodeID, dir); err != nil {
log.Fatal().Err(err).Msg("failed to publish network interfaces to BCDB")
}

ifaceVersion := -1

exitIface, err := getPubIface(dir, nodeID.Identity())
Expand Down Expand Up @@ -123,6 +115,22 @@ func main() {
}
}(ctx, chIface)

ifaces, err := getLocalInterfaces()
if err != nil {
log.Fatal().Err(err).Msg("failed to read local network interfaces")
}

ndmzIfaces, err := getNdmzInterfaces()
if err != nil {
log.Fatal().Err(err).Msg("failed to read ndmz network interfaces")
}

ifaces = append(ifaces, ndmzIfaces...)

if err := publishIfaces(ifaces, nodeID, dir); err != nil {
log.Fatal().Err(err).Msg("failed to publish ndmz network interfaces to BCDB")
}

go startAddrWatch(ctx, nodeID, dir, ifaces)

log.Info().Msg("start zbus server")
Expand Down

0 comments on commit e1a8692

Please sign in to comment.