@@ -147,23 +147,24 @@ func isBaseChain(chain string) bool {
147147}
148148
149149/*
150- Called once at startup.
151- Like the rest of PolicyManager, minimizes the number of OS calls by consolidating all possible actions into one iptables-restore call.
152-
153- 1. Delete the deprecated jump from FORWARD to AZURE-NPM chain (if it exists).
154- 2. Cleanup old NPM chains, and configure base chains and their rules.
155- 1. Do the following via iptables-restore --noflush:
156- - flush all deprecated chains
157- - flush old v2 policy chains
158- - create/flush the base chains
159- - add rules for the base chains, except for AZURE-NPM (so that PolicyManager will be deactivated)
160- 2. In the background:
161- - delete all deprecated chains
162- - delete old v2 policy chains
163- 3. Add/reposition the jump from FORWARD chain to AZURE-NPM chain.
164-
165- TODO: could use one grep call instead of separate calls for getting jump line nums and for getting deprecated chains and old v2 policy chains
166- - would use a grep pattern like so: <line num...AZURE-NPM>|<Chain AZURE-NPM>
150+ Called once at startup.
151+ Like the rest of PolicyManager, minimizes the number of OS calls by consolidating all possible actions into one iptables-restore call.
152+
153+ 1. Delete the deprecated jump from FORWARD to AZURE-NPM chain (if it exists).
154+ 2. Cleanup old NPM chains, and configure base chains and their rules.
155+ 1. Do the following via iptables-restore --noflush:
156+ - flush all deprecated chains
157+ - flush old v2 policy chains
158+ - create/flush the base chains
159+ - add rules for the base chains, except for AZURE-NPM (so that PolicyManager will be deactivated)
160+ 2. In the background:
161+ - delete all deprecated chains
162+ - delete old v2 policy chains
163+
164+ 3. Add/reposition the jump from FORWARD chain to AZURE-NPM chain.
165+
166+ TODO: could use one grep call instead of separate calls for getting jump line nums and for getting deprecated chains and old v2 policy chains
167+ - would use a grep pattern like so: <line num...AZURE-NPM>|<Chain AZURE-NPM>
167168*/
168169func (pMgr * PolicyManager ) bootup (_ []string ) error {
169170 klog .Infof ("booting up iptables Azure chains" )
@@ -173,6 +174,79 @@ func (pMgr *PolicyManager) bootup(_ []string) error {
173174 pMgr .reconcileManager .forceLock ()
174175 defer pMgr .reconcileManager .forceUnlock ()
175176
177+ if strings .Contains (util .Iptables , "nft" ) {
178+ util .Iptables = util .IptablesLegacy
179+ util .IptablesSave = util .IptablesSaveLegacy
180+ util .IptablesRestore = util .IptablesRestoreLegacy
181+
182+ // 0. delete the deprecated jump to deprecated AZURE-NPM in legacy iptables
183+ deprecatedErrCode , deprecatedErr := pMgr .ignoreErrorsAndRunIPTablesCommand (removeDeprecatedJumpIgnoredErrors , util .IptablesDeletionFlag , deprecatedJumpFromForwardToAzureChainArgs ... )
184+ if deprecatedErrCode == 0 {
185+ klog .Infof ("deleted deprecated jump rule from FORWARD chain to AZURE-NPM chain" )
186+ } else if deprecatedErr != nil {
187+ metrics .SendErrorLogAndMetric (util .IptmID ,
188+ "failed to delete deprecated jump rule from FORWARD chain to AZURE-NPM chain for unexpected reason with exit code %d and error: %s" ,
189+ deprecatedErrCode , deprecatedErr .Error ())
190+ }
191+
192+ // 0. delete the deprecated jump to current AZURE-NPM in legacy iptables
193+ deprecatedErrCode , deprecatedErr = pMgr .ignoreErrorsAndRunIPTablesCommand (removeDeprecatedJumpIgnoredErrors , util .IptablesDeletionFlag , jumpFromForwardToAzureChainArgs ... )
194+ if deprecatedErrCode == 0 {
195+ klog .Infof ("deleted deprecated jump rule from FORWARD chain to AZURE-NPM chain" )
196+ } else if deprecatedErr != nil {
197+ metrics .SendErrorLogAndMetric (util .IptmID ,
198+ "failed to delete deprecated jump rule from FORWARD chain to AZURE-NPM chain for unexpected reason with exit code %d and error: %s" ,
199+ deprecatedErrCode , deprecatedErr .Error ())
200+ }
201+
202+ // clean up current chains in legacy iptables
203+ currentChains , err := ioutil .AllCurrentAzureChains (pMgr .ioShim .Exec , util .IptablesDefaultWaitTime )
204+ if err != nil {
205+ return npmerrors .SimpleErrorWrapper ("failed to get current chains for bootup" , err )
206+ }
207+
208+ // We have only one chance to clean existing legacy iptables chains.
209+ // So flush all the chains and then destroy them
210+ var aggregateError error
211+ for chain := range currentChains {
212+ errCode , err := pMgr .runIPTablesCommand (util .IptablesFlushFlag , chain )
213+ if err != nil && errCode != doesNotExistErrorCode {
214+ // add to staleChains if it's not one of the iptablesAzureChains
215+ pMgr .staleChains .add (chain )
216+ currentErrString := fmt .Sprintf ("failed to flush chain %s with err [%v]" , chain , err )
217+ if aggregateError == nil {
218+ aggregateError = npmerrors .SimpleError (currentErrString )
219+ } else {
220+ aggregateError = npmerrors .SimpleErrorWrapper (fmt .Sprintf ("%s and had previous error" , currentErrString ), aggregateError )
221+ }
222+ }
223+ }
224+
225+ for chain := range currentChains {
226+ errCode , err := pMgr .runIPTablesCommand (util .IptablesDestroyFlag , chain )
227+ if err != nil && errCode != doesNotExistErrorCode {
228+ // add to staleChains if it's not one of the iptablesAzureChains
229+ pMgr .staleChains .add (chain )
230+ currentErrString := fmt .Sprintf ("failed to delete chain %s with err [%v]" , chain , err )
231+ if aggregateError == nil {
232+ aggregateError = npmerrors .SimpleError (currentErrString )
233+ } else {
234+ aggregateError = npmerrors .SimpleErrorWrapper (fmt .Sprintf ("%s and had previous error" , currentErrString ), aggregateError )
235+ }
236+ }
237+ }
238+
239+ if aggregateError != nil {
240+ metrics .SendErrorLogAndMetric (util .IptmID ,
241+ "failed to flush and delete stale chain in legacy iptables with error: %s" ,
242+ aggregateError .Error ())
243+ }
244+
245+ util .Iptables = util .IptablesNft
246+ util .IptablesSave = util .IptablesSaveNft
247+ util .IptablesRestore = util .IptablesRestoreNft
248+ }
249+
176250 // 1. delete the deprecated jump to AZURE-NPM
177251 deprecatedErrCode , deprecatedErr := pMgr .ignoreErrorsAndRunIPTablesCommand (removeDeprecatedJumpIgnoredErrors , util .IptablesDeletionFlag , deprecatedJumpFromForwardToAzureChainArgs ... )
178252 if deprecatedErrCode == 0 {
0 commit comments