-
Notifications
You must be signed in to change notification settings - Fork 36
/
group_assign.go
292 lines (256 loc) · 9.81 KB
/
group_assign.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2018 Cisco Systems, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hostagent
import (
"encoding/json"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
v1net "k8s.io/api/networking/v1"
"k8s.io/client-go/tools/cache"
"github.com/noironetworks/aci-containers/pkg/index"
"github.com/noironetworks/aci-containers/pkg/metadata"
"github.com/noironetworks/aci-containers/pkg/util"
)
func addGroup(gset map[metadata.OpflexGroup]bool, g []metadata.OpflexGroup,
tenant string, name string) []metadata.OpflexGroup {
newg := metadata.OpflexGroup{
PolicySpace: tenant,
Name: name,
}
if _, ok := gset[newg]; !ok {
gset[newg] = true
g = append(g, newg)
}
return g
}
func addPolicy(gset map[metadata.OpflexGroup]bool, g metadata.OpflexGroup,
tenant string, name string) metadata.OpflexGroup {
newg := metadata.OpflexGroup{
PolicySpace: tenant,
Name: name,
}
if _, ok := gset[newg]; !ok {
gset[newg] = true
g = newg
}
return g
}
func (agent *HostAgent) mergeQosPolicy(podkey string, qpval metadata.OpflexGroup) metadata.OpflexGroup {
gset := make(map[metadata.OpflexGroup]bool)
var g metadata.OpflexGroup
// Add qos policy that directly select this pod
for _, qpkey := range agent.qosPolPods.GetObjForPod(podkey) {
g = addPolicy(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "qp", qpkey))
}
// When the pod is not selected by any qos policy, return the
// existing value from the user annotation
if len(gset) == 0 {
return qpval
}
return g
}
func (agent *HostAgent) mergeNetPolSg(podkey string, pod *v1.Pod,
namespace *v1.Namespace, sgval []metadata.OpflexGroup) ([]metadata.OpflexGroup, error) {
gset := make(map[metadata.OpflexGroup]bool)
var g []metadata.OpflexGroup
ptypeset := make(map[v1net.PolicyType]bool)
if !agent.config.DisableHppRendering {
// Add network policies that directly select this pod
for _, npkey := range agent.netPolPods.GetObjForPod(podkey) {
obj, exists, err := agent.netPolInformer.GetIndexer().GetByKey(npkey)
if err != nil {
agent.log.Error("Could not lookup network policy ", npkey)
return g, err
}
if exists && obj != nil {
var labelKey string
if agent.config.HppOptimization {
np := obj.(*v1net.NetworkPolicy)
hash, err := util.CreateHashFromNetPol(np)
if err != nil {
agent.log.Error("Failed to create hash for network policy ", npkey)
return g, err
}
labelKey = util.AciNameForKey(agent.config.AciPrefix, "np", hash)
} else {
labelKey = util.AciNameForKey(agent.config.AciPrefix, "np", npkey)
}
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace, labelKey)
for _, t := range util.GetNetPolPolicyTypes(agent.netPolInformer.GetIndexer(), npkey) {
ptypeset[t] = true
}
}
}
}
// When the pod is not selected by any network policy, don't apply
// any extra security groups and return the existing value from
// the user annotation
if len(gset) == 0 {
return sgval, nil
}
// Add security groups from the user annotation
for _, og := range sgval {
gset[og] = true
g = append(g, og)
}
// Add network policy for accessing the pod's local node
if pod.Spec.NodeName != "" {
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "node", pod.Spec.NodeName))
}
// Add static-discovery network policy to allow ICMP/ARP
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "np", "static-discovery"))
if !ptypeset[v1net.PolicyTypeIngress] {
// Add static-ingress since no policy applies to ingress
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "np", "static-ingress"))
}
if !ptypeset[v1net.PolicyTypeEgress] {
// Add static-egress since no policy applies to egress
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "np", "static-egress"))
}
if len(g) == 0 {
return sgval, nil
}
return g, nil
}
func decodeAnnotation(annStr string, into interface{}, logger *logrus.Entry, comment string) {
if annStr != "" {
logger.Debugf("decodeAnnotation: %s -- [%s]", annStr, comment)
err := json.Unmarshal([]byte(annStr), &into)
if err != nil {
logger.WithFields(logrus.Fields{
"Annotation": annStr,
}).Errorf("%s - could not decode annotation: %v", comment, err)
}
}
}
// Gets eg, sg and qos policy annotations on associated deployment or rc
func (agent *HostAgent) getParentAnn(podKey string) (string, string, string, bool) {
set := []struct {
indexer *index.PodSelectorIndex
informer cache.SharedIndexInformer
}{
{agent.depPods, agent.depInformer},
{agent.rcPods, agent.rcInformer},
}
for _, parent := range set {
for _, pkey := range parent.indexer.GetObjForPod(podKey) {
agent.log.Debugf("deployment/rc found for pod: %s", pkey)
obj, exists, err :=
parent.informer.GetIndexer().GetByKey(pkey)
if err != nil {
agent.log.Error("Could not lookup parent " +
pkey + ": " + err.Error())
continue
}
if exists && obj != nil {
deployment, ok := obj.(*appsv1.Deployment)
if ok {
return deployment.ObjectMeta.Annotations[metadata.EgAnnotation],
deployment.ObjectMeta.Annotations[metadata.SgAnnotation],
deployment.ObjectMeta.Annotations[metadata.QpAnnotation], true
}
rc, ok := obj.(*v1.ReplicationController)
if ok {
return rc.ObjectMeta.Annotations[metadata.EgAnnotation],
rc.ObjectMeta.Annotations[metadata.SgAnnotation],
rc.ObjectMeta.Annotations[metadata.QpAnnotation], true
}
}
}
}
return "", "", "", false
}
// assignGroups assigns epg, security groups and qos policy based on annotations on the
// namespace, deployment and pod.
func (agent *HostAgent) assignGroups(pod *v1.Pod) (metadata.OpflexGroup, []metadata.OpflexGroup, metadata.OpflexGroup, error) {
var egval metadata.OpflexGroup
var sgval []metadata.OpflexGroup
var qpval metadata.OpflexGroup
logger := podLogger(agent.log, pod)
podkey, err := cache.MetaNamespaceKeyFunc(pod)
if err != nil {
logger.Error("Could not create pod key: ", err)
return egval, sgval, qpval, err
}
namespaceobj, exists, err :=
agent.nsInformer.GetIndexer().GetByKey(pod.ObjectMeta.Namespace)
if err != nil {
agent.log.Error("Could not lookup namespace " +
pod.ObjectMeta.Namespace + ": " + err.Error())
return egval, sgval, qpval, err
}
// top-level default annotation
egval = agent.config.DefaultEg
sgval = agent.config.DefaultSg
qpval = agent.config.DefaultQp
// configured namespace override has next-highest priority
if nseg, ok := agent.config.NamespaceDefaultEg[pod.Namespace]; ok {
egval = nseg
}
if nssgs, ok := agent.config.NamespaceDefaultSg[pod.Namespace]; ok {
sgval = nssgs
}
if nsqp, ok := agent.config.NamespaceDefaultQp[pod.Namespace]; ok {
qpval = nsqp
}
// namespace annotation has next-highest priority
var namespace *v1.Namespace
if exists && namespaceobj != nil {
namespace = namespaceobj.(*v1.Namespace)
var temp metadata.OpflexGroup
decodeAnnotation(namespace.ObjectMeta.Annotations[metadata.EgAnnotation], &temp, logger, "namespace[EpgAnnotation]")
// For OpenShift installations <= 3.11, we want to offset
// the namespace annotation applied in this PR:
// openshift/openshift-ansible@0ad7479. The pods
// belonging to the four namespaces (i.e.
// kube-service-catalog,kube-service-catalog,
// openshift-console, openshift-monitoring and
// openshift-web-console) need to go into the system
// EPG, which we do in acc-provision:
// noironetworks/acc-provision@f93a132
if agent.config.AciVmmDomainType == "OpenShift" && temp.Name == "kubernetes|kube-system" &&
(namespace.ObjectMeta.Name == "kube-service-catalog" || namespace.ObjectMeta.Name == "openshift-console" ||
namespace.ObjectMeta.Name == "openshift-monitoring" || namespace.ObjectMeta.Name == "openshift-web-console") {
agent.log.Debug("Ignoring annotation set on ", namespace.ObjectMeta.Name)
} else {
decodeAnnotation(namespace.ObjectMeta.Annotations[metadata.EgAnnotation], &egval, logger, "namespace[EpgAnnotation]")
}
decodeAnnotation(namespace.ObjectMeta.Annotations[metadata.SgAnnotation], &sgval, logger, "namespace[SgAnnotation]")
decodeAnnotation(namespace.ObjectMeta.Annotations[metadata.QpAnnotation], &qpval, logger, "namespace[QpAnnotation]")
}
// annotation on parent deployment or rc is next-highest priority
egAnn, sgAnn, qpAnn, found := agent.getParentAnn(podkey)
if found {
decodeAnnotation(egAnn, &egval, logger, "deployment/rc[EpgAnnotation]")
decodeAnnotation(sgAnn, &sgval, logger, "deployment/rc[SgAnnotation]")
decodeAnnotation(qpAnn, &qpval, logger, "deployment/rc[QpAnnotation]")
}
// direct pod annotation is highest priority
decodeAnnotation(pod.ObjectMeta.Annotations[metadata.EgAnnotation], &egval, logger, "pod[EpgAnnotation]")
decodeAnnotation(pod.ObjectMeta.Annotations[metadata.QpAnnotation], &qpval, logger, "pod[QpAnnotation]")
decodeAnnotation(pod.ObjectMeta.Annotations[metadata.SgAnnotation], &sgval, logger, "pod[SgAnnotation]")
sgval, err = agent.mergeNetPolSg(podkey, pod, namespace, sgval)
if err != nil {
logger.Error("Could not generate network policy ",
"security groups:", err)
}
qpval = agent.mergeQosPolicy(podkey, qpval)
return egval, sgval, qpval, nil
}