Skip to content

Commit d21346c

Browse files
committed
Added support for creating endpoints in other namespaces for CNI
1 parent 99e37e8 commit d21346c

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

network/endpoint.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ type EndpointInfo struct {
4444
// NewEndpoint creates a new endpoint in the network.
4545
func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
4646
var containerIf *net.Interface
47+
var ns *Namespace
4748
var ep *endpoint
4849
var err error
4950

51+
log.Printf("[net] Creating endpoint %v in network %v.", epInfo.Id, nw.Id)
52+
5053
if nw.Endpoints[epInfo.Id] != nil {
5154
return nil, errEndpointExists
5255
}
@@ -58,15 +61,14 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
5861
return nil, err
5962
}
6063

61-
log.Printf("[net] Creating endpoint %v in network %v.", epInfo.Id, nw.Id)
62-
6364
// Create a veth pair.
6465
hostIfName := fmt.Sprintf("%s%s", hostInterfacePrefix, epInfo.Id[:7])
6566
contIfName := fmt.Sprintf("%s%s-2", hostInterfacePrefix, epInfo.Id[:7])
6667

6768
log.Printf("[net] Creating veth pair %v %v.", hostIfName, contIfName)
6869
err = netlink.AddVethPair(contIfName, hostIfName)
6970
if err != nil {
71+
log.Printf("[net] Failed to create veth pair, err:%v.", err)
7072
return nil, err
7173
}
7274

@@ -105,6 +107,31 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
105107
goto cleanup
106108
}
107109

110+
// If a network namespace for the container interface is specified...
111+
if epInfo.NetNsPath != "" {
112+
// Open the network namespace.
113+
log.Printf("[net] Opening netns %v.", epInfo.NetNsPath)
114+
ns, err = OpenNamespace(epInfo.NetNsPath)
115+
if err != nil {
116+
goto cleanup
117+
}
118+
defer ns.Close()
119+
120+
// Move the container interface to container's network namespace.
121+
log.Printf("[net] Setting link %v netns %v.", contIfName, epInfo.NetNsPath)
122+
err = netlink.SetLinkNetNs(contIfName, ns.GetFd())
123+
if err != nil {
124+
goto cleanup
125+
}
126+
127+
// Enter the container network namespace.
128+
log.Printf("[net] Entering netns %v.", epInfo.NetNsPath)
129+
err = ns.Enter()
130+
if err != nil {
131+
goto cleanup
132+
}
133+
}
134+
108135
// If a name for the container interface is specified...
109136
if epInfo.IfName != "" {
110137
// Interface needs to be down before renaming.
@@ -137,6 +164,16 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
137164
goto cleanup
138165
}
139166

167+
// If inside the container network namespace...
168+
if ns != nil {
169+
// Return to host network namespace.
170+
log.Printf("[net] Exiting netns %v.", epInfo.NetNsPath)
171+
err = ns.Exit()
172+
if err != nil {
173+
goto cleanup
174+
}
175+
}
176+
140177
// Create the endpoint object.
141178
ep = &endpoint{
142179
Id: epInfo.Id,

0 commit comments

Comments
 (0)