-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathpodmanager_api.go
273 lines (229 loc) · 7.01 KB
/
podmanager_api.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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 podmanager
import (
"fmt"
"net"
controller "github.com/contiv/vpp/plugins/controller/api"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
"github.com/contiv/vpp/plugins/podmanager/cni"
)
/********************************* Plugin API *********************************/
// API defines methods provided by PodManager for use by other plugins.
type API interface {
// GetLocalPods returns all currently locally deployed pods.
// The method should be called only from within the main event loop
// (not thread safe) and not before the startup resync.
GetLocalPods() LocalPods
// GetPods returns all currently deployed pods (local and non-local) in the cluster.
// The method should be called only from within the main event loop
// (not thread safe) and not before the startup resync.
GetPods() Pods
}
// LocalPod represents a locally deployed pod (locally = on this node).
type LocalPod struct {
ID podmodel.ID
ContainerID string
NetworkNamespace string
}
// Pod represents a pod in the cluster (may or may not be local).
type Pod struct {
ID podmodel.ID
IPAddress string
Labels map[string]string
Annotations map[string]string
}
// LocalPods is a map of local pod-ID -> Pod info.
type LocalPods map[podmodel.ID]*LocalPod
// Pods is a map of pod-ID -> Pod info.
type Pods map[podmodel.ID]*Pod
// String returns human-readable string representation of local pod metadata.
func (p *LocalPod) String() string {
return fmt.Sprintf("Pod <ID:%v, Container:%s, Ns:%s>",
p.ID, p.ContainerID, p.NetworkNamespace)
}
// String returns human-readable string representation of pod metadata.
func (p *Pod) String() string {
return fmt.Sprintf("Pod <ID:%v, IP:%v, Labels:%v, Annotations:%v>",
p.ID, p.IPAddress, p.Labels, p.Annotations)
}
// String returns a string representation of the local pods.
func (ps LocalPods) String() string {
str := "{"
first := true
for podID, pod := range ps {
if !first {
str += ", "
}
first = false
str += fmt.Sprintf("%v: %s", podID, pod.String())
}
str += "}"
return str
}
// String returns a string representation of the pods.
func (ps Pods) String() string {
str := "{"
first := true
for podID, pod := range ps {
if !first {
str += ", "
}
first = false
str += fmt.Sprintf("%v: %s", podID, pod.String())
}
str += "}"
return str
}
/******************************* Add Pod Event ********************************/
// AddPod event is triggered when a new pod is being deployed on this node.
type AddPod struct {
result chan error
// input arguments (read by event handlers)
Pod podmodel.ID
ContainerID string
NetworkNamespace string
IPAMType string
IPAMData string
// output arguments (edited by event handlers)
Interfaces []PodInterface
Routes []Route
}
// PodInterface represents a single pod interface.
type PodInterface struct {
HostName string // name of the interface in the host stack
IPAddresses []*IPWithGateway // list of assigned IP addresses
}
// IPWithGateway encapsulates IP address and gateway.
type IPWithGateway struct {
Version IPVersion
Address *net.IPNet // IP with mask combined
Gateway net.IP
}
// IPVersion is either v4 or v6.
type IPVersion int
const (
// IPv4 represents IP version 4.
IPv4 IPVersion = iota
// IPv6 represents IP version 6.
IPv6
)
// Route represents single IP route.
type Route struct {
Network *net.IPNet
Gateway net.IP
}
// NewAddPodEvent is constructor for AddPod event.
func NewAddPodEvent(request *cni.CNIRequest) *AddPod {
extraArgs := parseCniExtraArgs(request.ExtraArguments)
podID := podmodel.ID{
Name: extraArgs[podNameExtraArg],
Namespace: extraArgs[podNamespaceExtraArg],
}
return &AddPod{
Pod: podID,
ContainerID: request.ContainerId,
NetworkNamespace: request.NetworkNamespace,
IPAMType: request.IpamType,
IPAMData: request.IpamData,
result: make(chan error, 1),
}
}
// GetName returns name of the AddPod event.
func (ev *AddPod) GetName() string {
return fmt.Sprintf("Add Pod %s", ev.Pod.String())
}
// String describes AddPod event.
func (ev *AddPod) String() string {
return fmt.Sprintf("%s\n"+
"* Container: %s\n"+
"* Network namespace: %s",
ev.GetName(), ev.ContainerID, ev.NetworkNamespace)
}
// Method is Update.
func (ev *AddPod) Method() controller.EventMethodType {
return controller.Update
}
// TransactionType is RevertOnFailure.
func (ev *AddPod) TransactionType() controller.UpdateTransactionType {
return controller.RevertOnFailure
}
// Direction is forward.
func (ev *AddPod) Direction() controller.UpdateDirectionType {
return controller.Forward
}
// IsBlocking returns true.
func (ev *AddPod) IsBlocking() bool {
return true
}
// Done propagates error to the event producer.
func (ev *AddPod) Done(err error) {
ev.result <- err
return
}
// Wait waits for the result of the AddPod event.
func (ev *AddPod) Wait() error {
return <-ev.result
}
/****************************** Delete Pod Event ******************************/
// DeletePod event is triggered when pod deployed on this node is being terminated.
type DeletePod struct {
result chan error
Pod podmodel.ID
}
// NewDeletePodEvent is constructor for DeletePod event.
func NewDeletePodEvent(request *cni.CNIRequest) *DeletePod {
extraArgs := parseCniExtraArgs(request.ExtraArguments)
podID := podmodel.ID{
Name: extraArgs[podNameExtraArg],
Namespace: extraArgs[podNamespaceExtraArg],
}
return &DeletePod{
Pod: podID,
result: make(chan error, 1),
}
}
// GetName returns name of the DeletePod event.
func (ev *DeletePod) GetName() string {
return fmt.Sprintf("Delete Pod %s", ev.Pod.String())
}
// String describes DeletePod event.
func (ev *DeletePod) String() string {
return ev.GetName()
}
// Method is Update.
func (ev *DeletePod) Method() controller.EventMethodType {
return controller.Update
}
// TransactionType is BestEffortIgnoreErrors.
func (ev *DeletePod) TransactionType() controller.UpdateTransactionType {
return controller.BestEffortIgnoreErrors
}
// Direction is Reverse.
func (ev *DeletePod) Direction() controller.UpdateDirectionType {
return controller.Reverse
}
// IsBlocking returns true.
func (ev *DeletePod) IsBlocking() bool {
return true
}
// Done propagates error to the event producer.
func (ev *DeletePod) Done(err error) {
ev.result <- err
return
}
// Wait waits for the result of the DeletePod event.
func (ev *DeletePod) Wait() error {
return <-ev.result
}