@@ -23,7 +23,7 @@ import (
2323 "strings"
2424
2525 "github.com/k8snetworkplumbingwg/multi-networkpolicy-iptables/pkg/controllers"
26- multiv1beta2 "github.com/k8snetworkplumbingwg/multi-networkpolicy/pkg/apis/k8s.cni.cncf.io/v1beta2 "
26+ multiv1beta1 "github.com/k8snetworkplumbingwg/multi-networkpolicy/pkg/apis/k8s.cni.cncf.io/v1beta1 "
2727 v1 "k8s.io/api/core/v1"
2828 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929 "k8s.io/apimachinery/pkg/labels"
@@ -141,10 +141,10 @@ func (ipt *iptableBuffer) FinalizeRules() {
141141
142142func (ipt * iptableBuffer ) SaveRules (path string ) error {
143143 file , err := os .Create (path )
144+ defer file .Close ()
144145 if err != nil {
145146 return err
146147 }
147- defer file .Close ()
148148 //_, err = ipt.filterRules.WriteTo(file)
149149 fmt .Fprintf (file , "%s" , ipt .filterRules .String ())
150150 return err
@@ -216,7 +216,7 @@ func (ipt *iptableBuffer) renderIngressCommon(s *Server) {
216216 writeLine (ipt .policyCommon , "-A" , ingressCommonChain , "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" )
217217}
218218
219- func (ipt * iptableBuffer ) renderIngress (s * Server , podInfo * controllers.PodInfo , idx int , policy * multiv1beta2 .MultiNetworkPolicy , policyNetworks []string ) {
219+ func (ipt * iptableBuffer ) renderIngress (s * Server , podInfo * controllers.PodInfo , idx int , policy * multiv1beta1 .MultiNetworkPolicy , policyNetworks []string ) {
220220 chainName := fmt .Sprintf ("MULTI-%d-INGRESS" , idx )
221221 ipt .CreateFilterChain (chainName )
222222
@@ -240,7 +240,7 @@ func (ipt *iptableBuffer) renderIngress(s *Server, podInfo *controllers.PodInfo,
240240 }
241241}
242242
243- func (ipt * iptableBuffer ) renderIngressPorts (_ * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , ports []multiv1beta2 .MultiNetworkPolicyPort , policyNetworks []string ) {
243+ func (ipt * iptableBuffer ) renderIngressPorts (_ * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , ports []multiv1beta1 .MultiNetworkPolicyPort , policyNetworks []string ) {
244244 chainName := fmt .Sprintf ("MULTI-%d-INGRESS-%d-PORTS" , pIndex , iIndex )
245245 ipt .CreateFilterChain (chainName )
246246
@@ -255,19 +255,11 @@ func (ipt *iptableBuffer) renderIngressPorts(_ *Server, podInfo *controllers.Pod
255255 if ! podIntf .CheckPolicyNetwork (policyNetworks ) {
256256 continue
257257 }
258- if port .EndPort != nil {
259- writeLine (ipt .ingressPorts , "-A" , chainName ,
260- "-i" , podIntf .InterfaceName ,
261- "-m" , proto , "-p" , proto , "--dport" , fmt .Sprintf ("%s:%d" , port .Port .String (), * port .EndPort ),
262- "-j" , "MARK" , "--set-xmark" , "0x10000/0x10000" )
263- validPorts ++
264- } else {
265- writeLine (ipt .ingressPorts , "-A" , chainName ,
266- "-i" , podIntf .InterfaceName ,
267- "-m" , proto , "-p" , proto , "--dport" , port .Port .String (),
268- "-j" , "MARK" , "--set-xmark" , "0x10000/0x10000" )
269- validPorts ++
270- }
258+ writeLine (ipt .ingressPorts , "-A" , chainName ,
259+ "-i" , podIntf .InterfaceName ,
260+ "-m" , proto , "-p" , proto , "--dport" , port .Port .String (),
261+ "-j" , "MARK" , "--set-xmark" , "0x10000/0x10000" )
262+ validPorts ++
271263 }
272264 }
273265
@@ -277,9 +269,10 @@ func (ipt *iptableBuffer) renderIngressPorts(_ *Server, podInfo *controllers.Pod
277269 "-m" , "comment" , "--comment" , "\" no ingress ports, skipped\" " ,
278270 "-j" , "MARK" , "--set-xmark" , "0x10000/0x10000" )
279271 }
272+ return
280273}
281274
282- func (ipt * iptableBuffer ) renderIngressFrom (s * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , from []multiv1beta2 .MultiNetworkPolicyPeer , policyNetworks []string ) {
275+ func (ipt * iptableBuffer ) renderIngressFrom (s * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , from []multiv1beta1 .MultiNetworkPolicyPeer , policyNetworks []string ) {
283276 chainName := fmt .Sprintf ("MULTI-%d-INGRESS-%d-FROM" , pIndex , iIndex )
284277 ipt .CreateFilterChain (chainName )
285278
@@ -398,6 +391,7 @@ func (ipt *iptableBuffer) renderIngressFrom(s *Server, podInfo *controllers.PodI
398391 "-m" , "comment" , "--comment" , "\" no ingress from, skipped\" " ,
399392 "-j" , "MARK" , "--set-xmark" , "0x20000/0x20000" )
400393 }
394+ return
401395}
402396
403397func (ipt * iptableBuffer ) renderEgressCommon (s * Server ) {
@@ -448,7 +442,7 @@ func (ipt *iptableBuffer) renderEgressCommon(s *Server) {
448442 writeLine (ipt .policyCommon , "-A" , egressCommonChain , "-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" )
449443}
450444
451- func (ipt * iptableBuffer ) renderEgress (s * Server , podInfo * controllers.PodInfo , idx int , policy * multiv1beta2 .MultiNetworkPolicy , policyNetworks []string ) {
445+ func (ipt * iptableBuffer ) renderEgress (s * Server , podInfo * controllers.PodInfo , idx int , policy * multiv1beta1 .MultiNetworkPolicy , policyNetworks []string ) {
452446 chainName := fmt .Sprintf ("MULTI-%d-EGRESS" , idx )
453447 ipt .CreateFilterChain (chainName )
454448
@@ -471,7 +465,7 @@ func (ipt *iptableBuffer) renderEgress(s *Server, podInfo *controllers.PodInfo,
471465 }
472466}
473467
474- func (ipt * iptableBuffer ) renderEgressPorts (_ * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , ports []multiv1beta2 .MultiNetworkPolicyPort , policyNetworks []string ) {
468+ func (ipt * iptableBuffer ) renderEgressPorts (_ * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , ports []multiv1beta1 .MultiNetworkPolicyPort , policyNetworks []string ) {
475469 chainName := fmt .Sprintf ("MULTI-%d-EGRESS-%d-PORTS" , pIndex , iIndex )
476470 ipt .CreateFilterChain (chainName )
477471
@@ -500,9 +494,10 @@ func (ipt *iptableBuffer) renderEgressPorts(_ *Server, podInfo *controllers.PodI
500494 "-m" , "comment" , "--comment" , "\" no egress ports, skipped\" " ,
501495 "-j" , "MARK" , "--set-xmark" , "0x10000/0x10000" )
502496 }
497+ return
503498}
504499
505- func (ipt * iptableBuffer ) renderEgressTo (s * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , to []multiv1beta2 .MultiNetworkPolicyPeer , policyNetworks []string ) {
500+ func (ipt * iptableBuffer ) renderEgressTo (s * Server , podInfo * controllers.PodInfo , pIndex , iIndex int , to []multiv1beta1 .MultiNetworkPolicyPeer , policyNetworks []string ) {
506501 chainName := fmt .Sprintf ("MULTI-%d-EGRESS-%d-TO" , pIndex , iIndex )
507502 ipt .CreateFilterChain (chainName )
508503
@@ -623,6 +618,7 @@ func (ipt *iptableBuffer) renderEgressTo(s *Server, podInfo *controllers.PodInfo
623618 "-m" , "comment" , "--comment" , "\" no egress to, skipped\" " ,
624619 "-j" , "MARK" , "--set-xmark" , "0x20000/0x20000" )
625620 }
621+ return
626622}
627623
628624func (ipt * iptableBuffer ) isIPFamilyCompatible (ip string ) bool {
0 commit comments