-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
103 lines (78 loc) · 2.47 KB
/
monitor.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
package cnimonitor
import (
"context"
"fmt"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/monitor/config"
"go.aporeto.io/trireme-lib/monitor/extractors"
"go.aporeto.io/trireme-lib/monitor/registerer"
)
// Config is the configuration options to start a CNI monitor
type Config struct {
EventMetadataExtractor extractors.EventMetadataExtractor
}
// DefaultConfig provides a default configuration
func DefaultConfig() *Config {
return &Config{
EventMetadataExtractor: DockerMetadataExtractor,
}
}
// SetupDefaultConfig adds defaults to a partial configuration
func SetupDefaultConfig(cniConfig *Config) *Config {
defaultConfig := DefaultConfig()
if cniConfig.EventMetadataExtractor == nil {
cniConfig.EventMetadataExtractor = defaultConfig.EventMetadataExtractor
}
return cniConfig
}
// CniMonitor captures all the monitor processor information
// It implements the EventProcessor interface of the rpc monitor
type CniMonitor struct {
proc *cniProcessor
}
// New returns a new implmentation of a monitor implmentation
func New() *CniMonitor {
return &CniMonitor{
proc: &cniProcessor{},
}
}
// Run implements Implementation interface
func (c *CniMonitor) Run(ctx context.Context) error {
return c.proc.config.IsComplete()
}
// SetupConfig provides a configuration to implmentations. Every implmentation
// can have its own config type.
func (c *CniMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
defaultConfig := DefaultConfig()
if cfg == nil {
cfg = defaultConfig
}
cniConfig, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("Invalid configuration specified")
}
if registerer != nil {
if err := registerer.RegisterProcessor(common.KubernetesPU, c.proc); err != nil {
return err
}
}
// Setup defaults
cniConfig = SetupDefaultConfig(cniConfig)
// Setup configuration
c.proc.metadataExtractor = cniConfig.EventMetadataExtractor
if c.proc.metadataExtractor == nil {
return fmt.Errorf("Unable to setup a metadata extractor")
}
return nil
}
// SetupHandlers sets up handlers for monitors to invoke for various events such as
// processing unit events and synchronization events. This will be called before Start()
// by the consumer of the monitor
func (c *CniMonitor) SetupHandlers(m *config.ProcessorConfig) {
c.proc.config = m
}
// Resync instructs the monitor to do a resync.
func (c *CniMonitor) Resync(ctx context.Context) error {
// TODO: Implement resync
return fmt.Errorf("resync not implemented")
}