-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhost.go
142 lines (127 loc) · 3.34 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2017 Che Wei, Lin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tinynet
import (
"net"
"path/filepath"
"github.com/containernetworking/plugins/pkg/ip"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
log "github.com/sirupsen/logrus"
)
// Host is a host instance
type Host struct {
NodeType string
Name string
VethName string
IfName string
Sandbox string
IP string
MAC string
}
// NewHost for creating a network namespace
func NewHost(name string) (*Host, error) {
h := new(Host)
h.NodeType = "Host"
h.Name = name
// Create a network namespace
targetNs, err := testutils.NewNS()
if err != nil {
log.Fatal("failed to open netns: ", err)
return nil, err
}
// log.Info("netns mouted into the host: ", targetNs.Path())
log.Infof("Adding a host: %s, namespace: %s", h.Name, filepath.Base(targetNs.Path()))
h.Sandbox = targetNs.Path()
return h, nil
}
// NewContainer for creating a docker container
func NewContainer(name string, imageRef string) (*Host, error) {
h := new(Host)
h.NodeType = "Host"
h.Name = name
_, sandboxKey, err := ensureDocker(imageRef)
if err != nil {
log.Fatal("failed to start container: ", err)
return nil, err
}
h.Sandbox = sandboxKey
log.Info("netns mouted into the host: ", h.Sandbox)
return h, nil
}
func (h *Host) setupVeth(ifName string, mtu int) (*Host, error) {
// Get network namespace object
netns, err := ns.GetNS(h.Sandbox)
if err != nil {
log.Fatal("failed to open netns: ", err)
}
defer netns.Close()
// attach network namespace and setup veth pair
err = netns.Do(func(hostNS ns.NetNS) error {
// create the veth pair in the container and move host end into host netns
hostVeth, containerVeth, err := ip.SetupVeth(ifName, mtu, hostNS)
if err != nil {
return err
}
// Host interface name
h.IfName = containerVeth.Name
// h.mac = containerVeth.HardwareAddr.String()
// Host veth name
h.VethName = hostVeth.Name
// ip link set lo up
_, err = ifaceUp("lo")
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return h, nil
}
func (h *Host) setIfaceIP(address string) error {
// Get network namespace object
netns, err := ns.GetNS(h.Sandbox)
if err != nil {
log.Fatal("failed to open netns: ", err)
}
defer netns.Close()
ipv4Addr, _, err := net.ParseCIDR(address)
if err != nil {
return err
}
h.IP = ipv4Addr.String()
err = netns.Do(func(hostNS ns.NetNS) error {
if err := setIP(h.IfName, address); err != nil {
return err
}
// ip link set ifName up
_, err := ifaceUp(h.IfName)
if err != nil {
return err
}
// get Host mac address
hostIface, err := net.InterfaceByName(h.IfName)
if err != nil {
return err
}
h.MAC = hostIface.HardwareAddr.String()
return nil
})
if err != nil {
return err
}
return nil
}