-
Notifications
You must be signed in to change notification settings - Fork 51
/
handler.go
129 lines (104 loc) · 4.9 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
package kubernetesmonitor
import (
"context"
"fmt"
"go.aporeto.io/trireme-lib/common"
"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 {
zap.L().Debug("unmanaged Kubernetes container on create or start", zap.String("puID", puID), zap.String("podNamespace", podNamespace), zap.String("podName", podName))
return nil
}
// 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))
return nil
}
}
if event == common.EventDestroy {
// Time to kill the cache entry
m.cache.deletePUIDEntry(puID)
}
// The event is then sent to the upstream policyResolver
return m.handlers.Policy.HandlePUEvent(ctx, puID, event, kubernetesRuntime)
}
// 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
}