-
Notifications
You must be signed in to change notification settings - Fork 51
/
handler.go
208 lines (167 loc) · 7.35 KB
/
handler.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
package kubernetesmonitor
import (
"context"
"fmt"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/monitor/constants"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
api "k8s.io/api/core/v1"
)
// General logic for handling logic fron the DockerMonitor ss the following:
// The only interesting event is the Start and Die event. All the other events are ignored
// Those events are then put together with the Pod events received from the Kubernetes API.
// Once both are received and are consistent, the Pod get activated.
// HandlePUEvent is called by all monitors when a PU event is generated. The implementer
// is responsible to update all components by explicitly adding a new PU.
// Specifically for Kubernetes, The monitor handles the downstream events from Docker.
func (m *KubernetesMonitor) HandlePUEvent(ctx context.Context, puID string, event common.Event, dockerRuntime policy.RuntimeReader) error {
zap.L().Debug("dockermonitor event", zap.String("puID", puID), zap.String("eventType", string(event)))
var kubernetesRuntime policy.RuntimeReader
// If the event coming from DockerMonitor is start or create, we will get a meaningful PURuntime from
// DockerMonitor. We can use it and combine it with the pod information on Kubernetes API.
if event == common.EventStart || event == common.EventCreate {
// We check first if this is a Kubernetes managed container
podNamespace, podName, err := getKubernetesInformation(dockerRuntime)
if err != nil {
return err
}
// We get the information for that specific POD from Kubernetes API
pod, err := m.getPod(podNamespace, podName)
if err != nil {
return err
}
// The KubernetesMetadataExtractor combines the information coming from Docker (runtime)
// and from Kube (pod) in order to create a KubernetesRuntime.
// The managedContainer parameters define if this container should be ignored.
var managedContainer bool
kubernetesRuntime, managedContainer, err = m.kubernetesExtractor(dockerRuntime, pod)
if err != nil {
return fmt.Errorf("error while processing Kubernetes pod %s/%s for container %s %s", podNamespace, podName, puID, err)
}
// UnmanagedContainers are simply ignored. No policy is associated.
if !managedContainer {
// for Unmanaged container check if host network and add the process to cgroup.
zap.L().Debug("unmanaged Kubernetes container on create or start", zap.String("puID", puID), zap.String("podNamespace", podNamespace), zap.String("podName", podName))
return m.decorateRuntime(puID, dockerRuntime, event, podName, podNamespace)
}
// We keep the cache uptoDate for future queries
m.cache.updatePUIDCache(podNamespace, podName, puID, dockerRuntime, kubernetesRuntime)
} else {
// We check if this PUID was previously managed. We only sent the event upstream to the resolver if it was managed on create or start.
kubernetesRuntime = m.cache.getKubernetesRuntimeByPUID(puID)
if kubernetesRuntime == nil {
zap.L().Debug("unmanaged Kubernetes container", zap.String("puID", puID), zap.String("event", string(event)))
return nil
}
}
if event == common.EventDestroy {
// Time to kill the cache entry
m.cache.deletePUIDCache(puID)
}
// The event is then sent to the upstream policyResolver
if err := m.handlers.Policy.HandlePUEvent(ctx, puID, event, kubernetesRuntime); err != nil {
zap.L().Error("Unable to resolve policy for puid", zap.String("puID", puID), zap.Error(err))
return fmt.Errorf("Unable to resolve policy for puid:%s", puID)
}
if dockerRuntime.PUType() == common.LinuxProcessPU {
return m.decorateRuntime(puID, dockerRuntime, event, "", "")
}
return nil
}
// RefreshPUs is used to resend an update event to the Upstream Policy Resolver in case of an update is needed.
func (m *KubernetesMonitor) RefreshPUs(ctx context.Context, pod *api.Pod) error {
if pod == nil {
return fmt.Errorf("pod is nil")
}
podNamespace := pod.GetNamespace()
podName := pod.GetName()
puIDs := m.cache.getPUIDsbyPod(podNamespace, podName)
for _, puid := range puIDs {
dockerRuntime := m.cache.getDockerRuntimeByPUID(puid)
if dockerRuntime == nil {
continue
}
kubernetesRuntime, managedContainer, err := m.kubernetesExtractor(dockerRuntime, pod)
if err != nil {
return fmt.Errorf("error while processing Kubernetes pod %s/%s for container %s %s", podNamespace, podName, puid, err)
}
// UnmanagedContainers are simply ignored. It should not come this far if it is a non managed container anyways.
if !managedContainer {
zap.L().Debug("unmanaged Kubernetes container", zap.String("puID", puid), zap.String("podNamespace", podNamespace), zap.String("podName", podName))
continue
}
// We keep the cache uptoDate for future queries
m.cache.updatePUIDCache(podNamespace, podName, puid, dockerRuntime, kubernetesRuntime)
if err := m.handlers.Policy.HandlePUEvent(ctx, puid, common.EventUpdate, kubernetesRuntime); err != nil {
return err
}
}
return nil
}
// getKubernetesInformation returns the name and namespace from a standard Docker runtime, if the docker container is associated at all with Kubernetes
func getKubernetesInformation(runtime policy.RuntimeReader) (string, string, error) {
podNamespace, ok := runtime.Tag(KubernetesPodNamespaceIdentifier)
if !ok {
return "", "", fmt.Errorf("Error getting Kubernetes Pod namespace")
}
podName, ok := runtime.Tag(KubernetesPodNameIdentifier)
if !ok {
return "", "", fmt.Errorf("Error getting Kubernetes Pod name")
}
return podNamespace, podName, nil
}
// decorateRuntime decorates the docker runtime with puid of the pause container.
func (m *KubernetesMonitor) decorateRuntime(puID string, runtimeInfo policy.RuntimeReader, event common.Event,
podName, podNamespace string) (err error) {
// Do nothing on other events apart from start event.
if event != common.EventStart {
return nil
}
puRuntime, ok := runtimeInfo.(*policy.PURuntime)
if !ok {
zap.L().Error("Found invalid runtime for puid", zap.String("puid", puID))
return fmt.Errorf("invalid runtime for puid:%s", puID)
}
extensions := policy.ExtendedMap{}
// pause container with host net set to true.
if runtimeInfo.PUType() == common.LinuxProcessPU {
extensions[constants.DockerHostMode] = "true"
extensions[constants.DockerHostPUID] = puID
options := puRuntime.Options()
options.PolicyExtensions = extensions
options.AutoPort = true
// set Options on docker runtime.
puRuntime.SetOptions(options)
return nil
}
pausePUID := ""
puIDs := m.cache.getPUIDsbyPod(podNamespace, podName)
// get the puid of the pause container.
for _, id := range puIDs {
rtm := m.cache.getDockerRuntimeByPUID(id)
if rtm == nil {
continue
}
if isPodInfraContainer(rtm) && rtm.PUType() == common.LinuxProcessPU {
pausePUID = id
break
}
// if the pause container is not host net container, nothing to do.
if isPodInfraContainer(rtm) {
return nil
}
}
extensions[constants.DockerHostPUID] = pausePUID
options := puRuntime.Options()
options.PolicyExtensions = extensions
// set Options on docker runtime.
puRuntime.SetOptions(options)
return nil
}
// isPodInfraContainer returns true if the runtime represents the infra container for the POD
func isPodInfraContainer(runtime policy.RuntimeReader) bool {
// The Infra container can be found by checking env. variable.
tagContent, ok := runtime.Tag(KubernetesContainerNameIdentifier)
return ok && tagContent == KubernetesInfraContainerName
}