Skip to content

Commit

Permalink
Added helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bohuini committed Apr 12, 2024
1 parent d1116dc commit e621ed4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 59 deletions.
22 changes: 11 additions & 11 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ var _ cns.IPConfigsHandlerMiddleware = (*K8sSWIFTv2Middleware)(nil)

// IPConfigsRequestHandlerWrapper is the middleware function for handling SWIFT v2 IP configs requests for AKS-SWIFT. This function wrapped the default SWIFT request
// and release IP configs handlers.
func (m *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, failureHandler cns.IPConfigsHandlerFunc) cns.IPConfigsHandlerFunc {
func (k *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, failureHandler cns.IPConfigsHandlerFunc) cns.IPConfigsHandlerFunc {
return func(ctx context.Context, req cns.IPConfigsRequest) (*cns.IPConfigsResponse, error) {
podInfo, respCode, message := m.validateIPConfigsRequest(ctx, &req)
podInfo, respCode, message := k.validateIPConfigsRequest(ctx, &req)

if respCode != types.Success {
return &cns.IPConfigsResponse{
Expand Down Expand Up @@ -68,7 +68,7 @@ func (m *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
if err != nil {
return ipConfigsResp, err
}
SWIFTv2PodIPInfo, err := m.getIPConfig(ctx, podInfo)
SWIFTv2PodIPInfo, err := k.getIPConfig(ctx, podInfo)
if err != nil {
return &cns.IPConfigsResponse{
Response: cns.Response{
Expand All @@ -82,7 +82,7 @@ func (m *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa
// Set routes for the pod
for i := range ipConfigsResp.PodIPInfo {
ipInfo := &ipConfigsResp.PodIPInfo[i]
err = m.setRoutes(ipInfo)
err = k.setRoutes(ipInfo)
if err != nil {
return &cns.IPConfigsResponse{
Response: cns.Response{
Expand All @@ -99,7 +99,7 @@ func (m *K8sSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(defaultHandler, fa

// validateIPConfigsRequest validates if pod is multitenant by checking the pod labels, used in SWIFT V2 AKS scenario.
// nolint
func (m *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req *cns.IPConfigsRequest) (podInfo cns.PodInfo, respCode types.ResponseCode, message string) {
func (k *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req *cns.IPConfigsRequest) (podInfo cns.PodInfo, respCode types.ResponseCode, message string) {
// Retrieve the pod from the cluster
podInfo, err := cns.UnmarshalPodInfo(req.OrchestratorContext)
if err != nil {
Expand All @@ -109,7 +109,7 @@ func (m *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
logger.Printf("[SWIFTv2Middleware] validate ipconfigs request for pod %s", podInfo.Name())
podNamespacedName := k8stypes.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
pod := v1.Pod{}
if err := m.Cli.Get(ctx, podNamespacedName, &pod); err != nil {
if err := k.Cli.Get(ctx, podNamespacedName, &pod); err != nil {
errBuf := errors.Wrapf(err, "failed to get pod %+v", podNamespacedName)
return nil, types.UnexpectedError, errBuf.Error()
}
Expand All @@ -120,11 +120,11 @@ func (m *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
// Check if the MTPNC CRD exists for the pod, if not, return error
mtpnc := v1alpha1.MultitenantPodNetworkConfig{}
mtpncNamespacedName := k8stypes.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
if err := m.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
return nil, types.UnexpectedError, fmt.Errorf("failed to get pod's mtpnc from cache : %w", err).Error()
}
// Check if the MTPNC CRD is ready. If one of the fields is empty, return error
if mtpnc.Status.PrimaryIP == "" || mtpnc.Status.MacAddress == "" || mtpnc.Status.NCID == "" || mtpnc.Status.GatewayIP == "" || mtpnc.Status.NICType == "" {
if !mtpnc.IsReady() {
return nil, types.UnexpectedError, errMTPNCNotReady.Error()
}
}
Expand All @@ -134,16 +134,16 @@ func (m *K8sSWIFTv2Middleware) validateIPConfigsRequest(ctx context.Context, req
}

// getIPConfig returns the pod's SWIFT V2 IP configuration.
func (m *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodInfo) (cns.PodIpInfo, error) {
func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodInfo) (cns.PodIpInfo, error) {
// Check if the MTPNC CRD exists for the pod, if not, return error
mtpnc := v1alpha1.MultitenantPodNetworkConfig{}
mtpncNamespacedName := k8stypes.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
if err := m.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
return cns.PodIpInfo{}, errors.Wrapf(err, "failed to get pod's mtpnc from cache")
}

// Check if the MTPNC CRD is ready. If one of the fields is empty, return error
if mtpnc.Status.PrimaryIP == "" || mtpnc.Status.MacAddress == "" || mtpnc.Status.NCID == "" || mtpnc.Status.GatewayIP == "" || mtpnc.Status.NICType == "" {
if !mtpnc.IsReady() {
return cns.PodIpInfo{}, errMTPNCNotReady
}
logger.Printf("[SWIFTv2Middleware] mtpnc for pod %s is : %+v", podInfo.Name(), mtpnc)
Expand Down
62 changes: 17 additions & 45 deletions cns/middlewares/k8sSwiftV2_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build linux

package middlewares

import (
Expand All @@ -14,7 +12,7 @@ import (
)

// setRoutes sets the routes for podIPInfo used in SWIFT V2 scenario.
func (m *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
logger.Printf("[SWIFTv2Middleware] set routes for pod with nic type : %s", podIPInfo.NICType)
podIPInfo.Routes = []cns.Route{}
switch podIPInfo.NICType {
Expand Down Expand Up @@ -65,54 +63,18 @@ func (m *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
}
if ip.Is4() {
// routes for IPv4 podCIDR traffic
for _, podCIDRv4 := range podCIDRsV4 {
podCIDRv4Route := cns.Route{
IPAddress: podCIDRv4,
GatewayIPAddress: overlayGatewayv4,
}
podIPInfo.Routes = append(podIPInfo.Routes, podCIDRv4Route)
}
addRoutes(&podIPInfo.Routes, podCIDRsV4, overlayGatewayv4)
// route for IPv4 serviceCIDR traffic
for _, serviceCIDRv4 := range serviceCIDRsV4 {
serviceCIDRv4Route := cns.Route{
IPAddress: serviceCIDRv4,
GatewayIPAddress: overlayGatewayv4,
}
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv4Route)
}
addRoutes(&podIPInfo.Routes, serviceCIDRsV4, overlayGatewayv4)
// route for IPv4 infraVNETCIDR traffic
for _, infraVNETCIDRv4 := range infraVNETCIDRsv4 {
infraVNETCIDRv4Route := cns.Route{
IPAddress: infraVNETCIDRv4,
GatewayIPAddress: overlayGatewayv4,
}
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRv4Route)
}
addRoutes(&podIPInfo.Routes, infraVNETCIDRsv4, overlayGatewayv4)
} else {
// routes for IPv6 podCIDR traffic
for _, podCIDRv6 := range podCIDRv6 {
podCIDRv6Route := cns.Route{
IPAddress: podCIDRv6,
GatewayIPAddress: overlayGatewayV6,
}
podIPInfo.Routes = append(podIPInfo.Routes, podCIDRv6Route)
}
addRoutes(&podIPInfo.Routes, podCIDRv6, overlayGatewayV6)
// route for IPv6 serviceCIDR traffic
for _, serviceCIDRv6 := range serviceCIDRsV6 {
serviceCIDRv6Route := cns.Route{
IPAddress: serviceCIDRv6,
GatewayIPAddress: overlayGatewayV6,
}
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv6Route)
}
addRoutes(&podIPInfo.Routes, serviceCIDRsV6, overlayGatewayV6)
// route for IPv6 infraVNETCIDR traffic
for _, infraVNETCIDRv6 := range infraVNETCIDRsv6 {
infraVNETCIDRv6Route := cns.Route{
IPAddress: infraVNETCIDRv6,
GatewayIPAddress: overlayGatewayV6,
}
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRv6Route)
}
addRoutes(&podIPInfo.Routes, infraVNETCIDRsv6, overlayGatewayV6)
}
podIPInfo.SkipDefaultRoutes = true
case cns.BackendNIC:
Expand All @@ -122,3 +84,13 @@ func (m *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
}
return nil
}

func addRoutes(routes *[]cns.Route, CIDRs []string, gatewayIP string) {

Check failure on line 88 in cns/middlewares/k8sSwiftV2_linux.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x, ubuntu-latest)

captLocal: `CIDRs' should not be capitalized (gocritic)
for _, cidr := range CIDRs {
route := cns.Route{
IPAddress: cidr,
GatewayIPAddress: gatewayIP,
}
*routes = append(*routes, route)
}
}
4 changes: 1 addition & 3 deletions cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build windows

package middlewares

import (
Expand All @@ -8,7 +6,7 @@ import (
)

// setRoutes sets the routes for podIPInfo used in SWIFT V2 scenario. This is a no-op as route setting is not applicable for Windows.
func (m *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
func (k *K8sSWIFTv2Middleware) setRoutes(_ *cns.PodIpInfo) error {
logger.Printf("[SWIFTv2Middleware] setRoutes is a no-op on Windows")
return nil
}
6 changes: 6 additions & 0 deletions crd/multitenancy/api/v1alpha1/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v1alpha1

// IsReady checks if all the required fields in the MTPNC status are populated
func (m *MultitenantPodNetworkConfig) IsReady() bool {
return m.Status.PrimaryIP != "" && m.Status.MacAddress != "" && m.Status.NCID != "" && m.Status.GatewayIP != "" && m.Status.NICType != ""
}

0 comments on commit e621ed4

Please sign in to comment.