@@ -10,6 +10,7 @@ import (
1010 "github.com/Azure/azure-container-networking/cns/middlewares/utils"
1111 "github.com/Azure/azure-container-networking/cns/types"
1212 "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
13+ "github.com/Azure/azure-container-networking/network/policy"
1314 "github.com/pkg/errors"
1415 v1 "k8s.io/api/core/v1"
1516 k8stypes "k8s.io/apimachinery/pkg/types"
@@ -40,7 +41,9 @@ var _ cns.IPConfigsHandlerMiddleware = (*K8sSWIFTv2Middleware)(nil)
4041// and release IP configs handlers.
4142func (k * K8sSWIFTv2Middleware ) IPConfigsRequestHandlerWrapper (defaultHandler , failureHandler cns.IPConfigsHandlerFunc ) cns.IPConfigsHandlerFunc {
4243 return func (ctx context.Context , req cns.IPConfigsRequest ) (* cns.IPConfigsResponse , error ) {
43- podInfo , respCode , message := k .validateIPConfigsRequest (ctx , & req )
44+ podInfo , respCode , defaultDenyACLbool , message := k .GetPodInfoForIPConfigsRequest (ctx , & req )
45+
46+ logger .Printf ("defaultDenyACLbool value is: %v" , defaultDenyACLbool )
4447
4548 if respCode != types .Success {
4649 return & cns.IPConfigsResponse {
@@ -55,6 +58,31 @@ func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
5558 if ! req .SecondaryInterfacesExist {
5659 return ipConfigsResp , err
5760 }
61+
62+ // ipConfigsResp has infra IP configs -> if defaultDenyACLbool is enabled, add the default deny endpoint policies as a property in PodIpInfo
63+ for i := range ipConfigsResp .PodIPInfo {
64+ ipInfo := & ipConfigsResp .PodIPInfo [i ]
65+ // there will be no pod connectivity to and from those pods
66+ var defaultDenyEgressPolicy , defaultDenyIngressPolicy policy.Policy
67+
68+ if defaultDenyACLbool && ipInfo .NICType == cns .InfraNIC {
69+ defaultDenyEgressPolicy , err = getEndpointPolicy (string (policy .ACLPolicy ), cns .ActionTypeBlock , cns .DirectionTypeOut , 10_000 )
70+ if err != nil {
71+ logger .Errorf ("failed to add default deny acl's for pod %v with err %v" , podInfo .Name (), err )
72+ }
73+
74+ defaultDenyIngressPolicy , err = getEndpointPolicy (string (policy .ACLPolicy ), cns .ActionTypeBlock , cns .DirectionTypeIn , 10_000 )
75+ if err != nil {
76+ logger .Errorf ("failed to add default deny acl's for pod %v with err %v" , podInfo .Name (), err )
77+ }
78+
79+ ipInfo .EndpointPolicies = append (ipInfo .EndpointPolicies , defaultDenyEgressPolicy , defaultDenyIngressPolicy )
80+ logger .Printf ("Created endpoint policies for defaultDenyEgressPolicy and defaultDenyIngressPolicy" )
81+
82+ break
83+ }
84+ }
85+
5886 // If the pod is v2, get the infra IP configs from the handler first and then add the SWIFTv2 IP config
5987 defer func () {
6088 // Release the default IP config if there is an error
@@ -100,21 +128,23 @@ func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
100128 }
101129}
102130
103- // validateIPConfigsRequest validates if pod is multitenant by checking the pod labels, used in SWIFT V2 AKS scenario.
131+ // GetPodInfoForIPConfigsRequest validates if pod is multitenant by checking the pod labels, used in SWIFT V2 AKS scenario.
104132// nolint
105- func (k * K8sSWIFTv2Middleware ) validateIPConfigsRequest (ctx context.Context , req * cns.IPConfigsRequest ) (podInfo cns.PodInfo , respCode types.ResponseCode , message string ) {
133+ func (k * K8sSWIFTv2Middleware ) GetPodInfoForIPConfigsRequest (ctx context.Context , req * cns.IPConfigsRequest ) (podInfo cns.PodInfo , respCode types.ResponseCode , defaultDenyACL bool , message string ) {
134+ defaultDenyACLbool := false
135+
106136 // Retrieve the pod from the cluster
107137 podInfo , err := cns .UnmarshalPodInfo (req .OrchestratorContext )
108138 if err != nil {
109139 errBuf := errors .Wrapf (err , "failed to unmarshalling pod info from ipconfigs request %+v" , req )
110- return nil , types .UnexpectedError , errBuf .Error ()
140+ return nil , types .UnexpectedError , defaultDenyACLbool , errBuf .Error ()
111141 }
112142 logger .Printf ("[SWIFTv2Middleware] validate ipconfigs request for pod %s" , podInfo .Name ())
113143 podNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
114144 pod := v1.Pod {}
115145 if err := k .Cli .Get (ctx , podNamespacedName , & pod ); err != nil {
116146 errBuf := errors .Wrapf (err , "failed to get pod %+v" , podNamespacedName )
117- return nil , types .UnexpectedError , errBuf .Error ()
147+ return nil , types .UnexpectedError , defaultDenyACLbool , errBuf .Error ()
118148 }
119149
120150 // check the pod labels for Swift V2, set the request's SecondaryInterfaceSet flag to true and check if its MTPNC CRD is ready
@@ -126,12 +156,16 @@ func (k *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
126156 mtpnc := v1alpha1.MultitenantPodNetworkConfig {}
127157 mtpncNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
128158 if err := k .Cli .Get (ctx , mtpncNamespacedName , & mtpnc ); err != nil {
129- return nil , types .UnexpectedError , fmt .Errorf ("failed to get pod's mtpnc from cache : %w" , err ).Error ()
159+ return nil , types .UnexpectedError , defaultDenyACLbool , fmt .Errorf ("failed to get pod's mtpnc from cache : %w" , err ).Error ()
130160 }
131161 // Check if the MTPNC CRD is ready. If one of the fields is empty, return error
132162 if ! mtpnc .IsReady () {
133- return nil , types .UnexpectedError , errMTPNCNotReady .Error ()
163+ return nil , types .UnexpectedError , defaultDenyACLbool , errMTPNCNotReady .Error ()
134164 }
165+
166+ // copying defaultDenyACL bool from mtpnc
167+ defaultDenyACLbool = mtpnc .Status .DefaultDenyACL
168+
135169 // If primary Ip is set in status field, it indicates the presence of secondary interfaces
136170 if mtpnc .Status .PrimaryIP != "" {
137171 req .SecondaryInterfacesExist = true
@@ -140,7 +174,7 @@ func (k *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
140174 for _ , interfaceInfo := range interfaceInfos {
141175 if interfaceInfo .DeviceType == v1alpha1 .DeviceTypeInfiniBandNIC {
142176 if interfaceInfo .MacAddress == "" || interfaceInfo .NCID == "" {
143- return nil , types .UnexpectedError , errMTPNCNotReady .Error ()
177+ return nil , types .UnexpectedError , defaultDenyACLbool , errMTPNCNotReady .Error ()
144178 }
145179 req .BackendInterfaceExist = true
146180 req .BackendInterfaceMacAddresses = append (req .BackendInterfaceMacAddresses , interfaceInfo .MacAddress )
@@ -154,7 +188,7 @@ func (k *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
154188 logger .Printf ("[SWIFTv2Middleware] pod %s has secondary interface : %v" , podInfo .Name (), req .SecondaryInterfacesExist )
155189 logger .Printf ("[SWIFTv2Middleware] pod %s has backend interface : %v" , podInfo .Name (), req .BackendInterfaceExist )
156190 // retrieve podinfo from orchestrator context
157- return podInfo , types .Success , ""
191+ return podInfo , types .Success , defaultDenyACLbool , ""
158192}
159193
160194// getIPConfig returns the pod's SWIFT V2 IP configuration.
0 commit comments