This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
202 lines (178 loc) · 6.46 KB
/
client.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright 2016 Mirantis
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 cni
import (
"fmt"
"github.com/containernetworking/cni/libcni"
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
"github.com/davecgh/go-spew/spew"
"github.com/golang/glog"
"github.com/Mirantis/virtlet/pkg/nsfix"
"github.com/Mirantis/virtlet/pkg/utils"
)
// Client provides an interface to CNI plugins.
type Client interface {
// AddSandboxToNetwork adds a pod sandbox to the CNI network.
AddSandboxToNetwork(podID, podName, podNs string) (*cnicurrent.Result, error)
// RemoveSandboxFromNetwork removes a pod sandbox from the CNI network.
RemoveSandboxFromNetwork(podID, podName, podNs string) error
// GetDummyNetwork creates a dummy network using CNI plugin.
// It's used for making a dummy gateway for Calico CNI plugin.
// It returns a CNI result and a path to the network namespace.
GetDummyNetwork() (*cnicurrent.Result, string, error)
}
// client provides an implementation of Client interface.
type client struct {
pluginsDir string
configsDir string
}
var _ Client = &client{}
// NewClient returns a client perpared to call plugins in `pluginsDir`
// using configurations found in `configsDir`.
func NewClient(pluginsDir, configsDir string) (*client, error) {
return &client{
pluginsDir: pluginsDir,
configsDir: configsDir,
}, nil
}
// GetDummyNetwork implements GetDummyNetwork method of Client interface.
func (c *client) GetDummyNetwork() (*cnicurrent.Result, string, error) {
// TODO: virtlet pod restarts should not grab another address for
// the gateway. That's not a big problem usually though
// as the IPs are not returned to Calico so both old
// IPs on existing VMs and new ones should work.
podID := utils.NewUUID()
if err := CreateNetNS(podID); err != nil {
return nil, "", fmt.Errorf("couldn't create netns for fake pod %q: %v", podID, err)
}
r, err := c.AddSandboxToNetwork(podID, "", "")
if err != nil {
return nil, "", fmt.Errorf("couldn't set up CNI for fake pod %q: %v", podID, err)
}
return r, PodNetNSPath(podID), nil
}
// AddSandboxToNetwork implements AddSandboxToNetwork method of Client interface.
func (c *client) AddSandboxToNetwork(podID, podName, podNs string) (*cnicurrent.Result, error) {
var r cnicurrent.Result
if err := nsfix.NewCall("cniAddSandboxToNetwork").
Arg(cniRequest{
PluginsDir: c.pluginsDir,
ConfigsDir: c.configsDir,
PodID: podID,
PodName: podName,
PodNs: podNs,
}).
SpawnInNamespaces(&r); err != nil {
return nil, err
}
return &r, nil
}
// RemoveSandboxFromNetwork implements RemoveSandboxFromNetwork method of Client interface.
func (c *client) RemoveSandboxFromNetwork(podID, podName, podNs string) error {
return nsfix.NewCall("cniRemoveSandboxFromNetwork").
Arg(cniRequest{
PluginsDir: c.pluginsDir,
ConfigsDir: c.configsDir,
PodID: podID,
PodName: podName,
PodNs: podNs,
}).
SpawnInNamespaces(nil)
}
type cniRequest struct {
PluginsDir string
ConfigsDir string
PodID string
PodName string
PodNs string
}
type realClient struct {
cniConfig *libcni.CNIConfig
netConfigList *libcni.NetworkConfigList
}
func newRealclient(pluginsDir, configsDir string) (*realClient, error) {
netConfigList, err := ReadConfiguration(configsDir)
if err != nil {
return nil, fmt.Errorf("failed to read CNI configuration %q: %v", configsDir, err)
}
glog.V(3).Infof("CNI config: name: %q type: %q", netConfigList.Plugins[0].Network.Name, netConfigList.Plugins[0].Network.Type)
return &realClient{
cniConfig: &libcni.CNIConfig{Path: []string{pluginsDir}},
netConfigList: netConfigList,
}, nil
}
func (c *realClient) cniRuntimeConf(podID, podName, podNs string) *libcni.RuntimeConf {
r := &libcni.RuntimeConf{
ContainerID: podID,
NetNS: PodNetNSPath(podID),
IfName: "virtlet-eth0",
}
if podName != "" && podNs != "" {
r.Args = [][2]string{
{"IgnoreUnknown", "1"},
{"K8S_POD_NAMESPACE", podNs},
{"K8S_POD_NAME", podName},
{"K8S_POD_INFRA_CONTAINER_ID", podID},
}
}
return r
}
func handleAddSandboxToNetwork(arg interface{}) (interface{}, error) {
req := arg.(*cniRequest)
c, err := newRealclient(req.PluginsDir, req.ConfigsDir)
if err != nil {
return nil, err
}
rtConf := c.cniRuntimeConf(req.PodID, req.PodName, req.PodNs)
// NOTE: this annotation is only need by CNI Genie
rtConf.Args = append(rtConf.Args, [2]string{
"K8S_ANNOT", `{"cni": "calico"}`,
})
glog.V(3).Infof("AddSandboxToNetwork: PodID %q, PodName %q, PodNs %q, runtime config:\n%s",
req.PodID, req.PodName, req.PodNs, spew.Sdump(rtConf))
result, err := c.cniConfig.AddNetworkList(c.netConfigList, rtConf)
if err == nil {
glog.V(3).Infof("AddSandboxToNetwork: PodID %q, PodName %q, PodNs %q: result:\n%s",
req.PodID, req.PodName, req.PodNs, spew.Sdump(result))
} else {
glog.Errorf("AddSandboxToNetwork: PodID %q, PodName %q, PodNs %q: error: %v",
req.PodID, req.PodName, req.PodNs, err)
return nil, err
}
r, err := cnicurrent.NewResultFromResult(result)
if err != nil {
return nil, fmt.Errorf("error converting CNI result to the current version: %v", err)
}
return r, err
}
func handleRemoveSandboxFromNetwork(arg interface{}) (interface{}, error) {
req := arg.(*cniRequest)
c, err := newRealclient(req.PluginsDir, req.ConfigsDir)
if err != nil {
return nil, err
}
glog.V(3).Infof("RemoveSandboxFromNetwork: PodID %q, PodName %q, PodNs %q", req.PodID, req.PodName, req.PodNs)
err = c.cniConfig.DelNetworkList(c.netConfigList, c.cniRuntimeConf(req.PodID, req.PodName, req.PodNs))
if err == nil {
glog.V(3).Infof("RemoveSandboxFromNetwork: PodID %q, PodName %q, PodNs %q: success",
req.PodID, req.PodName, req.PodNs)
} else {
glog.Errorf("RemoveSandboxFromNetwork: PodID %q, PodName %q, PodNs %q: error: %v",
req.PodID, req.PodName, req.PodNs, err)
}
return nil, err
}
func init() {
nsfix.RegisterReexec("cniAddSandboxToNetwork", handleAddSandboxToNetwork, cniRequest{})
nsfix.RegisterReexec("cniRemoveSandboxFromNetwork", handleRemoveSandboxFromNetwork, cniRequest{})
}