-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathhandler.go
More file actions
113 lines (96 loc) · 3.28 KB
/
handler.go
File metadata and controls
113 lines (96 loc) · 3.28 KB
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
package reprocessor
import (
"github.com/pkg/errors"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/pkg/centralsensor"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/sensor/common"
"github.com/stackrox/rox/sensor/common/admissioncontroller"
"github.com/stackrox/rox/sensor/common/centralcaps"
"github.com/stackrox/rox/sensor/common/detector"
"github.com/stackrox/rox/sensor/common/image/cache"
"github.com/stackrox/rox/sensor/common/message"
"github.com/stackrox/rox/sensor/common/unimplemented"
)
var (
log = logging.LoggerForModule()
)
// Handler handles request to reprocess deployment (sent by Central).
//
//go:generate mockgen-wrapper
type Handler interface {
common.SensorComponent
ProcessReprocessDeployments(*central.ReprocessDeployment) error
ProcessInvalidateImageCache(*central.InvalidateImageCache) error
}
// NewHandler returns a new instance of a deployment reprocessor.
func NewHandler(admCtrlSettingsMgr admissioncontroller.SettingsManager, detector detector.Detector, imageCache cache.Image) Handler {
return &handlerImpl{
admCtrlSettingsMgr: admCtrlSettingsMgr,
detector: detector,
imageCache: imageCache,
stopSig: concurrency.NewErrorSignal(),
}
}
type handlerImpl struct {
unimplemented.Receiver
admCtrlSettingsMgr admissioncontroller.SettingsManager
detector detector.Detector
imageCache cache.Image
stopSig concurrency.ErrorSignal
}
func (h *handlerImpl) Name() string {
return "reprocessor.handlerImpl"
}
func (h *handlerImpl) Start() error {
return nil
}
func (h *handlerImpl) Stop() {
h.stopSig.Signal()
}
func (h *handlerImpl) Notify(common.SensorComponentEvent) {}
func (h *handlerImpl) Capabilities() []centralsensor.SensorCapability {
// A new sensor capability to reprocess deployment has not been added. In case of mismatched upgrades,
// the re-processing is discarded, which is fine.
return []centralsensor.SensorCapability{
centralsensor.TargetedImageCacheInvalidation,
}
}
func (h *handlerImpl) ProcessReprocessDeployments(req *central.ReprocessDeployment) error {
log.Debug("Received request to reprocess deployments from Central")
select {
case <-h.stopSig.Done():
return errors.Wrap(h.stopSig.Err(), "could not fulfill re-process deployment(s) request")
default:
go h.detector.ReprocessDeployments(req.GetDeploymentIds()...)
}
return nil
}
func (h *handlerImpl) ProcessInvalidateImageCache(req *central.InvalidateImageCache) error {
log.Debug("Received request to invalidate image caches")
select {
case <-h.stopSig.Done():
return errors.Wrap(h.stopSig.Err(), "could not fulfill invalidate image cache request")
default:
h.admCtrlSettingsMgr.InvalidateImageCache(req.GetImageKeys())
keysToDelete := make([]cache.Key, 0, len(req.GetImageKeys()))
for _, image := range req.GetImageKeys() {
var key string
if centralcaps.Has(centralsensor.FlattenImageData) {
key = image.GetImageIdV2()
} else {
key = image.GetImageId()
}
if key == "" {
key = image.GetImageFullName()
}
keysToDelete = append(keysToDelete, cache.Key(key))
}
h.imageCache.Remove(keysToDelete...)
}
return nil
}
func (h *handlerImpl) ResponsesC() <-chan *message.ExpiringMessage {
return nil
}