@@ -9,21 +9,21 @@ import (
99 "github.com/Azure/azure-container-networking/npm/ipsm"
1010 "github.com/Azure/azure-container-networking/npm/iptm"
1111 "github.com/Azure/azure-container-networking/npm/util"
12- "k8s.io/apimachinery/pkg/types"
1312
1413 corev1 "k8s.io/api/core/v1"
1514 networkingv1 "k8s.io/api/networking/v1"
1615)
1716
1817type namespace struct {
19- name string
20- labelsMap map [string ]string
21- setMap map [string ]string
22- podMap map [types.UID ]* corev1.Pod
23- rawNpMap map [string ]* networkingv1.NetworkPolicy
24- processedNpMap map [string ]* networkingv1.NetworkPolicy
25- ipsMgr * ipsm.IpsetManager
26- iptMgr * iptm.IptablesManager
18+ name string
19+ labelsMap map [string ]string // NameSpace labels
20+ setMap map [string ]string
21+ podMap map [string ]* npmPod // Key is PodUID
22+ rawNpMap map [string ]* networkingv1.NetworkPolicy
23+ processedNpMap map [string ]* networkingv1.NetworkPolicy
24+ ipsMgr * ipsm.IpsetManager
25+ iptMgr * iptm.IptablesManager
26+ resourceVersion uint64 // NameSpace ResourceVersion
2727}
2828
2929// newNS constructs a new namespace object.
@@ -32,16 +32,24 @@ func newNs(name string) (*namespace, error) {
3232 name : name ,
3333 labelsMap : make (map [string ]string ),
3434 setMap : make (map [string ]string ),
35- podMap : make (map [types. UID ] * corev1. Pod ),
35+ podMap : make (map [string ] * npmPod ),
3636 rawNpMap : make (map [string ]* networkingv1.NetworkPolicy ),
3737 processedNpMap : make (map [string ]* networkingv1.NetworkPolicy ),
3838 ipsMgr : ipsm .NewIpsetManager (),
3939 iptMgr : iptm .NewIptablesManager (),
40+ // resource version is converted to uint64
41+ // so make sure it is initialized to "0"
42+ resourceVersion : 0 ,
4043 }
4144
4245 return ns , nil
4346}
4447
48+ // setResourceVersion setter func for RV
49+ func setResourceVersion (nsObj * namespace , rv string ) {
50+ nsObj .resourceVersion = util .ParseResourceVersion (rv )
51+ }
52+
4553func isSystemNs (nsObj * corev1.Namespace ) bool {
4654 return nsObj .ObjectMeta .Name == util .KubeSystemFlag
4755}
@@ -56,10 +64,22 @@ func isInvalidNamespaceUpdate(oldNsObj, newNsObj *corev1.Namespace) (isInvalidUp
5664}
5765
5866func (ns * namespace ) policyExists (npObj * networkingv1.NetworkPolicy ) bool {
59- if np , exists := ns .rawNpMap [npObj .ObjectMeta .Name ]; exists {
60- if isSamePolicy (np , npObj ) {
61- return true
62- }
67+ np , exists := ns .rawNpMap [npObj .ObjectMeta .Name ]
68+ if ! exists {
69+ return false
70+ }
71+
72+ if ! util .CompareResourceVersions (np .ObjectMeta .ResourceVersion , npObj .ObjectMeta .ResourceVersion ) {
73+ log .Logf ("Cached Network Policy has larger ResourceVersion number than new Obj. Name: %s Cached RV: %d New RV: %d\n " ,
74+ npObj .ObjectMeta .Name ,
75+ np .ObjectMeta .ResourceVersion ,
76+ npObj .ObjectMeta .ResourceVersion ,
77+ )
78+ return true
79+ }
80+
81+ if isSamePolicy (np , npObj ) {
82+ return true
6383 }
6484
6585 return false
@@ -103,7 +123,7 @@ func (npMgr *NetworkPolicyManager) UninitAllNsList() error {
103123func (npMgr * NetworkPolicyManager ) AddNamespace (nsObj * corev1.Namespace ) error {
104124 var err error
105125
106- nsName , nsLabel := "ns-" + nsObj .ObjectMeta .Name , nsObj .ObjectMeta .Labels
126+ nsName , nsLabel := util . GetNSNameWithPrefix ( nsObj .ObjectMeta .Name ) , nsObj .ObjectMeta .Labels
107127 log .Logf ("NAMESPACE CREATING: [%s/%v]" , nsName , nsLabel )
108128
109129 ipsMgr := npMgr .nsMap [util .KubeAllNamespacesFlag ].ipsMgr
@@ -121,14 +141,14 @@ func (npMgr *NetworkPolicyManager) AddNamespace(nsObj *corev1.Namespace) error {
121141 // Add the namespace to its label's ipset list.
122142 nsLabels := nsObj .ObjectMeta .Labels
123143 for nsLabelKey , nsLabelVal := range nsLabels {
124- labelKey := "ns-" + nsLabelKey
144+ labelKey := util . GetNSNameWithPrefix ( nsLabelKey )
125145 log .Logf ("Adding namespace %s to ipset list %s" , nsName , labelKey )
126146 if err = ipsMgr .AddToList (labelKey , nsName ); err != nil {
127147 log .Errorf ("Error: failed to add namespace %s to ipset list %s" , nsName , labelKey )
128148 return err
129149 }
130150
131- label := "ns-" + nsLabelKey + ":" + nsLabelVal
151+ label := util . GetNSNameWithPrefix ( nsLabelKey + ":" + nsLabelVal )
132152 log .Logf ("Adding namespace %s to ipset list %s" , nsName , label )
133153 if err = ipsMgr .AddToList (label , nsName ); err != nil {
134154 log .Errorf ("Error: failed to add namespace %s to ipset list %s" , nsName , label )
@@ -140,6 +160,7 @@ func (npMgr *NetworkPolicyManager) AddNamespace(nsObj *corev1.Namespace) error {
140160 if err != nil {
141161 log .Errorf ("Error: failed to create namespace %s" , nsName )
142162 }
163+ setResourceVersion (ns , nsObj .GetObjectMeta ().GetResourceVersion ())
143164
144165 // Append all labels to the cache NS obj
145166 ns .labelsMap = util .AppendMap (ns .labelsMap , nsLabel )
@@ -155,8 +176,8 @@ func (npMgr *NetworkPolicyManager) UpdateNamespace(oldNsObj *corev1.Namespace, n
155176 }
156177
157178 var err error
158- oldNsNs , oldNsLabel := "ns-" + oldNsObj .ObjectMeta .Name , oldNsObj .ObjectMeta .Labels
159- newNsNs , newNsLabel := "ns-" + newNsObj .ObjectMeta .Name , newNsObj .ObjectMeta .Labels
179+ oldNsNs , oldNsLabel := util . GetNSNameWithPrefix ( oldNsObj .ObjectMeta .Name ) , oldNsObj .ObjectMeta .Labels
180+ newNsNs , newNsLabel := util . GetNSNameWithPrefix ( newNsObj .ObjectMeta .Name ) , newNsObj .ObjectMeta .Labels
160181 log .Logf (
161182 "NAMESPACE UPDATING:\n old namespace: [%s/%v]\n new namespace: [%s/%v]" ,
162183 oldNsNs , oldNsLabel , newNsNs , newNsLabel ,
@@ -189,6 +210,16 @@ func (npMgr *NetworkPolicyManager) UpdateNamespace(oldNsObj *corev1.Namespace, n
189210 return nil
190211 }
191212
213+ newRv := util .ParseResourceVersion (newNsObj .ObjectMeta .ResourceVersion )
214+ if ! util .CompareUintResourceVersions (curNsObj .resourceVersion , newRv ) {
215+ log .Logf ("Cached NameSpace has larger ResourceVersion number than new Obj. NameSpace: %s Cached RV: %d New RV:\n " ,
216+ oldNsNs ,
217+ curNsObj .resourceVersion ,
218+ newRv ,
219+ )
220+ return nil
221+ }
222+
192223 //if no change in labels then return
193224 if reflect .DeepEqual (curNsObj .labelsMap , newNsLabel ) {
194225 log .Logf (
@@ -199,45 +230,32 @@ func (npMgr *NetworkPolicyManager) UpdateNamespace(oldNsObj *corev1.Namespace, n
199230 }
200231
201232 //If the Namespace is not deleted, delete removed labels and create new labels
202- toAddNsLabels , toDeleteNsLabels := util .CompareMapDiff (curNsObj .labelsMap , newNsLabel )
233+ addToIPSets , deleteFromIPSets := util .GetIPSetListCompareLabels (curNsObj .labelsMap , newNsLabel )
203234
204235 // Delete the namespace from its label's ipset list.
205236 ipsMgr := npMgr .nsMap [util .KubeAllNamespacesFlag ].ipsMgr
206- for nsLabelKey , nsLabelVal := range toDeleteNsLabels {
207- labelKey := "ns-" + nsLabelKey
237+ for _ , nsLabelVal := range deleteFromIPSets {
238+ labelKey := util . GetNSNameWithPrefix ( nsLabelVal )
208239 log .Logf ("Deleting namespace %s from ipset list %s" , oldNsNs , labelKey )
209240 if err = ipsMgr .DeleteFromList (labelKey , oldNsNs ); err != nil {
210241 log .Errorf ("Error: failed to delete namespace %s from ipset list %s" , oldNsNs , labelKey )
211242 return err
212243 }
213-
214- label := "ns-" + nsLabelKey + ":" + nsLabelVal
215- log .Logf ("Deleting namespace %s from ipset list %s" , oldNsNs , label )
216- if err = ipsMgr .DeleteFromList (label , oldNsNs ); err != nil {
217- log .Errorf ("Error: failed to delete namespace %s from ipset list %s" , oldNsNs , label )
218- return err
219- }
220244 }
221245
222246 // Add the namespace to its label's ipset list.
223- for nsLabelKey , nsLabelVal := range toAddNsLabels {
224- labelKey := "ns-" + nsLabelKey
247+ for _ , nsLabelVal := range addToIPSets {
248+ labelKey := util . GetNSNameWithPrefix ( nsLabelVal )
225249 log .Logf ("Adding namespace %s to ipset list %s" , oldNsNs , labelKey )
226250 if err = ipsMgr .AddToList (labelKey , oldNsNs ); err != nil {
227251 log .Errorf ("Error: failed to add namespace %s to ipset list %s" , oldNsNs , labelKey )
228252 return err
229253 }
230-
231- label := "ns-" + nsLabelKey + ":" + nsLabelVal
232- log .Logf ("Adding namespace %s to ipset list %s" , oldNsNs , label )
233- if err = ipsMgr .AddToList (label , oldNsNs ); err != nil {
234- log .Errorf ("Error: failed to add namespace %s to ipset list %s" , oldNsNs , label )
235- return err
236- }
237254 }
238255
239256 // Append all labels to the cache NS obj
240257 curNsObj .labelsMap = util .ClearAndAppendMap (curNsObj .labelsMap , newNsLabel )
258+ setResourceVersion (curNsObj , newNsObj .GetObjectMeta ().GetResourceVersion ())
241259 npMgr .nsMap [newNsNs ] = curNsObj
242260
243261 return nil
@@ -247,26 +265,27 @@ func (npMgr *NetworkPolicyManager) UpdateNamespace(oldNsObj *corev1.Namespace, n
247265func (npMgr * NetworkPolicyManager ) DeleteNamespace (nsObj * corev1.Namespace ) error {
248266 var err error
249267
250- nsName , nsLabel := "ns-" + nsObj .ObjectMeta .Name , nsObj .ObjectMeta .Labels
268+ nsName , nsLabel := util . GetNSNameWithPrefix ( nsObj .ObjectMeta .Name ) , nsObj .ObjectMeta .Labels
251269 log .Logf ("NAMESPACE DELETING: [%s/%v]" , nsName , nsLabel )
252270
253- _ , exists := npMgr .nsMap [nsName ]
271+ cachedNsObj , exists := npMgr .nsMap [nsName ]
254272 if ! exists {
255273 return nil
256274 }
257275
276+ log .Logf ("NAMESPACE DELETING cached labels: [%s/%v]" , nsName , cachedNsObj .labelsMap )
258277 // Delete the namespace from its label's ipset list.
259278 ipsMgr := npMgr .nsMap [util .KubeAllNamespacesFlag ].ipsMgr
260- nsLabels := nsObj . ObjectMeta . Labels
279+ nsLabels := cachedNsObj . labelsMap
261280 for nsLabelKey , nsLabelVal := range nsLabels {
262- labelKey := "ns-" + nsLabelKey
281+ labelKey := util . GetNSNameWithPrefix ( nsLabelKey )
263282 log .Logf ("Deleting namespace %s from ipset list %s" , nsName , labelKey )
264283 if err = ipsMgr .DeleteFromList (labelKey , nsName ); err != nil {
265284 log .Errorf ("Error: failed to delete namespace %s from ipset list %s" , nsName , labelKey )
266285 return err
267286 }
268287
269- label := "ns-" + nsLabelKey + ":" + nsLabelVal
288+ label := util . GetNSNameWithPrefix ( nsLabelKey + ":" + nsLabelVal )
270289 log .Logf ("Deleting namespace %s from ipset list %s" , nsName , label )
271290 if err = ipsMgr .DeleteFromList (label , nsName ); err != nil {
272291 log .Errorf ("Error: failed to delete namespace %s from ipset list %s" , nsName , label )
0 commit comments