Skip to content

check for prefix in service.go #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions agent/applier/applyspec/v1_apply_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package applyspec

import (
"encoding/json"
"fmt"

"github.com/cloudfoundry/bosh-agent/v2/agent/applier/models"
)
Expand Down Expand Up @@ -90,6 +91,7 @@ func (s NetworkSpec) PopulateIPInfo(ip, netmask, gateway string) NetworkSpec {
s.Fields["ip"] = ip
s.Fields["netmask"] = netmask
s.Fields["gateway"] = gateway
fmt.Println(s.Fields, ": PopulateIPInfo")
return s
}

Expand Down
20 changes: 16 additions & 4 deletions platform/net/default_network_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ func NewDefaultNetworkResolver(

func (r defaultNetworkResolver) GetDefaultNetwork() (boshsettings.Network, error) {
network := boshsettings.Network{}

routes, err := r.routesSearcher.SearchRoutes()
// TODO: [SD] check what kind of ip
var routes []Route
var err error
if network.Prefix == "128" {
routes, err = r.routesSearcher.SearchIPv6Routes()
} else {
routes, err = r.routesSearcher.SearchRoutes()
}
if err != nil {
return network, bosherr.WrapError(err, "Searching routes")
}
Expand All @@ -40,8 +46,14 @@ func (r defaultNetworkResolver) GetDefaultNetwork() (boshsettings.Network, error
if !route.IsDefault() {
continue
}

ip, err := r.ipResolver.GetPrimaryIPv4(route.InterfaceName)
// TODO: [SD] check for ipv6
var ip *gonet.IPNet
if network.Prefix == "128" {
ip, err = r.ipResolver.GetPrimaryIPv6(route.InterfaceName)
} else {
ip, err = r.ipResolver.GetPrimaryIPv4(route.InterfaceName)
}
// ip, err := r.ipResolver.GetPrimaryIPv4(route.InterfaceName) ||
if err != nil {
return network, bosherr.WrapErrorf(err, "Getting primary IPv4 for interface '%s'", route.InterfaceName)
}
Expand Down
25 changes: 25 additions & 0 deletions platform/net/ip/ip_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NetworkInterfaceToAddrsFunc(interfaceName string) ([]gonet.Addr, error) {
type Resolver interface {
// GetPrimaryIPv4 always returns error unless IPNet is found for given interface
GetPrimaryIPv4(interfaceName string) (*gonet.IPNet, error)
GetPrimaryIPv6(interfaceName string) (*gonet.IPNet, error)
}

type ipResolver struct {
Expand All @@ -30,6 +31,30 @@ func NewResolver(ifaceToAddrsFunc InterfaceToAddrsFunc) Resolver {
return ipResolver{ifaceToAddrsFunc: ifaceToAddrsFunc}
}

func (r ipResolver) GetPrimaryIPv6(interfaceName string) (*gonet.IPNet, error) {
addrs, err := r.ifaceToAddrsFunc(interfaceName)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Looking up addresses for interface '%s'", interfaceName)
}

if len(addrs) == 0 {
return nil, bosherr.Errorf("No addresses found for interface '%s'", interfaceName)
}

for _, addr := range addrs {
ip, ok := addr.(*gonet.IPNet)
if !ok {
continue
}

if ip.IP.To16() != nil || ip.IP.IsGlobalUnicast() {
return ip, nil
}
}

return nil, bosherr.Errorf("Failed to find primary address for interface '%s'", interfaceName)
}

func (r ipResolver) GetPrimaryIPv4(interfaceName string) (*gonet.IPNet, error) {
addrs, err := r.ifaceToAddrsFunc(interfaceName)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions platform/net/routes_searcher_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Route struct {

type RoutesSearcher interface {
SearchRoutes() ([]Route, error)
SearchIPv6Routes() ([]Route, error)
}

const DefaultAddress = `0.0.0.0`
Expand Down
23 changes: 23 additions & 0 deletions platform/net/routes_searcher_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@ func (s cmdRoutesSearcher) SearchRoutes() ([]Route, error) {

return routes, nil
}

func (s cmdRoutesSearcher) SearchIPv6Routes() ([]Route, error) {
stdout, _, _, err := s.runner.RunCommandQuietly("ip", "-6", "r")
if err != nil {
return []Route{}, bosherr.WrapError(err, "Running route")
}

routeEntries := strings.Split(stdout, "\n")
routes := make([]Route, 0, len(routeEntries))
for _, routeEntry := range routeEntries {
if len(routeEntry) == 0 {
continue
}
route, err := parseRoute(routeEntry)
if err != nil {
s.logger.Warn("SearchRoutes", "parseRoute error for route '%s': %s", routeEntry, err.Error())
continue
}
routes = append(routes, route)
}

return routes, nil
}
14 changes: 9 additions & 5 deletions settings/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,15 @@ func (s *settingsService) GetSettings() Settings {
continue
}

resolvedNetwork, err := s.resolveNetwork(network)
if err != nil {
break
if network.Prefix == "32" || network.Prefix == "128" || network.Prefix == "" {
resolvedNetwork, err := s.resolveNetwork(network)
if err != nil {
break
}
settingsCopy.Networks[networkName] = resolvedNetwork
} else {
settingsCopy.Networks[networkName] = network
}

settingsCopy.Networks[networkName] = resolvedNetwork
}
return settingsCopy
}
Expand All @@ -275,6 +278,7 @@ func (s *settingsService) InvalidateSettings() error {
}

func (s *settingsService) resolveNetwork(network Network) (Network, error) {
// TODO: check if the n/w has a prefix, check if it is ipv4 or 6, curl metadata server for the user-data
// Ideally this would be GetNetworkByMACAddress(mac string)
// Currently, we are relying that if the default network does not contain
// the MAC adddress the InterfaceConfigurationCreator will fail.
Expand Down
1 change: 1 addition & 0 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ type Network struct {

Default []string `json:"default"`
DNS []string `json:"dns"`
Prefix string `json:"prefix"`

Mac string `json:"mac"`

Expand Down