forked from Azure/azure-container-networking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
178 lines (145 loc) · 4.67 KB
/
pod.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
// Copyright 2018 Microsoft. All rights reserved.
// MIT License
package npm
import (
"strings"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/npm/util"
corev1 "k8s.io/api/core/v1"
)
func isValidPod(podObj *corev1.Pod) bool {
return podObj.Status.Phase != "Failed" &&
podObj.Status.Phase != "Succeeded" &&
podObj.Status.Phase != "Unknown" &&
len(podObj.Status.PodIP) > 0
}
func isSystemPod(podObj *corev1.Pod) bool {
return podObj.ObjectMeta.Namespace == util.KubeSystemFlag
}
// AddPod handles adding pod ip to its label's ipset.
func (npMgr *NetworkPolicyManager) AddPod(podObj *corev1.Pod) error {
npMgr.Lock()
defer npMgr.Unlock()
if !isValidPod(podObj) {
return nil
}
var err error
defer func() {
if err = npMgr.UpdateAndSendReport(err, util.AddPodEvent); err != nil {
log.Printf("Error sending NPM telemetry report")
}
}()
podNs := podObj.ObjectMeta.Namespace
podName := podObj.ObjectMeta.Name
podNodeName := podObj.Spec.NodeName
podLabels := podObj.ObjectMeta.Labels
podIP := podObj.Status.PodIP
log.Printf("POD CREATING: %s/%s/%s%+v%s\n", podNs, podName, podNodeName, podLabels, podIP)
// Add the pod to ipset
ipsMgr := npMgr.nsMap[util.KubeAllNamespacesFlag].ipsMgr
// Add the pod to its namespace's ipset.
log.Printf("Adding pod %s to ipset %s\n", podIP, podNs)
if err = ipsMgr.AddToSet(podNs, podIP); err != nil {
log.Printf("Error adding pod to namespace ipset.\n")
return err
}
// Add the pod to its label's ipset.
var labelKeys []string
for podLabelKey, podLabelVal := range podLabels {
//Ignore pod-template-hash label.
if strings.Contains(podLabelKey, util.KubePodTemplateHashFlag) {
continue
}
labelKey := util.KubeAllNamespacesFlag + "-" + podLabelKey + ":" + podLabelVal
log.Printf("Adding pod %s to ipset %s\n", podIP, labelKey)
if err = ipsMgr.AddToSet(labelKey, podIP); err != nil {
log.Printf("Error adding pod to label ipset.\n")
return err
}
labelKeys = append(labelKeys, labelKey)
}
npMgr.clusterState.PodCount++
ns, err := newNs(podNs)
if err != nil {
log.Printf("Error creating namespace %s\n", podNs)
return err
}
npMgr.nsMap[podNs] = ns
return nil
}
// UpdatePod handles updating pod ip in its label's ipset.
func (npMgr *NetworkPolicyManager) UpdatePod(oldPodObj, newPodObj *corev1.Pod) error {
if !isValidPod(newPodObj) {
return nil
}
var err error
defer func() {
npMgr.Lock()
if err = npMgr.UpdateAndSendReport(err, util.UpdateNamespaceEvent); err != nil {
log.Printf("Error sending NPM telemetry report")
}
npMgr.Unlock()
}()
oldPodObjNs := oldPodObj.ObjectMeta.Namespace
oldPodObjName := oldPodObj.ObjectMeta.Name
oldPodObjPhase := oldPodObj.Status.Phase
oldPodObjIP := oldPodObj.Status.PodIP
newPodObjNs := newPodObj.ObjectMeta.Namespace
newPodObjName := newPodObj.ObjectMeta.Name
newPodObjPhase := newPodObj.Status.Phase
newPodObjIP := newPodObj.Status.PodIP
log.Printf(
"POD UPDATING. %s %s %s %s %s %s %s %s\n",
oldPodObjNs, oldPodObjName, oldPodObjPhase, oldPodObjIP, newPodObjNs, newPodObjName, newPodObjPhase, newPodObjIP,
)
if err = npMgr.DeletePod(oldPodObj); err != nil {
return err
}
if newPodObj.ObjectMeta.DeletionTimestamp == nil && newPodObj.ObjectMeta.DeletionGracePeriodSeconds == nil {
if err = npMgr.AddPod(newPodObj); err != nil {
return err
}
}
return nil
}
// DeletePod handles deleting pod from its label's ipset.
func (npMgr *NetworkPolicyManager) DeletePod(podObj *corev1.Pod) error {
npMgr.Lock()
defer npMgr.Unlock()
if !isValidPod(podObj) {
return nil
}
var err error
defer func() {
if err = npMgr.UpdateAndSendReport(err, util.DeletePodEvent); err != nil {
log.Printf("Error sending NPM telemetry report")
}
}()
podNs := podObj.ObjectMeta.Namespace
podName := podObj.ObjectMeta.Name
podNodeName := podObj.Spec.NodeName
podLabels := podObj.ObjectMeta.Labels
podIP := podObj.Status.PodIP
log.Printf("POD DELETING: %s/%s/%s\n", podNs, podName, podNodeName)
// Delete pod from ipset
ipsMgr := npMgr.nsMap[util.KubeAllNamespacesFlag].ipsMgr
// Delete the pod from its namespace's ipset.
if err = ipsMgr.DeleteFromSet(podNs, podIP); err != nil {
log.Printf("Error deleting pod from namespace ipset.\n")
return err
}
// Delete the pod from its label's ipset.
for podLabelKey, podLabelVal := range podLabels {
//Ignore pod-template-hash label.
if strings.Contains(podLabelKey, "pod-template-hash") {
continue
}
labelKey := util.KubeAllNamespacesFlag + "-" + podLabelKey + ":" + podLabelVal
if err = ipsMgr.DeleteFromSet(labelKey, podIP); err != nil {
log.Printf("Error deleting pod from label ipset.\n")
return err
}
}
npMgr.clusterState.PodCount--
return nil
}