Skip to content

Commit 1d30ab3

Browse files
committed
Implemented Windows network and endpoint configuration
1 parent c6b708f commit 1d30ab3

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

network/endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
4444
var ep *endpoint
4545
var err error
4646

47-
log.Printf("[net] Creating endpoint %v in network %v.", epInfo.Id, nw.Id)
47+
log.Printf("[net] Creating endpoint %+v in network %v.", epInfo, nw.Id)
4848

4949
if nw.Endpoints[epInfo.Id] != nil {
5050
err = errEndpointExists

network/endpoint_windows.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package network
77

88
import (
99
"encoding/json"
10+
"net"
11+
"strings"
1012

1113
"github.com/Azure/azure-container-networking/log"
1214
"github.com/Microsoft/hcsshim"
@@ -16,7 +18,29 @@ import (
1618
func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
1719
// Initialize HNS endpoint.
1820
hnsEndpoint := &hcsshim.HNSEndpoint{
19-
Name: epInfo.Id,
21+
Name: epInfo.Id,
22+
VirtualNetwork: nw.HnsId,
23+
DNSSuffix: epInfo.DNSSuffix,
24+
DNSServerList: strings.Join(epInfo.DNSServers, ","),
25+
}
26+
27+
// HNS currently supports only one IP address per endpoint.
28+
if epInfo.IPAddresses != nil {
29+
hnsEndpoint.IPAddress = epInfo.IPAddresses[0].IP
30+
pl, _ := epInfo.IPAddresses[0].Mask.Size()
31+
hnsEndpoint.PrefixLength = uint8(pl)
32+
}
33+
34+
// HNS currently supports only one (default) route per endpoint.
35+
for _, route := range epInfo.Routes {
36+
var pl int
37+
if route.Dst.Mask != nil {
38+
pl, _ = route.Dst.Mask.Size()
39+
}
40+
if route.Dst.Mask == nil || pl == 0 {
41+
hnsEndpoint.GatewayAddress = route.Gw.String()
42+
break
43+
}
2044
}
2145

2246
// Marshal the request.
@@ -34,12 +58,26 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
3458
return nil, err
3559
}
3660

61+
// Attach the endpoint.
62+
log.Printf("[net] Attaching endpoint %v to container %v.", hnsResponse.Id, epInfo.ContainerID)
63+
err = hcsshim.HotAttachEndpoint(epInfo.ContainerID, hnsResponse.Id)
64+
if err != nil {
65+
log.Printf("[net] Failed to attach endpoint: %v.", err)
66+
return nil, err
67+
}
68+
3769
// Create the endpoint object.
3870
ep := &endpoint{
3971
Id: epInfo.Id,
4072
HnsId: hnsResponse.Id,
73+
SandboxKey: epInfo.ContainerID,
74+
IfName: epInfo.IfName,
75+
IPAddresses: epInfo.IPAddresses,
76+
Gateways: []net.IP{net.ParseIP(hnsEndpoint.GatewayAddress)},
4177
}
4278

79+
ep.MacAddress, _ = net.ParseMAC(hnsResponse.MacAddress)
80+
4381
return ep, nil
4482
}
4583

network/network_windows.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
const (
1616
// HNS network types.
17-
HnsL2bridge = "l2bridge"
18-
HnsL2tunnel = "l2tunnel"
17+
hnsL2bridge = "l2bridge"
18+
hnsL2tunnel = "l2tunnel"
1919
)
2020

2121
// Windows implementation of route.
@@ -26,18 +26,24 @@ func (nm *networkManager) newNetworkImpl(nwInfo *NetworkInfo, extIf *externalInt
2626
// Initialize HNS network.
2727
hnsNetwork := &hcsshim.HNSNetwork{
2828
Name: nwInfo.Id,
29-
Type: HnsL2bridge,
3029
NetworkAdapterName: extIf.Name,
31-
SourceMac: extIf.MacAddress.String(),
32-
DNSSuffix: "",
33-
DNSServerList: "10.1.1.2",
30+
}
31+
32+
// Set network mode.
33+
switch nwInfo.Mode {
34+
case opModeBridge:
35+
hnsNetwork.Type = hnsL2bridge
36+
case opModeTunnel:
37+
hnsNetwork.Type = hnsL2tunnel
38+
default:
39+
return nil, errNetworkModeInvalid
3440
}
3541

3642
// Populate subnets.
3743
for _, subnet := range nwInfo.Subnets {
3844
hnsSubnet := hcsshim.Subnet{
3945
AddressPrefix: subnet,
40-
GatewayAddress: "10.1.1.1",
46+
GatewayAddress: extIf.IPv4Gateway.String(),
4147
}
4248

4349
hnsNetwork.Subnets = append(hnsNetwork.Subnets, hnsSubnet)

0 commit comments

Comments
 (0)