/
cri.go
332 lines (290 loc) · 9.63 KB
/
cri.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2018-2019 Authors of Cilium
//
// 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 workloads
import (
"context"
"errors"
"fmt"
"net"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/common/addressing"
"github.com/cilium/cilium/pkg/endpoint"
endpointid "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/containerd/containerd/namespaces"
criStore "github.com/containerd/cri/pkg/store"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
criRuntime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/kubernetes/pkg/kubelet/util"
)
func getGRPCCLient(ctx context.Context) (*grpc.ClientConn, error) {
ep, ok := ctx.Value(EpOpt).(string)
if !ok {
return nil, fmt.Errorf("unknown runtime endpoint")
}
log.Debugf("using CRI endpoint %s", ep)
addr, dialer, err := util.GetAddressAndDialer(ep)
if err != nil {
return nil, err
}
c, cancel := context.WithTimeout(ctx, time.Duration(5*time.Second))
defer cancel()
conn, err := grpc.DialContext(c, addr, grpc.WithDialer(dialer), grpc.WithInsecure(), grpc.WithBackoffMaxDelay(15*time.Second))
if err != nil {
return nil, fmt.Errorf("failed to connect: %s", err)
}
return conn, nil
}
type criClient struct {
criRuntime.RuntimeServiceClient
}
func newCRIClient(ctx context.Context) (*criClient, error) {
cc, err := getGRPCCLient(ctx)
if err != nil {
return nil, err
}
rsc := criRuntime.NewRuntimeServiceClient(cc)
return &criClient{rsc}, nil
}
// IsRunning returns false if the provided endpoint cannot be associated with a
// running workload. The runtime must be reachable to make this decision.
func (c *criClient) IsRunning(ep *endpoint.Endpoint) bool {
if c == nil {
return false
}
podID := ep.GetContainerID()
if podID == "" {
return false
}
pssr := criRuntime.PodSandboxStatusRequest{
PodSandboxId: podID,
}
ns := namespaces.WithNamespace(context.Background(), k8sContainerdNamespace)
cont, err := c.RuntimeServiceClient.PodSandboxStatus(ns, &pssr)
if err == criStore.ErrNotExist {
return false
}
if err != nil {
log.WithError(err).Debugf("unable to get pod sandbox status of %v", pssr)
return false
}
return cont.GetStatus().State == criRuntime.PodSandboxState_SANDBOX_READY
}
// Status returns the status of the workload runtime
func (c *criClient) Status() *models.Status {
if c == nil {
return workloadStatusDisabled
}
sreq := &criRuntime.StatusRequest{
Verbose: false,
}
sresp, err := c.RuntimeServiceClient.Status(context.Background(), sreq)
if err != nil {
return &models.Status{State: models.StatusStateFailure, Msg: err.Error()}
}
for _, runtimeCondition := range sresp.Status.Conditions {
if !runtimeCondition.Status {
return &models.Status{State: models.StatusStateFailure, Msg: runtimeCondition.Message}
}
}
return &models.Status{State: models.StatusStateOk, Msg: "cri daemon: Ok"}
}
// EnableEventListener watches for containerD events. Performs the plumbing for the
// containers started or dead.
func (c *criClient) EnableEventListener() (chan<- *EventMessage, error) {
if c == nil {
log.Debug("Not enabling CRI event listener because CRI client is nil")
return nil, nil
}
log.Info("Enabling CRI event listener")
ws := newWatcherState()
eventsCh := make(chan *EventMessage, 100)
go func(state *watcherState, eventsCh <-chan *EventMessage) {
for event := range eventsCh {
ws.enqueueByContainerID(event.WorkloadID, event)
}
}(ws, eventsCh)
return eventsCh, nil
}
func (c *criClient) processEvent(m EventMessage) {
switch m.EventType {
case EventTypeStart:
req := &criRuntime.PodSandboxStatusRequest{
PodSandboxId: m.WorkloadID,
}
_, err := c.PodSandboxStatus(context.Background(), req)
if err != nil {
// ignore containers if not found
// startIgnoringContainer(m.WorkloadID)
log.WithError(err).Debugf("Unable to get more details for workload %s", m.WorkloadID)
return
}
stopIgnoringContainer(m.WorkloadID)
c.handleCreateWorkload(m.WorkloadID, true)
case EventTypeDelete:
Owner().DeleteEndpoint(endpointid.NewID(endpointid.ContainerIdPrefix, m.WorkloadID))
}
}
func (c *criClient) processEvents(events chan EventMessage) {
for m := range events {
if m.WorkloadID != "" {
log.WithFields(logrus.Fields{
logfields.ContainerID: shortContainerID(m.WorkloadID),
}).Debug("Processing event for Container")
c.processEvent(m)
}
}
}
func (c *criClient) getEndpointByPodIP(pod *criRuntime.PodSandboxStatus) *endpoint.Endpoint {
scopedLog := log.WithField(logfields.ContainerID, shortContainerID(pod.GetId()))
if ciliumIP := c.getCiliumIP(pod); ciliumIP != nil {
id := endpointid.NewIPPrefixID(ciliumIP.IP())
if ep, err := endpointmanager.Lookup(id); err != nil {
log.WithError(err).Warning("Unable to lookup endpoint by IP prefix")
} else if ep != nil {
return ep
}
}
scopedLog.Debug("IP address assigned by Cilium could not be derived from pod")
return nil
}
func (c *criClient) getCiliumIP(pod *criRuntime.PodSandboxStatus) addressing.CiliumIP {
ip := net.ParseIP(pod.GetNetwork().GetIp())
if ip == nil {
return nil
}
if ip.To4() == nil {
return addressing.DeriveCiliumIPv4(ip)
}
return addressing.DeriveCiliumIPv6(ip)
}
func (c *criClient) handleCreateWorkload(id string, retry bool) {
scopedLog := log.WithFields(logrus.Fields{
logfields.ContainerID: shortContainerID(id),
fieldMaxRetry: EndpointCorrelationMaxRetries,
"willRetry": retry,
})
for try := 1; try <= EndpointCorrelationMaxRetries; try++ {
retryLog := scopedLog.WithFields(logrus.Fields{
"retry": try,
"podID": id,
})
if try > 1 {
if retry {
retryLog.Debug("Waiting for endpoint representing pod to appear")
time.Sleep(EndpointCorrelationSleepTime(try))
} else {
break
}
}
ctx := namespaces.WithNamespace(context.Background(), k8sContainerdNamespace)
pssr := criRuntime.PodSandboxStatusRequest{
PodSandboxId: id,
}
cont, err := c.RuntimeServiceClient.PodSandboxStatus(ctx, &pssr)
if err != nil {
retryLog.WithError(err).Debugf("Unable to inspect pod %s after pod create event", id)
continue
}
pod := cont.GetStatus()
if pod.GetId() == "" {
retryLog.Warn("Container name not set in event from containerD")
}
ep := endpointmanager.LookupContainerID(id)
if ep == nil {
// Container ID is not yet known; try and find endpoint
// via one of the IP addresses assigned.
ep = c.getEndpointByPodIP(pod)
}
if ep == nil {
// Endpoint does not exist yet. This indicates that the
// orchestration system has not requested us to handle
// networking for this pod yet (or never will).
// We will retry a couple of times to wait for this to
// happen.
retryLog.Debug("Container event could not be associated with endpoint yet")
continue
}
retryLog.WithFields(logrus.Fields{
logfields.EndpointID: ep.ID,
}).Debug("Associated container event with endpoint")
processCreateWorkload(ep, id, pod.Labels)
return
}
startIgnoringContainer(id)
scopedLog.Info("No request received to manage networking for container")
}
// IgnoreRunningWorkloads checks for already running containers and checks
// their IP address, then adds the containers to the list of ignored containers
// and allocates the IPs they are using to prevent future collisions.
func (c *criClient) IgnoreRunningWorkloads() {
if c == nil {
return
}
req := &criRuntime.ListPodSandboxRequest{}
resp, err := c.RuntimeServiceClient.ListPodSandbox(context.Background(), req)
if err != nil {
log.WithError(err).Error("unable to get list of pods running")
return
}
for _, pod := range resp.GetItems() {
scopedLog := log.WithField(logfields.ContainerID, pod.GetId())
scopedLog.Info("Adding running container to the list of ignored containers")
startIgnoringContainer(pod.GetId())
ctx := namespaces.WithNamespace(context.Background(), k8sContainerdNamespace)
pssr := criRuntime.PodSandboxStatusRequest{
PodSandboxId: pod.GetId(),
}
cont, err := c.RuntimeServiceClient.PodSandboxStatus(ctx, &pssr)
if err != nil {
continue
}
cIP := c.getCiliumIP(cont.GetStatus())
if cIP == nil {
continue
}
allocator.BlacklistIP(cIP.IP(), "ignored container: "+pod.GetId())
//TODO Release this address when the ignored container leaves
scopedLog.WithFields(logrus.Fields{
logfields.IPAddr: cIP.IP(),
}).Info("Found container running with potential " +
"collision IP address, adding to the list " +
"of allocated IPs")
}
}
// workloadIDsList returns a list of running workload IDs.
func (c *criClient) workloadIDsList(ctx context.Context) ([]string, error) {
ctx = namespaces.WithNamespace(ctx, k8sContainerdNamespace)
req := &criRuntime.ListPodSandboxRequest{}
resp, err := c.RuntimeServiceClient.ListPodSandbox(ctx, req)
if err != nil {
return nil, err
}
cont := []string{}
for _, pod := range resp.GetItems() {
if pod.GetId() != "" {
cont = append(cont, pod.GetId())
}
}
return cont, nil
}
// GetAllInfraContainersPID returns a map that maps container IDs to the PID
// of that container.
func (c *criClient) GetAllInfraContainersPID() (map[string]int, error) {
return nil, errors.New("not implemented for cri")
}