github.com/coreos/mantle@v0.13.0/network/nsdialer.go (about) 1 // Copyright 2015 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package network 16 17 import ( 18 "net" 19 20 "github.com/vishvananda/netns" 21 22 "github.com/coreos/mantle/system/ns" 23 ) 24 25 // NsDialer is a RetryDialer that can enter any network namespace. 26 type NsDialer struct { 27 RetryDialer 28 NsHandle netns.NsHandle 29 } 30 31 func NewNsDialer(ns netns.NsHandle) *NsDialer { 32 return &NsDialer{ 33 RetryDialer: RetryDialer{ 34 Dialer: net.Dialer{ 35 Timeout: DefaultTimeout, 36 KeepAlive: DefaultKeepAlive, 37 }, 38 Retries: DefaultRetries, 39 }, 40 NsHandle: ns, 41 } 42 } 43 44 func (d *NsDialer) Dial(network, address string) (net.Conn, error) { 45 nsExit, err := ns.Enter(d.NsHandle) 46 if err != nil { 47 return nil, err 48 } 49 defer nsExit() 50 51 return d.RetryDialer.Dial(network, address) 52 }