-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
391 lines (341 loc) · 10.5 KB
/
controller.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package imagescan
import (
"context"
"errors"
"fmt"
"reflect"
"sort"
"sync"
"time"
castaipb "github.com/castai/kvisor/api/v1/runtime"
imagescanconfig "github.com/castai/kvisor/cmd/imagescan/config"
"github.com/castai/kvisor/pkg/metrics"
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
"github.com/castai/kvisor/cmd/controller/kube"
"github.com/castai/kvisor/pkg/logging"
"github.com/samber/lo"
)
type castaiClient interface {
GetSyncState(ctx context.Context, in *castaipb.GetSyncStateRequest, opts ...grpc.CallOption) (*castaipb.GetSyncStateResponse, error)
UpdateSyncState(ctx context.Context, in *castaipb.UpdateSyncStateRequest, opts ...grpc.CallOption) (*castaipb.UpdateSyncStateResponse, error)
}
type Config struct {
Enabled bool
CastaiSecretRefName string
ScanInterval time.Duration `validate:"required"`
ScanTimeout time.Duration
MaxConcurrentScans int64 `validate:"required"`
ScanJobImagePullPolicy string
Mode string
CPURequest string
CPULimit string
MemoryRequest string
MemoryLimit string
ProfileEnabled bool
PhlareEnabled bool
PrivateRegistryPullSecret string
ServiceAccount string
InitDelay time.Duration
CastaiGRPCAddress string
CastaiClusterID string
CastaiGrpcInsecure bool
ImageScanBlobsCacheURL string
CloudProvider string
IgnoredNamespaces []string
}
type ImageScanImage struct {
PullPolicy string `envconfig:"IMAGE_SCAN_IMAGE_PULL_POLICY" yaml:"pullPolicy"`
}
func NewController(
log *logging.Logger,
cfg Config,
imageScanner imageScanner,
client castaiClient,
kubeController kubeClient,
) *Controller {
log = log.WithField("component", "imagescan")
ignoredNamespaces := make(map[string]struct{}, len(cfg.IgnoredNamespaces))
for _, ns := range cfg.IgnoredNamespaces {
ignoredNamespaces[ns] = struct{}{}
}
return &Controller{
imageScanner: imageScanner,
client: client,
kubeController: kubeController,
delta: newDeltaState(kubeController, ignoredNamespaces),
log: log,
cfg: cfg,
timeGetter: timeGetter(),
initialScansDelay: cfg.InitDelay,
}
}
func timeGetter() func() time.Time {
return func() time.Time {
return time.Now().UTC()
}
}
type Controller struct {
delta *deltaState
imageScanner imageScanner
client castaiClient
kubeController kubeClient
log *logging.Logger
cfg Config
timeGetter func() time.Time
initialScansDelay time.Duration
fullSnapshotSent bool
}
func (c *Controller) RequiredTypes() []reflect.Type {
return []reflect.Type{
reflect.TypeOf(&corev1.Pod{}),
reflect.TypeOf(&corev1.Node{}),
}
}
func (c *Controller) Run(ctx context.Context) error {
c.log.Info("running")
defer c.log.Infof("stopping")
// Before starting normal scans and deltas processing
// we need to spend some time processing only deltas to make sure
// we have full images view.
if err := c.waitInitialDeltaQueueSync(ctx); err != nil {
return err
}
scanTicker := time.NewTicker(c.cfg.ScanInterval)
defer scanTicker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-scanTicker.C:
if err := c.scheduleScans(ctx); err != nil {
c.log.Errorf("images scan failed: %v", err)
}
}
}
}
func (c *Controller) waitInitialDeltaQueueSync(ctx context.Context) error {
waitTimeout := time.After(c.initialScansDelay)
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-waitTimeout:
return nil
}
}
}
func (c *Controller) OnAdd(obj kube.Object) {
c.delta.Upsert(obj)
}
func (c *Controller) OnUpdate(obj kube.Object) {
c.delta.Upsert(obj)
}
func (c *Controller) OnDelete(obj kube.Object) {
c.delta.Delete(obj)
}
func (c *Controller) scheduleScans(ctx context.Context) (rerr error) {
c.syncFromRemoteState(ctx)
images := c.delta.GetImagesCopy()
if err := c.updateImageStatuses(ctx, images); err != nil {
c.log.Errorf("sending images resources changes: %v", err)
}
// Scan pending images.
pendingImages := c.findPendingImages(images)
concurrentScans := int(c.cfg.MaxConcurrentScans)
imagesForScan := pendingImages
if len(imagesForScan) > concurrentScans {
imagesForScan = imagesForScan[:concurrentScans]
}
if l := len(imagesForScan); l > 0 {
c.log.Infof("scheduling %d images scans", l)
if err := c.scanImages(ctx, imagesForScan); err != nil {
return err
}
c.log.Info("images scan finished")
} else {
c.log.Debug("skipping images scan, no pending images")
}
return nil
}
func (c *Controller) findPendingImages(images []*image) []*image {
now := c.timeGetter()
privateImagesCount := lo.CountBy(images, func(v *image) bool {
return isImagePrivate(v)
})
pendingImages := lo.Filter(images, func(v *image, _ int) bool {
return isImagePending(v, now)
})
sort.Slice(pendingImages, func(i, j int) bool {
return pendingImages[i].failures < pendingImages[j].failures
})
c.log.Infof("found %d images, pending images %d", len(images), len(pendingImages))
metrics.ControllerImagesCount.Set(float64(len(images)))
metrics.ControllerPendingImagesCount.Set(float64(len(pendingImages)))
if privateImagesCount > 0 {
c.log.Warnf("skipping %d private images", privateImagesCount)
}
return pendingImages
}
func (c *Controller) scanImages(ctx context.Context, images []*image) error {
var wg sync.WaitGroup
for _, img := range images {
if img.name == "" {
return fmt.Errorf("no image name set, image_id=%s", img.id)
}
wg.Add(1)
go func(img *image) {
defer wg.Done()
if ctx.Err() != nil {
return
}
ctx, cancel := context.WithTimeout(ctx, c.cfg.ScanTimeout)
defer cancel()
log := c.log.WithField("image", img.name)
log.Info("scanning image")
if err := c.scanImage(ctx, img); err != nil {
log.Errorf("image scan failed: %v", err)
parsedErr := parseErrorFromLog(err)
c.delta.SetImageScanError(img.key, parsedErr)
if err := c.updateImageStatusAsFailed(ctx, img, parsedErr); err != nil {
c.log.Errorf("sending images resources changes: %v", err)
}
return
}
log.Info("image scan finished")
c.delta.SetImageScanned(img.key)
}(img)
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (c *Controller) scanImage(ctx context.Context, img *image) (rerr error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
agentImageDetails, found := c.kubeController.GetKvisorAgentImageDetails()
if !found {
return errors.New("kvisor image details not found")
}
return c.imageScanner.ScanImage(ctx, ScanImageParams{
ImageName: img.name,
ImageID: img.id,
ContainerRuntime: string(img.containerRuntime),
Mode: string(imagescanconfig.ModeRemote),
ResourceIDs: lo.Keys(img.owners),
DeleteFinishedJob: true,
WaitForCompletion: true,
WaitDurationAfterCompletion: 30 * time.Second,
Architecture: img.architecture,
Os: img.os,
ScanImageDetails: agentImageDetails,
})
}
func (c *Controller) updateImageStatuses(ctx context.Context, images []*image) error {
if c.fullSnapshotSent {
images = lo.Filter(images, func(item *image, index int) bool {
return item.ownerChangedAt.After(item.resourcesUpdatedAt)
})
}
if len(images) == 0 {
return nil
}
now := c.timeGetter()
var imagesChanges []*castaipb.Image
for _, img := range images {
resourceIds := lo.Keys(img.owners)
var updatedStatus castaipb.ImageScanStatus
if isImagePending(img, now) {
updatedStatus = castaipb.ImageScanStatus_IMAGE_SCAN_STATUS_PENDING
}
imagesChanges = append(imagesChanges, &castaipb.Image{
Id: img.id,
Architecture: img.architecture,
ResourceIds: resourceIds,
Name: img.name,
ScanStatus: updatedStatus,
})
}
c.log.Info("sending images sync state")
report := &castaipb.UpdateSyncStateRequest{
FullSnapshot: !c.fullSnapshotSent,
Images: imagesChanges,
}
_, err := c.client.UpdateSyncState(ctx, report)
if err != nil {
return err
}
c.delta.SetResourcesUpdatedAt(images, now)
c.fullSnapshotSent = true
return nil
}
func (c *Controller) updateImageStatusAsFailed(ctx context.Context, image *image, scanJobError error) error {
if image == nil {
return errors.New("image is missing")
}
var errorMsg string
if scanJobError != nil {
errorMsg = scanJobError.Error()
}
updatedImage := &castaipb.Image{
Id: image.id,
Name: image.name,
Architecture: image.architecture,
ScanStatus: castaipb.ImageScanStatus_IMAGE_SCAN_STATUS_SCAN_ERROR,
ScanError: errorMsg,
}
c.log.Info("sending image failed status")
req := &castaipb.UpdateSyncStateRequest{
Images: []*castaipb.Image{updatedImage},
}
_, err := c.client.UpdateSyncState(ctx, req)
return err
}
func (c *Controller) syncFromRemoteState(ctx context.Context) {
images := c.delta.GetImagesCopy()
now := c.timeGetter().UTC()
imagesWithNotSyncedState := lo.Filter(images, func(item *image, index int) bool {
return !item.scanned && item.lastRemoteSyncAt.Before(now.Add(-10*time.Minute))
})
if len(imagesWithNotSyncedState) == 0 {
return
}
imagesIds := lo.Map(imagesWithNotSyncedState, func(item *image, index int) string {
return item.id
})
c.log.Debugf("sync images state from remote")
resp, err := c.client.GetSyncState(ctx, &castaipb.GetSyncStateRequest{ImageIds: imagesIds})
if err != nil {
c.log.Errorf("getting images sync state from remote: %v", err)
return
}
if resp.Images == nil {
return
}
// Set sync state for all these images to prevent constant api calls.
c.delta.UpdateRemoteSyncedAt(imagesWithNotSyncedState, now)
// Set images as scanned from remote response.
c.delta.SetScannedImages(resp.Images.Images)
// If full resources resync is required it will be sent during next scheduled scan.
if resp.Images.FullResyncRequired {
c.fullSnapshotSent = false
}
c.log.Infof("images updated from remote state, full_resync=%v, scanned_images=%d", resp.Images.FullResyncRequired, len(resp.Images.Images))
}
func isImagePending(v *image, now time.Time) bool {
return !v.scanned &&
len(v.owners) > 0 &&
!isImagePrivate(v) &&
(v.nextScan.IsZero() || v.nextScan.Before(now))
}
func isImagePrivate(v *image) bool {
return errors.Is(v.lastScanErr, errPrivateImage)
}