@@ -34,7 +34,6 @@ type NFTProxyProcessor struct {
3434
3535// InitRules initializes the nftables configuration in a single table "cozy_proxy".
3636// It flushes the entire ruleset, then re-creates the table with the desired sets, maps, and chains.
37- // (If a previous table existed, its set elements are saved and then restored.)
3837func (p * NFTProxyProcessor ) InitRules () error {
3938 log .Info ("Initializing nftables NAT configuration" )
4039
@@ -51,43 +50,6 @@ func (p *NFTProxyProcessor) InitRules() error {
5150 log .Info ("Using existing nftables connection" )
5251 }
5352
54- // --- Save existing table "cozy_proxy" if present ---
55- var savedPod , savedSvc , savedPodSvc , savedSvcPod []nftables.SetElement
56- tables , _ := p .conn .ListTables ()
57- var existingTable * nftables.Table
58- for _ , t := range tables {
59- if t .Family == nftables .TableFamilyIPv4 && t .Name == "cozy_proxy" {
60- existingTable = t
61- break
62- }
63- }
64- if existingTable != nil {
65- log .Info ("Found existing 'cozy_proxy' table; saving set/map elements" )
66- // Create dummy set objects for lookup.
67- dummyPod := & nftables.Set {Table : existingTable , Name : "pod" , KeyType : nftables .TypeIPAddr }
68- dummySvc := & nftables.Set {Table : existingTable , Name : "svc" , KeyType : nftables .TypeIPAddr }
69- dummyPodSvc := & nftables.Set {Table : existingTable , Name : "pod_svc" , KeyType : nftables .TypeIPAddr , DataType : nftables .TypeIPAddr , IsMap : true }
70- dummySvcPod := & nftables.Set {Table : existingTable , Name : "svc_pod" , KeyType : nftables .TypeIPAddr , DataType : nftables .TypeIPAddr , IsMap : true }
71- if elems , err := p .conn .GetSetElements (dummyPod ); err == nil {
72- savedPod = elems
73- }
74- if elems , err := p .conn .GetSetElements (dummySvc ); err == nil {
75- savedSvc = elems
76- }
77- if elems , err := p .conn .GetSetElements (dummyPodSvc ); err == nil {
78- savedPodSvc = elems
79- }
80- if elems , err := p .conn .GetSetElements (dummySvcPod ); err == nil {
81- savedSvcPod = elems
82- }
83- } else {
84- log .Info ("No existing 'cozy_proxy' table found; starting fresh" )
85- }
86-
87- // Flush the entire ruleset.
88- p .conn .FlushRuleset ()
89- log .Info ("Flushed entire ruleset" )
90-
9153 // --- Create new table "cozy_proxy" ---
9254 p .table = p .conn .AddTable (& nftables.Table {
9355 Family : nftables .TableFamilyIPv4 ,
@@ -148,34 +110,12 @@ func (p *NFTProxyProcessor) InitRules() error {
148110 }
149111 log .Info ("Created svc_pod map" , "map" , p .svcPodMap .Name )
150112
151- // Restore saved elements, if any.
152- if len (savedPod ) > 0 {
153- if err := p .conn .SetAddElements (p .podSet , savedPod ); err != nil {
154- log .Error (err , "Failed to restore elements to pod set" )
155- return fmt .Errorf ("failed to restore elements to pod set: %v" , err )
156- }
157- log .Info ("Restored elements to pod set" )
158- }
159- if len (savedSvc ) > 0 {
160- if err := p .conn .SetAddElements (p .svcSet , savedSvc ); err != nil {
161- log .Error (err , "Failed to restore elements to svc set" )
162- return fmt .Errorf ("failed to restore elements to svc set: %v" , err )
163- }
164- log .Info ("Restored elements to svc set" )
165- }
166- if len (savedPodSvc ) > 0 {
167- if err := p .conn .SetAddElements (p .podSvcMap , savedPodSvc ); err != nil {
168- log .Error (err , "Failed to restore elements to pod_svc map" )
169- return fmt .Errorf ("failed to restore elements to pod_svc map: %v" , err )
170- }
171- log .Info ("Restored elements to pod_svc map" )
172- }
173- if len (savedSvcPod ) > 0 {
174- if err := p .conn .SetAddElements (p .svcPodMap , savedSvcPod ); err != nil {
175- log .Error (err , "Failed to restore elements to svc_pod map" )
176- return fmt .Errorf ("failed to restore elements to svc_pod map: %v" , err )
113+ // --- Delete Chains ---
114+ chains , _ := p .conn .ListChains ()
115+ for _ , chain := range chains {
116+ if chain .Table .Name == p .table .Name {
117+ p .conn .DelChain (chain )
177118 }
178- log .Info ("Restored elements to svc_pod map" )
179119 }
180120
181121 // --- Create Chains ---
0 commit comments