@@ -7,6 +7,8 @@ package network
77
88import (
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 (
1618func (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
0 commit comments