-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
128 lines (100 loc) · 3.78 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
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
package kubernetesmonitor
import (
"context"
"fmt"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/monitor/config"
"go.aporeto.io/trireme-lib/monitor/extractors"
dockermonitor "go.aporeto.io/trireme-lib/monitor/internal/docker"
"go.aporeto.io/trireme-lib/monitor/registerer"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
kubecache "k8s.io/client-go/tools/cache"
)
// KubernetesMonitor implements a monitor that sends pod events upstream
// It is implemented as a filter on the standard DockerMonitor.
// It gets all the PU events from the DockerMonitor and if the container is the POD container from Kubernetes,
// It connects to the Kubernetes API and adds the tags that are coming from Kuberntes that cannot be found
type KubernetesMonitor struct {
dockerMonitor *dockermonitor.DockerMonitor
kubeClient kubernetes.Interface
localNode string
handlers *config.ProcessorConfig
cache *cache
kubernetesExtractor extractors.KubernetesMetadataExtractorType
podStore kubecache.Store
podController kubecache.Controller
podControllerStop chan struct{}
enableHostPods bool
}
// New returns a new kubernetes monitor.
func New() *KubernetesMonitor {
kubeMonitor := &KubernetesMonitor{}
kubeMonitor.cache = newCache()
return kubeMonitor
}
// SetupConfig provides a configuration to implmentations. Every implmentation
// can have its own config type.
func (m *KubernetesMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
defaultConfig := DefaultConfig()
if cfg == nil {
cfg = defaultConfig
}
kubernetesconfig, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("Invalid configuration specified")
}
kubernetesconfig = SetupDefaultConfig(kubernetesconfig)
processorConfig := &config.ProcessorConfig{
Policy: m,
Collector: collector.NewDefaultCollector(),
}
// As the Kubernetes monitor depends on the DockerMonitor, we setup the Docker monitor first
dockerMon := dockermonitor.New()
dockerMon.SetupHandlers(processorConfig)
dockerConfig := dockermonitor.DefaultConfig()
dockerConfig.EventMetadataExtractor = kubernetesconfig.DockerExtractor
// we use the defaultconfig for now
if err := dockerMon.SetupConfig(nil, dockerConfig); err != nil {
return fmt.Errorf("docker monitor instantiation error: %s", err.Error())
}
m.dockerMonitor = dockerMon
// Setting up Kubernetes
m.localNode = kubernetesconfig.Nodename
kubeClient, err := NewKubeClient(kubernetesconfig.Kubeconfig)
if err != nil {
return fmt.Errorf("kubernetes client instantiation error: %s", err.Error())
}
m.kubeClient = kubeClient
m.enableHostPods = kubernetesconfig.EnableHostPods
m.kubernetesExtractor = kubernetesconfig.KubernetesExtractor
m.podStore, m.podController = m.CreateLocalPodController("",
m.addPod,
m.deletePod,
m.updatePod)
m.podControllerStop = make(chan struct{})
zap.L().Warn("Using deprecated Kubernetes Monitor")
return nil
}
// Run starts the monitor.
func (m *KubernetesMonitor) Run(ctx context.Context) error {
if m.kubeClient == nil {
return fmt.Errorf("kubernetes client is not initialized correctly")
}
// TODO. Give directly the channel to the Kubernetes library
go m.podController.Run(ctx.Done())
initialPodSync := make(chan struct{})
go hasSynced(initialPodSync, m.podController)
<-initialPodSync
return m.dockerMonitor.Run(ctx)
}
// 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 (m *KubernetesMonitor) SetupHandlers(c *config.ProcessorConfig) {
m.handlers = c
}
// Resync requests to the monitor to do a resync.
func (m *KubernetesMonitor) Resync(ctx context.Context) error {
return m.dockerMonitor.Resync(ctx)
}