This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
cni.go
179 lines (147 loc) · 4.98 KB
/
cni.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Copyright (c) 2016 Intel Corporation
//
// 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 virtcontainers
import (
"fmt"
cniTypes "github.com/containernetworking/cni/pkg/types"
cniV2Types "github.com/containernetworking/cni/pkg/types/020"
cniLatestTypes "github.com/containernetworking/cni/pkg/types/current"
cniPlugin "github.com/containers/virtcontainers/pkg/cni"
"github.com/sirupsen/logrus"
)
// CniPrimaryInterface Name chosen for the primary interface
// If CNI ever support multiple primary interfaces this should be revisited
const CniPrimaryInterface = "eth0"
// cni is a network implementation for the CNI plugin.
type cni struct{}
// Logger returns a logrus logger appropriate for logging cni messages
func (n *cni) Logger() *logrus.Entry {
return virtLog.WithField("subsystem", "cni")
}
func cniDNSToDNSInfo(cniDNS cniTypes.DNS) DNSInfo {
return DNSInfo{
Servers: cniDNS.Nameservers,
Domain: cniDNS.Domain,
Searches: cniDNS.Search,
Options: cniDNS.Options,
}
}
func convertLatestCNIResult(result *cniLatestTypes.Result) NetworkInfo {
return NetworkInfo{
DNS: cniDNSToDNSInfo(result.DNS),
}
}
func convertV2CNIResult(result *cniV2Types.Result) NetworkInfo {
return NetworkInfo{
DNS: cniDNSToDNSInfo(result.DNS),
}
}
func convertCNIResult(cniResult cniTypes.Result) (NetworkInfo, error) {
switch result := cniResult.(type) {
case *cniLatestTypes.Result:
return convertLatestCNIResult(result), nil
case *cniV2Types.Result:
return convertV2CNIResult(result), nil
default:
return NetworkInfo{}, fmt.Errorf("Unknown CNI result type %T", result)
}
}
func (n *cni) invokePluginsAdd(pod Pod, networkNS *NetworkNamespace) (*NetworkInfo, error) {
netPlugin, err := cniPlugin.NewNetworkPlugin()
if err != nil {
return nil, err
}
// Note: In the case of multus or cni-genie this will return only the results
// corresponding to the primary interface. The remaining results need to be
// derived
result, err := netPlugin.AddNetwork(pod.id, networkNS.NetNsPath, CniPrimaryInterface)
if err != nil {
return nil, err
}
netInfo, err := convertCNIResult(result)
if err != nil {
return nil, err
}
// We do not care about this for now but
// If present, the CNI DNS result has to be updated in resolv.conf
// if the kubelet has not supplied it already
n.Logger().Infof("AddNetwork results %s", result.String())
return &netInfo, nil
}
func (n *cni) invokePluginsDelete(pod Pod, networkNS NetworkNamespace) error {
netPlugin, err := cniPlugin.NewNetworkPlugin()
if err != nil {
return err
}
err = netPlugin.RemoveNetwork(pod.id, networkNS.NetNsPath, CniPrimaryInterface)
if err != nil {
return err
}
return nil
}
func (n *cni) updateEndpointsFromScan(networkNS *NetworkNamespace, netInfo *NetworkInfo, config NetworkConfig) error {
endpoints, err := createEndpointsFromScan(networkNS.NetNsPath, config)
if err != nil {
return err
}
for _, endpoint := range endpoints {
if CniPrimaryInterface == endpoint.Name() {
prop := endpoint.Properties()
prop.DNS = netInfo.DNS
endpoint.SetProperties(prop)
break
}
}
networkNS.Endpoints = endpoints
return nil
}
// init initializes the network, setting a new network namespace for the CNI network.
func (n *cni) init(config NetworkConfig) (string, bool, error) {
return initNetworkCommon(config)
}
// run runs a callback in the specified network namespace.
func (n *cni) run(networkNSPath string, cb func() error) error {
return runNetworkCommon(networkNSPath, cb)
}
// add adds all needed interfaces inside the network namespace for the CNI network.
func (n *cni) add(pod Pod, config NetworkConfig, netNsPath string, netNsCreated bool) (NetworkNamespace, error) {
networkNS := NetworkNamespace{
NetNsPath: netNsPath,
NetNsCreated: netNsCreated,
}
netInfo, err := n.invokePluginsAdd(pod, &networkNS)
if err != nil {
return NetworkNamespace{}, err
}
if err := n.updateEndpointsFromScan(&networkNS, netInfo, config); err != nil {
return NetworkNamespace{}, err
}
if err := addNetworkCommon(pod, &networkNS); err != nil {
return NetworkNamespace{}, err
}
return networkNS, nil
}
// remove unbridges and deletes TAP interfaces. It also removes virtual network
// interfaces and deletes the network namespace for the CNI network.
func (n *cni) remove(pod Pod, networkNS NetworkNamespace) error {
if err := removeNetworkCommon(networkNS); err != nil {
return err
}
if err := n.invokePluginsDelete(pod, networkNS); err != nil {
return err
}
return deleteNetNS(networkNS.NetNsPath, true)
}