-
Notifications
You must be signed in to change notification settings - Fork 36
/
snats.go
1232 lines (1173 loc) · 37.5 KB
/
snats.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 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 WARRATIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Handlers for snat updates.
package hostagent
import (
"bytes"
"context"
"encoding/json"
"fmt"
snatglobal "github.com/noironetworks/aci-containers/pkg/snatglobalinfo/apis/aci.snat/v1"
snatglobalclset "github.com/noironetworks/aci-containers/pkg/snatglobalinfo/clientset/versioned"
snatpolicy "github.com/noironetworks/aci-containers/pkg/snatpolicy/apis/aci.snat/v1"
snatpolicyclset "github.com/noironetworks/aci-containers/pkg/snatpolicy/clientset/versioned"
"github.com/noironetworks/aci-containers/pkg/util"
"github.com/sirupsen/logrus"
"io/ioutil"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/controller"
"net"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
)
// Filename used to create external service file on host
// example snat-external.service
const SnatService = "snat-external"
type ResourceType int
const (
POD ResourceType = 1 << iota
SERVICE
DEPLOYMENT
NAMESPACE
CLUSTER
INVALID
)
type OpflexPortRange struct {
Start int `json:"start,omitempty"`
End int `json:"end,omitempty"`
}
// This structure is to write the SnatFile
type OpflexSnatIp struct {
Uuid string `json:"uuid"`
InterfaceName string `json:"interface-name,omitempty"`
SnatIp string `json:"snat-ip,omitempty"`
InterfaceMac string `json:"interface-mac,omitempty"`
Local bool `json:"local,omitempty"`
DestIpAddress []string `json:"dest,omitempty"`
PortRange []OpflexPortRange `json:"port-range,omitempty"`
InterfaceVlan uint `json:"interface-vlan,omitempty"`
Zone uint `json:"zone,omitempty"`
Remote []OpflexSnatIpRemoteInfo `json:"remote,omitempty"`
}
// This Structure is to calculate remote Info
type OpflexSnatIpRemoteInfo struct {
NodeIp string `json:"snat_ip,omitempty"`
MacAddress string `json:"mac,omitempty"`
PortRange []OpflexPortRange `json:"port-range,omitempty"`
Refcount int `json:"ref,omitempty"`
}
type opflexSnatGlobalInfo struct {
SnatIp string
MacAddress string
PortRange []OpflexPortRange
SnatIpUid string
SnatPolicyName string
}
type opflexSnatLocalInfo struct {
Snatpolicies map[ResourceType][]string //Each resource can represent multiple entries
PlcyUuids []string //sorted policy uuids
}
func (agent *HostAgent) initSnatGlobalInformerFromClient(
snatClient *snatglobalclset.Clientset) {
agent.initSnatGlobalInformerBase(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return snatClient.AciV1().SnatGlobalInfos(metav1.NamespaceAll).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return snatClient.AciV1().SnatGlobalInfos(metav1.NamespaceAll).Watch(context.TODO(), options)
},
})
}
func (agent *HostAgent) initSnatPolicyInformerFromClient(
snatClient *snatpolicyclset.Clientset) {
agent.initSnatPolicyInformerBase(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return snatClient.AciV1().SnatPolicies().List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return snatClient.AciV1().SnatPolicies().Watch(context.TODO(), options)
},
})
}
func writeSnat(snatfile string, snat *OpflexSnatIp) (bool, error) {
newdata, err := json.MarshalIndent(snat, "", " ")
if err != nil {
return true, err
}
existingdata, err := ioutil.ReadFile(snatfile)
if err == nil && reflect.DeepEqual(existingdata, newdata) {
return false, nil
}
err = ioutil.WriteFile(snatfile, newdata, 0644)
return true, err
}
func (agent *HostAgent) FormSnatFilePath(uuid string) string {
return filepath.Join(agent.config.OpFlexSnatDir, uuid+".snat")
}
func SnatGlobalInfoLogger(log *logrus.Logger, snat *snatglobal.SnatGlobalInfo) *logrus.Entry {
return log.WithFields(logrus.Fields{
"namespace": snat.ObjectMeta.Namespace,
"name": snat.ObjectMeta.Name,
"spec": snat.Spec,
})
}
func opflexSnatIpLogger(log *logrus.Logger, snatip *OpflexSnatIp) *logrus.Entry {
return log.WithFields(logrus.Fields{
"uuid": snatip.Uuid,
"snat_ip": snatip.SnatIp,
"mac_address": snatip.InterfaceMac,
"port_range": snatip.PortRange,
"local": snatip.Local,
"interface-name": snatip.InterfaceName,
"interfcae-vlan": snatip.InterfaceVlan,
"remote": snatip.Remote,
})
}
func (agent *HostAgent) initSnatGlobalInformerBase(listWatch *cache.ListWatch) {
agent.snatGlobalInformer = cache.NewSharedIndexInformer(
listWatch,
&snatglobal.SnatGlobalInfo{},
controller.NoResyncPeriodFunc(),
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
agent.snatGlobalInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
agent.snatGlobalInfoUpdate(obj)
},
UpdateFunc: func(_ interface{}, obj interface{}) {
agent.snatGlobalInfoUpdate(obj)
},
DeleteFunc: func(obj interface{}) {
agent.snatGlobalInfoDelete(obj)
},
})
agent.log.Info("Initializing SnatGlobal Info Informers")
}
func (agent *HostAgent) initSnatPolicyInformerBase(listWatch *cache.ListWatch) {
agent.snatPolicyInformer = cache.NewSharedIndexInformer(
listWatch,
&snatpolicy.SnatPolicy{}, controller.NoResyncPeriodFunc(),
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
)
agent.snatPolicyInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
agent.snatPolicyAdded(obj)
},
UpdateFunc: func(oldobj interface{}, newobj interface{}) {
agent.snatPolicyUpdated(oldobj, newobj)
},
DeleteFunc: func(obj interface{}) {
agent.snatPolicyDeleted(obj)
},
})
agent.log.Infof("Initializing Snat Policy Informers")
}
func (agent *HostAgent) snatPolicyAdded(obj interface{}) {
agent.indexMutex.Lock()
defer agent.indexMutex.Unlock()
policyinfo := obj.(*snatpolicy.SnatPolicy)
agent.log.Info("Policy Info Added: ", policyinfo.ObjectMeta.Name)
if policyinfo.Status.State != snatpolicy.Ready {
return
}
agent.snatPolicyCache[policyinfo.ObjectMeta.Name] = policyinfo
setDestIp(agent.snatPolicyCache[policyinfo.ObjectMeta.Name].Spec.DestIp)
agent.handleSnatUpdate(policyinfo)
}
func (agent *HostAgent) snatPolicyUpdated(oldobj interface{}, newobj interface{}) {
agent.indexMutex.Lock()
defer agent.indexMutex.Unlock()
oldpolicyinfo := oldobj.(*snatpolicy.SnatPolicy)
newpolicyinfo := newobj.(*snatpolicy.SnatPolicy)
agent.log.Info("Policy Info Updated: ", newpolicyinfo.ObjectMeta.Name)
agent.log.Info("Policy Status: ", newpolicyinfo.Status.State)
if reflect.DeepEqual(oldpolicyinfo, newpolicyinfo) {
return
}
//1. check if the local nodename is present in globalinfo
// 2. if it is not present then delete the policy from localInfo as the portinfo is not allocated for node
if newpolicyinfo.Status.State == snatpolicy.IpPortsExhausted {
agent.log.Info("Ports exhausted: ", newpolicyinfo.ObjectMeta.Name)
ginfo, ok := agent.opflexSnatGlobalInfos[agent.config.NodeName]
present := false
if ok {
for _, v := range ginfo {
if v.SnatPolicyName == newpolicyinfo.ObjectMeta.Name {
present = true
}
}
}
if !ok || !present {
agent.deletePolicy(newpolicyinfo)
}
return
}
if newpolicyinfo.Status.State != snatpolicy.Ready {
return
}
agent.snatPolicyCache[newpolicyinfo.ObjectMeta.Name] = newpolicyinfo
setDestIp(agent.snatPolicyCache[newpolicyinfo.ObjectMeta.Name].Spec.DestIp)
// After Validation of SnatPolicy State will be set to Ready
if newpolicyinfo.Status.State != oldpolicyinfo.Status.State {
agent.handleSnatUpdate(newpolicyinfo)
return
}
update := true
if !reflect.DeepEqual(oldpolicyinfo.Spec.Selector,
newpolicyinfo.Spec.Selector) {
var poduids []string
// delete all the pods matching the policy
for uuid, res := range agent.snatPods[newpolicyinfo.ObjectMeta.Name] {
agent.deleteSnatLocalInfo(uuid, res, newpolicyinfo.ObjectMeta.Name)
poduids = append(poduids, uuid)
}
agent.updateEpFiles(poduids)
agent.handleSnatUpdate(newpolicyinfo)
var matchingpods []string
for uuid := range agent.snatPods[newpolicyinfo.ObjectMeta.Name] {
matchingpods = append(matchingpods, uuid)
}
// Nodeinfo trigger handles if handlesnatUpdate don't match any pods
// this trigger clears the globalinfo allocated for node
if len(poduids) > 0 && len(matchingpods) == 0 {
agent.scheduleSyncNodeInfo()
} else {
// Epfiles needs to be updated if there are any pods matching with newlabel
agent.updateEpFiles(matchingpods)
}
update = false
}
// destination update can be ignored if labels also changed
if !reflect.DeepEqual(oldpolicyinfo.Spec.DestIp,
newpolicyinfo.Spec.DestIp) && update {
// updateEpFile
// SyncSnatFile
var poduids []string
for uid := range agent.snatPods[newpolicyinfo.ObjectMeta.Name] {
poduids = append(poduids, uid)
}
// This EPfiles update based on destip chanes policy order is maintained
agent.updateEpFiles(poduids)
agent.scheduleSyncSnats()
}
}
func (agent *HostAgent) snatPolicyDeleted(obj interface{}) {
agent.indexMutex.Lock()
defer agent.indexMutex.Unlock()
policyinfo := obj.(*snatpolicy.SnatPolicy)
agent.deletePolicy(policyinfo)
delete(agent.snatPolicyCache, policyinfo.ObjectMeta.Name)
}
func (agent *HostAgent) handleSnatUpdate(policy *snatpolicy.SnatPolicy) {
// First Parse the policy and check for applicability
// list all the Pods based on labels and namespace
agent.log.Debug("Handle snatUpdate: ", policy)
_, err := cache.MetaNamespaceKeyFunc(policy)
if err != nil {
return
}
// 1.List the targets matching the policy based on policy config
uids := make(map[ResourceType][]string)
switch {
case len(policy.Spec.SnatIp) == 0:
//handle policy for service pods
var services []*v1.Service
var poduids []string
selector := labels.SelectorFromSet(labels.Set(policy.Spec.Selector.Labels))
cache.ListAll(agent.serviceInformer.GetIndexer(), selector,
func(servobj interface{}) {
services = append(services, servobj.(*v1.Service))
})
// list the pods and apply the policy at service target
for _, service := range services {
uids, _ := agent.getPodsMatchingObjet(service, policy.ObjectMeta.Name)
poduids = append(poduids, uids...)
key, err := cache.MetaNamespaceKeyFunc(service)
if err == nil {
_, ok := agent.ReadSnatPolicyLabel(key)
if ok && len(policy.Spec.Selector.Labels) > 0 {
agent.WriteSnatPolicyLabel(key, policy.ObjectMeta.Name, SERVICE)
}
}
}
uids[SERVICE] = poduids
case reflect.DeepEqual(policy.Spec.Selector, snatpolicy.PodSelector{}):
// This Policy will be applied at cluster level
var poduids []string
// handle policy for cluster
for k := range agent.opflexEps {
poduids = append(poduids, k)
}
uids[CLUSTER] = poduids
case len(policy.Spec.Selector.Labels) == 0:
// This is namespace based policy
var poduids []string
cache.ListAllByNamespace(agent.podInformer.GetIndexer(), policy.Spec.Selector.Namespace, labels.Everything(),
func(podobj interface{}) {
pod := podobj.(*v1.Pod)
if pod.Spec.NodeName == agent.config.NodeName {
poduids = append(poduids, string(pod.ObjectMeta.UID))
}
})
uids[NAMESPACE] = poduids
default:
poduids, deppoduids, nspoduids :=
agent.getPodUidsMatchingLabel(policy.Spec.Selector.Namespace, policy.Spec.Selector.Labels, policy.ObjectMeta.Name)
uids[POD] = poduids
uids[DEPLOYMENT] = deppoduids
uids[NAMESPACE] = nspoduids
}
for res, poduids := range uids {
agent.applyPolicy(poduids, res, policy.GetName())
}
}
func (agent *HostAgent) updateSnatPolicyLabels(obj interface{}, policyname string) (poduids []string) {
uids, res := agent.getPodsMatchingObjet(obj, policyname)
if len(uids) > 0 {
key, _ := cache.MetaNamespaceKeyFunc(obj)
if _, ok := agent.ReadSnatPolicyLabel(key); ok {
agent.WriteSnatPolicyLabel(key, policyname, res)
}
}
return uids
}
// Get all the pods matching the Policy Selector
func (agent *HostAgent) getPodUidsMatchingLabel(namespace string, label map[string]string, policyname string) (poduids []string,
deppoduids []string, nspoduids []string) {
selector := labels.SelectorFromSet(labels.Set(label))
cache.ListAll(agent.podInformer.GetIndexer(), selector,
func(podobj interface{}) {
pod := podobj.(*v1.Pod)
if pod.Spec.NodeName == agent.config.NodeName {
poduids = append(poduids, agent.updateSnatPolicyLabels(podobj, policyname)...)
}
})
cache.ListAll(agent.depInformer.GetIndexer(), selector,
func(depobj interface{}) {
deppoduids = append(deppoduids, agent.updateSnatPolicyLabels(depobj, policyname)...)
})
cache.ListAll(agent.nsInformer.GetIndexer(), selector,
func(nsobj interface{}) {
nspoduids = append(nspoduids, agent.updateSnatPolicyLabels(nsobj, policyname)...)
})
return
}
// Apply the Policy at Resource level
func (agent *HostAgent) applyPolicy(poduids []string, res ResourceType, snatPolicyName string) {
nodeUpdate := false
if len(poduids) == 0 {
return
}
if _, ok := agent.snatPods[snatPolicyName]; !ok {
agent.snatPods[snatPolicyName] = make(map[string]ResourceType)
nodeUpdate = true
}
for _, uid := range poduids {
_, ok := agent.opflexSnatLocalInfos[uid]
if !ok {
var localinfo opflexSnatLocalInfo
localinfo.Snatpolicies = make(map[ResourceType][]string)
localinfo.Snatpolicies[res] = append(localinfo.Snatpolicies[res], snatPolicyName)
agent.opflexSnatLocalInfos[uid] = &localinfo
agent.snatPods[snatPolicyName][uid] |= res
agent.log.Debug("Apply policy res: ", agent.snatPods[snatPolicyName][uid])
} else {
present := false
for _, name := range agent.opflexSnatLocalInfos[uid].Snatpolicies[res] {
if name == snatPolicyName {
present = true
}
}
if present == false {
agent.opflexSnatLocalInfos[uid].Snatpolicies[res] =
append(agent.opflexSnatLocalInfos[uid].Snatpolicies[res], snatPolicyName)
agent.snatPods[snatPolicyName][uid] |= res
agent.log.Debug("Apply policy res: ", agent.snatPods[snatPolicyName][uid])
}
}
}
if nodeUpdate == true {
agent.log.Debug("Schedule the node Sync")
agent.scheduleSyncNodeInfo()
} else {
// trigger update the epfile
agent.updateEpFiles(poduids)
}
return
}
// Sync the NodeInfo
func (agent *HostAgent) syncSnatNodeInfo() bool {
if !agent.syncEnabled {
return false
}
snatPolicyNames := make(map[string]bool)
agent.indexMutex.Lock()
for key, val := range agent.snatPods {
if len(val) > 0 {
snatPolicyNames[key] = true
}
}
agent.indexMutex.Unlock()
env := agent.env.(*K8sEnvironment)
if env == nil {
return false
}
// send nodeupdate for the policy names
if agent.InformNodeInfo(env.nodeInfo, snatPolicyNames) == false {
agent.log.Debug("Failed to update retry: ", snatPolicyNames)
return true
}
agent.log.Debug("Updated Node Info: ", snatPolicyNames)
return false
}
func (agent *HostAgent) deletePolicy(policy *snatpolicy.SnatPolicy) {
pods, ok := agent.snatPods[policy.GetName()]
var poduids []string
if !ok {
return
}
for uuid, res := range pods {
agent.deleteSnatLocalInfo(uuid, res, policy.GetName())
poduids = append(poduids, uuid)
}
agent.updateEpFiles(poduids)
delete(agent.snatPods, policy.GetName())
agent.log.Info("SnatPolicy deleted update Nodeinfo: ", policy.GetName())
agent.scheduleSyncNodeInfo()
agent.DeleteMatchingSnatPolicyLabel(policy.GetName())
return
}
func (agent *HostAgent) deleteSnatLocalInfo(poduid string, res ResourceType, plcyname string) {
localinfo, ok := agent.opflexSnatLocalInfos[poduid]
if ok {
i := uint(0)
j := uint(0)
// loop through all the resources matching the policy
for i < uint(res) {
i = 1 << j
j = j + 1
if i&uint(res) == i {
length := len(localinfo.Snatpolicies[ResourceType(i)])
deletedcount := 0
for k := 0; k < length; k++ {
l := k - deletedcount
// delete the matching policy from policy stack
if plcyname == localinfo.Snatpolicies[ResourceType(i)][l] {
agent.log.Info("Delete the Policy name: ", plcyname)
localinfo.Snatpolicies[ResourceType(i)] =
append(localinfo.Snatpolicies[ResourceType(i)][:l],
localinfo.Snatpolicies[ResourceType(i)][l+1:]...)
deletedcount++
}
}
agent.log.Debug("Opflex agent and localinfo: ", agent.opflexSnatLocalInfos[poduid], localinfo)
if len(localinfo.Snatpolicies[res]) == 0 {
delete(localinfo.Snatpolicies, res)
}
if v, ok := agent.snatPods[plcyname]; ok {
if _, ok := v[poduid]; ok {
agent.snatPods[plcyname][poduid] &= ^(res) // clear the bit
agent.log.Debug("Res: ", agent.snatPods[plcyname][poduid])
if agent.snatPods[plcyname][poduid] == 0 { // delete the pod if no resource is pointing for the policy
delete(agent.snatPods[plcyname], poduid)
if len(agent.snatPods[plcyname]) == 0 {
delete(agent.snatPods, plcyname)
}
}
}
}
}
}
}
}
func (agent *HostAgent) snatGlobalInfoUpdate(obj interface{}) {
agent.indexMutex.Lock()
defer agent.indexMutex.Unlock()
snat := obj.(*snatglobal.SnatGlobalInfo)
key, err := cache.MetaNamespaceKeyFunc(snat)
if err != nil {
SnatGlobalInfoLogger(agent.log, snat).
Error("Could not create key:" + err.Error())
return
}
agent.log.Info("Snat Global Object added/Updated: ", snat)
agent.doUpdateSnatGlobalInfo(key)
}
func (agent *HostAgent) doUpdateSnatGlobalInfo(key string) {
snatobj, exists, err :=
agent.snatGlobalInformer.GetStore().GetByKey(key)
if err != nil {
agent.log.Error("Could not lookup snat for " +
key + ": " + err.Error())
return
}
if !exists || snatobj == nil {
return
}
snat := snatobj.(*snatglobal.SnatGlobalInfo)
logger := SnatGlobalInfoLogger(agent.log, snat)
agent.snaGlobalInfoChanged(snatobj, logger)
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func (agent *HostAgent) snaGlobalInfoChanged(snatobj interface{}, logger *logrus.Entry) {
snat := snatobj.(*snatglobal.SnatGlobalInfo)
syncSnat := false
updateLocalInfo := false
if logger == nil {
logger = agent.log.WithFields(logrus.Fields{})
}
logger.Debug("Snat Global info Changed...")
globalInfo := snat.Spec.GlobalInfos
// This case is possible when all the pods will be deleted from that node
if len(globalInfo) < len(agent.opflexSnatGlobalInfos) {
for nodename := range agent.opflexSnatGlobalInfos {
if _, ok := globalInfo[nodename]; !ok {
delete(agent.opflexSnatGlobalInfos, nodename)
syncSnat = true
}
}
}
for nodename, val := range globalInfo {
var newglobalinfos []*opflexSnatGlobalInfo
for _, v := range val {
portrange := make([]OpflexPortRange, 1)
portrange[0].Start = v.PortRanges[0].Start
portrange[0].End = v.PortRanges[0].End
nodeInfo := &opflexSnatGlobalInfo{
SnatIp: v.SnatIp,
MacAddress: v.MacAddress,
PortRange: portrange,
SnatIpUid: v.SnatIpUid,
SnatPolicyName: v.SnatPolicyName,
}
newglobalinfos = append(newglobalinfos, nodeInfo)
}
existing, ok := agent.opflexSnatGlobalInfos[nodename]
if (ok && !reflect.DeepEqual(existing, newglobalinfos)) || !ok {
agent.opflexSnatGlobalInfos[nodename] = newglobalinfos
if nodename == agent.config.NodeName {
updateLocalInfo = true
}
syncSnat = true
}
}
snatFileName := SnatService + ".service"
filePath := filepath.Join(agent.config.OpFlexServiceDir, snatFileName)
file_exists := fileExists(filePath)
if len(agent.opflexSnatGlobalInfos) > 0 {
// if more than one global infos, create snat ext file
as := &opflexService{
Uuid: SnatService,
DomainPolicySpace: agent.config.AciVrfTenant,
DomainName: agent.config.AciVrf,
ServiceMode: "loadbalancer",
ServiceMappings: make([]opflexServiceMapping, 0),
InterfaceName: agent.config.UplinkIface,
InterfaceVlan: uint16(agent.config.ServiceVlan),
ServiceMac: agent.config.UplinkMacAdress,
InterfaceIp: agent.serviceEp.Ipv4.String(),
}
sm := &opflexServiceMapping{
Conntrack: true,
}
as.ServiceMappings = append(as.ServiceMappings, *sm)
agent.opflexServices[SnatService] = as
if !file_exists {
wrote, err := writeAs(filePath, as)
if err != nil {
agent.log.Debug("Unable to write snat ext service file:")
} else if wrote {
agent.log.Debug("Created snat ext service file")
}
}
} else {
delete(agent.opflexServices, SnatService)
// delete snat service file if no global infos exist
if file_exists {
err := os.Remove(filePath)
if err != nil {
agent.log.Debug("Unable to delete snat ext service file")
} else {
agent.log.Debug("Deleted snat ext service file")
}
}
}
if syncSnat {
agent.scheduleSyncSnats()
}
if updateLocalInfo {
var poduids []string
for _, v := range agent.opflexSnatGlobalInfos[agent.config.NodeName] {
for uuid := range agent.snatPods[v.SnatPolicyName] {
poduids = append(poduids, uuid)
}
}
agent.log.Info("Updating EpFile GlobalInfo Context: ", poduids)
agent.updateEpFiles(poduids)
}
}
func (agent *HostAgent) snatGlobalInfoDelete(obj interface{}) {
agent.log.Debug("Snat Global Info Obj Delete")
snat := obj.(*snatglobal.SnatGlobalInfo)
globalInfo := snat.Spec.GlobalInfos
agent.indexMutex.Lock()
for nodename := range globalInfo {
if _, ok := agent.opflexSnatGlobalInfos[nodename]; ok {
delete(agent.opflexSnatGlobalInfos, nodename)
}
}
agent.indexMutex.Unlock()
}
func (agent *HostAgent) syncSnat() bool {
if !agent.syncEnabled {
return false
}
agent.log.Debug("Syncing snats")
agent.indexMutex.Lock()
opflexSnatIps := make(map[string]*OpflexSnatIp)
remoteinfo := make(map[string][]OpflexSnatIpRemoteInfo)
// set the remote info for every snatIp
for nodename, v := range agent.opflexSnatGlobalInfos {
for _, ginfo := range v {
if nodename != agent.config.NodeName {
var remote OpflexSnatIpRemoteInfo
remote.MacAddress = ginfo.MacAddress
remote.PortRange = ginfo.PortRange
remoteinfo[ginfo.SnatIp] = append(remoteinfo[ginfo.SnatIp], remote)
}
}
}
agent.log.Debug("RemoteInfo: ", remoteinfo)
// set the Opflex Snat IP information
localportrange := make(map[string][]OpflexPortRange)
ginfos, ok := agent.opflexSnatGlobalInfos[agent.config.NodeName]
if ok {
for _, ginfo := range ginfos {
localportrange[ginfo.SnatIp] = ginfo.PortRange
}
}
for _, v := range agent.opflexSnatGlobalInfos {
for _, ginfo := range v {
var snatinfo OpflexSnatIp
// set the local portrange
snatinfo.InterfaceName = agent.config.UplinkIface
snatinfo.InterfaceVlan = agent.config.ServiceVlan
snatinfo.InterfaceMac = agent.config.UplinkMacAdress
snatinfo.Local = false
if _, ok := localportrange[ginfo.SnatIp]; ok {
snatinfo.PortRange = localportrange[ginfo.SnatIp]
// need to sort the order
if _, ok := agent.snatPolicyCache[ginfo.SnatPolicyName]; ok {
if len(agent.snatPolicyCache[ginfo.SnatPolicyName].Spec.DestIp) == 0 {
snatinfo.DestIpAddress = []string{"0.0.0.0/0"}
} else {
snatinfo.DestIpAddress =
agent.snatPolicyCache[ginfo.SnatPolicyName].Spec.DestIp
}
}
snatinfo.Local = true
}
snatinfo.SnatIp = ginfo.SnatIp
snatinfo.Uuid = ginfo.SnatIpUid
snatinfo.Zone = agent.config.Zone
snatinfo.Remote = remoteinfo[ginfo.SnatIp]
opflexSnatIps[ginfo.SnatIpUid] = &snatinfo
agent.log.Debug("Opflex Snat data IP: ", opflexSnatIps[ginfo.SnatIpUid])
}
}
agent.indexMutex.Unlock()
files, err := ioutil.ReadDir(agent.config.OpFlexSnatDir)
if err != nil {
agent.log.WithFields(
logrus.Fields{"SnatDir: ": agent.config.OpFlexSnatDir},
).Error("Could not read directory " + err.Error())
return true
}
seen := make(map[string]bool)
for _, f := range files {
uuid := f.Name()
if strings.HasSuffix(uuid, ".snat") {
uuid = uuid[:len(uuid)-5]
} else {
continue
}
snatfile := filepath.Join(agent.config.OpFlexSnatDir, f.Name())
logger := agent.log.WithFields(
logrus.Fields{"Uuid": uuid})
existing, ok := opflexSnatIps[uuid]
if ok {
fmt.Printf("snatfile:%s\n", snatfile)
wrote, err := writeSnat(snatfile, existing)
if err != nil {
opflexSnatIpLogger(agent.log, existing).Error("Error writing snat file: ", err)
} else if wrote {
opflexSnatIpLogger(agent.log, existing).Info("Updated snat")
}
seen[uuid] = true
} else {
logger.Info("Removing snat")
os.Remove(snatfile)
}
}
for _, snat := range opflexSnatIps {
if seen[snat.Uuid] {
continue
}
opflexSnatIpLogger(agent.log, snat).Info("Adding Snat")
snatfile :=
agent.FormSnatFilePath(snat.Uuid)
_, err = writeSnat(snatfile, snat)
if err != nil {
opflexSnatIpLogger(agent.log, snat).
Error("Error writing snat file: ", err)
}
}
agent.log.Debug("Finished snat sync")
return false
}
// Get the Pods matching the Object selector
func (agent *HostAgent) getPodsMatchingObjet(obj interface{}, policyname string) (poduids []string, res ResourceType) {
metadata, err := meta.Accessor(obj)
if err != nil {
return
}
if agent.isPolicyNameSpaceMatches(policyname, metadata.GetNamespace()) == false {
return
}
switch obj.(type) {
case *v1.Pod:
pod, _ := obj.(*v1.Pod)
poduids = append(poduids, string(pod.ObjectMeta.UID))
agent.log.Info("Matching pod uids: ", poduids)
case *appsv1.Deployment:
deployment, _ := obj.(*appsv1.Deployment)
depkey, _ :=
cache.MetaNamespaceKeyFunc(deployment)
for _, podkey := range agent.depPods.GetPodForObj(depkey) {
podobj, exists, err := agent.podInformer.GetStore().GetByKey(podkey)
if err != nil {
agent.log.Error("Could not lookup pod: ", err)
continue
}
if !exists || podobj == nil {
agent.log.Error("Object doesn't exist yet ", podkey)
continue
}
poduids = append(poduids, string(podobj.(*v1.Pod).ObjectMeta.UID))
}
agent.log.Info("Matching deployment pod uids: ", poduids)
res = DEPLOYMENT
case *v1.Service:
service, _ := obj.(*v1.Service)
selector := labels.SelectorFromSet(labels.Set(service.Spec.Selector))
cache.ListAllByNamespace(agent.podInformer.GetIndexer(),
service.ObjectMeta.Namespace, selector,
func(podobj interface{}) {
pod := podobj.(*v1.Pod)
if pod.Spec.NodeName == agent.config.NodeName {
poduids = append(poduids, string(pod.ObjectMeta.UID))
}
})
agent.log.Info("Matcing service pod uids: ", poduids)
res = SERVICE
case *v1.Namespace:
ns, _ := obj.(*v1.Namespace)
cache.ListAllByNamespace(agent.podInformer.GetIndexer(),
ns.ObjectMeta.Name, labels.Everything(),
func(podobj interface{}) {
pod := podobj.(*v1.Pod)
if pod.Spec.NodeName == agent.config.NodeName {
poduids = append(poduids, string(pod.ObjectMeta.UID))
}
})
agent.log.Info("Matching namespace pod uids: ", poduids)
res = NAMESPACE
default:
}
return
}
// Updates the EPFile with Snatuuid's
func (agent *HostAgent) updateEpFiles(poduids []string) {
syncEp := false
for _, uid := range poduids {
localinfo, ok := agent.opflexSnatLocalInfos[uid]
if !ok {
continue
}
var i uint = 1
var pos uint = 0
var policystack []string
// 1. loop through all the resource hierarchy
// 2. Compute the Policy Stack
for ; i <= uint(CLUSTER); i = 1 << pos {
pos = pos + 1
seen := make(map[string]bool)
policies, ok := localinfo.Snatpolicies[ResourceType(i)]
var sortedpolicies []string
if ok {
for _, name := range policies {
if _, ok := seen[name]; !ok {
seen[name] = true
sortedpolicies = append(sortedpolicies, name)
} else {
continue
}
}
sort.Slice(sortedpolicies,
func(i, j int) bool {
return agent.compare(sortedpolicies[i], sortedpolicies[j])
})
}
policystack = append(policystack, sortedpolicies...)
}
var uids []string
for _, name := range policystack {
for _, val := range agent.opflexSnatGlobalInfos[agent.config.NodeName] {
if val.SnatPolicyName == name {
uids = append(uids, val.SnatIpUid)
}
}
if len(agent.snatPolicyCache[name].Spec.DestIp) == 0 {
break
}
}
if !reflect.DeepEqual(agent.opflexSnatLocalInfos[uid].PlcyUuids, uids) {
agent.log.Debug("Update EpFile: ", uids)
agent.opflexSnatLocalInfos[uid].PlcyUuids = uids
if len(uids) == 0 {
delete(agent.opflexSnatLocalInfos, uid)
}
syncEp = true
}
}
if syncEp {
agent.scheduleSyncEps()
}
agent.scheduleSyncLocalInfo()
}
func (agent *HostAgent) compare(plcy1, plcy2 string) bool {
sort := false
for _, a := range agent.snatPolicyCache[plcy1].Spec.DestIp {
ip_temp := net.ParseIP(a)
if ip_temp != nil && ip_temp.To4() != nil {
a = a + "/32"
}
for _, b := range agent.snatPolicyCache[plcy2].Spec.DestIp {
ip_temp := net.ParseIP(b)
if ip_temp != nil && ip_temp.To4() != nil {
b = b + "/32"
}
// TODO need to handle if the order is reversed across the policies.
// order reversing is ideally a wrong config. may be we need to block at verfication level
if compareIps(a, b) == true {
sort = true
}
}
}
return sort
}
func (agent *HostAgent) getMatchingSnatPolicy(obj interface{}) (snatPolicyNames map[string][]ResourceType) {
snatPolicyNames = make(map[string][]ResourceType)
_, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
return
}
metadata, err := meta.Accessor(obj)
if err != nil {
return
}
namespace := metadata.GetNamespace()
label := metadata.GetLabels()
res := getResourceType(obj)
for _, item := range agent.snatPolicyCache {
// check for empty policy selctor
if reflect.DeepEqual(item.Spec.Selector, snatpolicy.PodSelector{}) {
snatPolicyNames[item.ObjectMeta.Name] =
append(snatPolicyNames[item.ObjectMeta.Name], CLUSTER)
} else if len(item.Spec.Selector.Labels) == 0 &&
item.Spec.Selector.Namespace == namespace { // check policy matches namespace
if res == SERVICE {
if len(item.Spec.SnatIp) == 0 {
snatPolicyNames[item.ObjectMeta.Name] =
append(snatPolicyNames[item.ObjectMeta.Name], SERVICE)
}
} else {
if len(item.Spec.SnatIp) > 0 {
snatPolicyNames[item.ObjectMeta.Name] =
append(snatPolicyNames[item.ObjectMeta.Name], NAMESPACE)
}
}
} else { //Check Policy matches the labels on the Object
if (item.Spec.Selector.Namespace != "" &&
item.Spec.Selector.Namespace == namespace) ||
(item.Spec.Selector.Namespace == "") {
if util.MatchLabels(item.Spec.Selector.Labels, label) {
snatPolicyNames[item.ObjectMeta.Name] =
append(snatPolicyNames[item.ObjectMeta.Name], res)
}
if res == POD {
if len(item.Spec.SnatIp) == 0 {
var services []*v1.Service
cache.ListAllByNamespace(agent.serviceInformer.GetIndexer(), namespace, labels.Everything(),
func(servobj interface{}) {
services = append(services, servobj.(*v1.Service))
})
// list the pods and apply the policy at service target
for _, service := range services {
if util.MatchLabels(item.Spec.Selector.Labels,
service.ObjectMeta.Labels) {
snatPolicyNames[item.ObjectMeta.Name] =
append(snatPolicyNames[item.ObjectMeta.Name], SERVICE)
break
}