@@ -36,124 +36,33 @@ type K8sSWIFTv2Middleware struct {
3636// Verify interface compliance at compile time
3737var _ cns.IPConfigsHandlerMiddleware = (* K8sSWIFTv2Middleware )(nil )
3838
39- // IPConfigsRequestHandlerWrapper is the middleware function for handling SWIFT v2 IP configs requests for AKS-SWIFT. This function wrapped the default SWIFT request
40- // and release IP configs handlers.
41- func (k * K8sSWIFTv2Middleware ) IPConfigsRequestHandlerWrapper (defaultHandler , failureHandler cns.IPConfigsHandlerFunc ) cns.IPConfigsHandlerFunc {
42- return func (ctx context.Context , req cns.IPConfigsRequest ) (* cns.IPConfigsResponse , error ) {
43- podInfo , respCode , message := k .validateIPConfigsRequest (ctx , & req )
44-
45- if respCode != types .Success {
46- return & cns.IPConfigsResponse {
47- Response : cns.Response {
48- ReturnCode : respCode ,
49- Message : message ,
50- },
51- }, errors .New ("failed to validate IP configs request" )
52- }
53- ipConfigsResp , err := defaultHandler (ctx , req )
54- // If the pod is not v2, return the response from the handler
55- if ! req .SecondaryInterfacesExist {
56- return ipConfigsResp , err
57- }
58- // If the pod is v2, get the infra IP configs from the handler first and then add the SWIFTv2 IP config
59- defer func () {
60- // Release the default IP config if there is an error
61- if err != nil {
62- _ , err = failureHandler (ctx , req )
63- if err != nil {
64- logger .Errorf ("failed to release default IP config : %v" , err )
65- }
66- }
67- }()
68- if err != nil {
69- return ipConfigsResp , err
70- }
71- SWIFTv2PodIPInfos , err := k .getIPConfig (ctx , podInfo )
72- if err != nil {
73- return & cns.IPConfigsResponse {
74- Response : cns.Response {
75- ReturnCode : types .FailedToAllocateIPConfig ,
76- Message : fmt .Sprintf ("AllocateIPConfig failed: %v, IP config request is %v" , err , req ),
77- },
78- PodIPInfo : []cns.PodIpInfo {},
79- }, errors .Wrapf (err , "failed to get SWIFTv2 IP config : %v" , req )
80- }
81- ipConfigsResp .PodIPInfo = append (ipConfigsResp .PodIPInfo , SWIFTv2PodIPInfos ... )
82- // Set routes for the pod
83- for i := range ipConfigsResp .PodIPInfo {
84- ipInfo := & ipConfigsResp .PodIPInfo [i ]
85- // Backend nics doesn't need routes to be set
86- if ipInfo .NICType != cns .BackendNIC {
87- err = k .setRoutes (ipInfo )
88- if err != nil {
89- return & cns.IPConfigsResponse {
90- Response : cns.Response {
91- ReturnCode : types .FailedToAllocateIPConfig ,
92- Message : fmt .Sprintf ("AllocateIPConfig failed: %v, IP config request is %v" , err , req ),
93- },
94- PodIPInfo : []cns.PodIpInfo {},
95- }, errors .Wrapf (err , "failed to set routes for pod %s" , podInfo .Name ())
96- }
97- }
98- }
99- return ipConfigsResp , nil
39+ func (k * K8sSWIFTv2Middleware ) GetPodInfoForIPConfigsRequest (ctx context.Context , req * cns.IPConfigsRequest ) (podInfo cns.PodInfo , respCode types.ResponseCode , message string ) {
40+ // gets pod info for the specified request
41+ podInfo , pod , respCode , message := k .GetPodInfo (ctx , req )
42+ if respCode != types .Success {
43+ return nil , respCode , message
10044 }
101- }
10245
103- // validateIPConfigsRequest validates if pod is multitenant by checking the pod labels, used in SWIFT V2 AKS scenario.
104- // nolint
105- func (k * K8sSWIFTv2Middleware ) validateIPConfigsRequest (ctx context.Context , req * cns.IPConfigsRequest ) (podInfo cns.PodInfo , respCode types.ResponseCode , message string ) {
106- // Retrieve the pod from the cluster
107- podInfo , err := cns .UnmarshalPodInfo (req .OrchestratorContext )
108- if err != nil {
109- errBuf := errors .Wrapf (err , "failed to unmarshalling pod info from ipconfigs request %+v" , req )
110- return nil , types .UnexpectedError , errBuf .Error ()
111- }
112- logger .Printf ("[SWIFTv2Middleware] validate ipconfigs request for pod %s" , podInfo .Name ())
113- podNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
114- pod := v1.Pod {}
115- if err := k .Cli .Get (ctx , podNamespacedName , & pod ); err != nil {
116- errBuf := errors .Wrapf (err , "failed to get pod %+v" , podNamespacedName )
117- return nil , types .UnexpectedError , errBuf .Error ()
118- }
119-
120- // check the pod labels for Swift V2, set the request's SecondaryInterfaceSet flag to true and check if its MTPNC CRD is ready
121- _ , swiftV2PodNetworkLabel := pod .Labels [configuration .LabelPodSwiftV2 ]
122- _ , swiftV2PodNetworkInstanceLabel := pod .Labels [configuration .LabelPodNetworkInstanceSwiftV2 ]
123- if swiftV2PodNetworkLabel || swiftV2PodNetworkInstanceLabel {
46+ // validates if pod is swiftv2
47+ isSwiftv2 := ValidateSwiftv2Pod (pod )
12448
125- // Check if the MTPNC CRD exists for the pod, if not, return error
126- mtpnc := v1alpha1.MultitenantPodNetworkConfig {}
127- mtpncNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
128- 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 ()
130- }
131- // Check if the MTPNC CRD is ready. If one of the fields is empty, return error
132- if ! mtpnc .IsReady () {
133- return nil , types .UnexpectedError , errMTPNCNotReady .Error ()
134- }
135- // If primary Ip is set in status field, it indicates the presence of secondary interfaces
136- if mtpnc .Status .PrimaryIP != "" {
137- req .SecondaryInterfacesExist = true
49+ var mtpnc v1alpha1.MultitenantPodNetworkConfig
50+ // if swiftv2 is enabled, get mtpnc
51+ if isSwiftv2 {
52+ mtpnc , respCode , message = k .getMTPNC (ctx , podInfo )
53+ if respCode != types .Success {
54+ return nil , respCode , message
13855 }
139- interfaceInfos := mtpnc .Status .InterfaceInfos
140- for _ , interfaceInfo := range interfaceInfos {
141- if interfaceInfo .DeviceType == v1alpha1 .DeviceTypeInfiniBandNIC {
142- if interfaceInfo .MacAddress == "" || interfaceInfo .NCID == "" {
143- return nil , types .UnexpectedError , errMTPNCNotReady .Error ()
144- }
145- req .BackendInterfaceExist = true
146- req .BackendInterfaceMacAddresses = append (req .BackendInterfaceMacAddresses , interfaceInfo .MacAddress )
14756
148- }
149- if interfaceInfo . DeviceType == v1alpha1 . DeviceTypeVnetNIC {
150- req . SecondaryInterfacesExist = true
151- }
57+ // update ipConfigRequest
58+ respCode , message = k . UpdateIPConfigRequest ( mtpnc , req )
59+ if respCode != types . Success {
60+ return nil , respCode , message
15261 }
15362 }
15463 logger .Printf ("[SWIFTv2Middleware] pod %s has secondary interface : %v" , podInfo .Name (), req .SecondaryInterfacesExist )
15564 logger .Printf ("[SWIFTv2Middleware] pod %s has backend interface : %v" , podInfo .Name (), req .BackendInterfaceExist )
156- // retrieve podinfo from orchestrator context
65+
15766 return podInfo , types .Success , ""
15867}
15968
@@ -249,3 +158,71 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
249158func (k * K8sSWIFTv2Middleware ) Type () cns.SWIFTV2Mode {
250159 return cns .K8sSWIFTV2
251160}
161+
162+ // gets Pod Data
163+ func (k * K8sSWIFTv2Middleware ) GetPodInfo (ctx context.Context , req * cns.IPConfigsRequest ) (podInfo cns.PodInfo , k8sPod v1.Pod , respCode types.ResponseCode , message string ) {
164+ // Retrieve the pod from the cluster
165+ podInfo , err := cns .UnmarshalPodInfo (req .OrchestratorContext )
166+ if err != nil {
167+ errBuf := errors .Wrapf (err , "failed to unmarshalling pod info from ipconfigs request %+v" , req )
168+ return nil , v1.Pod {}, types .UnexpectedError , errBuf .Error ()
169+ }
170+ logger .Printf ("[SWIFTv2Middleware] validate ipconfigs request for pod %s" , podInfo .Name ())
171+ podNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
172+ pod := v1.Pod {}
173+ if err := k .Cli .Get (ctx , podNamespacedName , & pod ); err != nil {
174+ errBuf := errors .Wrapf (err , "failed to get pod %+v" , podNamespacedName )
175+ return nil , v1.Pod {}, types .UnexpectedError , errBuf .Error ()
176+ }
177+ return podInfo , pod , types .Success , ""
178+ }
179+
180+ // validates if pod is multitenant by checking the pod labels, used in SWIFT V2 AKS scenario.
181+ func ValidateSwiftv2Pod (pod v1.Pod ) bool {
182+ // check the pod labels for Swift V2
183+ _ , swiftV2PodNetworkLabel := pod .Labels [configuration .LabelPodSwiftV2 ]
184+ _ , swiftV2PodNetworkInstanceLabel := pod .Labels [configuration .LabelPodNetworkInstanceSwiftV2 ]
185+ return swiftV2PodNetworkLabel || swiftV2PodNetworkInstanceLabel
186+ }
187+
188+ func (k * K8sSWIFTv2Middleware ) getMTPNC (ctx context.Context , podInfo cns.PodInfo ) (mtpncResource v1alpha1.MultitenantPodNetworkConfig , respCode types.ResponseCode , message string ) {
189+ // Check if the MTPNC CRD exists for the pod, if not, return error
190+ mtpnc := v1alpha1.MultitenantPodNetworkConfig {}
191+ mtpncNamespacedName := k8stypes.NamespacedName {Namespace : podInfo .Namespace (), Name : podInfo .Name ()}
192+ if err := k .Cli .Get (ctx , mtpncNamespacedName , & mtpnc ); err != nil {
193+ return v1alpha1.MultitenantPodNetworkConfig {}, types .UnexpectedError , fmt .Errorf ("failed to get pod's mtpnc from cache : %w" , err ).Error ()
194+ }
195+ // Check if the MTPNC CRD is ready. If one of the fields is empty, return error
196+ if ! mtpnc .IsReady () {
197+ return v1alpha1.MultitenantPodNetworkConfig {}, types .UnexpectedError , errMTPNCNotReady .Error ()
198+ }
199+ return mtpnc , types .Success , ""
200+ }
201+
202+ // Updates Ip Config Request
203+ func (k * K8sSWIFTv2Middleware ) UpdateIPConfigRequest (mtpnc v1alpha1.MultitenantPodNetworkConfig , req * cns.IPConfigsRequest ) (
204+ respCode types.ResponseCode ,
205+ message string ,
206+ ) {
207+ // If primary Ip is set in status field, it indicates the presence of secondary interfaces
208+ if mtpnc .Status .PrimaryIP != "" {
209+ req .SecondaryInterfacesExist = true
210+ }
211+
212+ interfaceInfos := mtpnc .Status .InterfaceInfos
213+ for _ , interfaceInfo := range interfaceInfos {
214+ if interfaceInfo .DeviceType == v1alpha1 .DeviceTypeInfiniBandNIC {
215+ if interfaceInfo .MacAddress == "" || interfaceInfo .NCID == "" {
216+ return types .UnexpectedError , errMTPNCNotReady .Error ()
217+ }
218+ req .BackendInterfaceExist = true
219+ req .BackendInterfaceMacAddresses = append (req .BackendInterfaceMacAddresses , interfaceInfo .MacAddress )
220+
221+ }
222+ if interfaceInfo .DeviceType == v1alpha1 .DeviceTypeVnetNIC {
223+ req .SecondaryInterfacesExist = true
224+ }
225+ }
226+
227+ return types .Success , ""
228+ }
0 commit comments