diff --git a/pkg/k8s/K8sCommonService.go b/pkg/k8s/K8sCommonService.go index c38519b9522..f8c7e3f3721 100644 --- a/pkg/k8s/K8sCommonService.go +++ b/pkg/k8s/K8sCommonService.go @@ -345,9 +345,10 @@ func (impl *K8sCommonServiceImpl) GetCoreClientByClusterId(clusterId int) (*kube } func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceResponse, resourceTree map[string]interface{}) map[string]interface{} { - portsService := make([]int64, 0) - portsEndpoint := make([]int64, 0) - portEndpointSlice := make([]int64, 0) + servicePortMapping := make(map[string]interface{}) + endpointPortMapping := make(map[string]interface{}) + endpointSlicePortMapping := make(map[string]interface{}) + for _, portHolder := range resp { if portHolder.ManifestResponse == nil { continue @@ -357,6 +358,26 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("kind not found in resource tree, unable to extract port no") continue } + metadataResp, ok := portHolder.ManifestResponse.Manifest.Object[k8sCommonBean.K8sClusterResourceMetadataKey] + if !ok { + impl.logger.Warnw("metadata not found in resource tree, unable to extract port no") + continue + } + metadata, ok := metadataResp.(map[string]interface{}) + if !ok { + impl.logger.Warnw("metadata not found in resource tree, unable to extract port no") + continue + } + serviceNameResp, ok := metadata[k8sCommonBean.K8sClusterResourceMetadataNameKey] + if !ok { + impl.logger.Warnw("service name not found in resource tree, unable to extract port no") + continue + } + serviceName, ok := serviceNameResp.(string) + if !ok { + impl.logger.Warnw("service name not found in resource tree, unable to extract port no") + continue + } if kind == k8sCommonBean.ServiceKind { specField, ok := portHolder.ManifestResponse.Manifest.Object[k8sCommonBean.Spec] if !ok { @@ -368,6 +389,7 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("spec not found in resource tree, unable to extract port no") continue } + if spec != nil { ports, ok := spec[k8sCommonBean.Ports] if !ok { @@ -379,6 +401,7 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("portList not found in resource tree, unable to extract port no") continue } + servicePorts := make([]int64, 0) for _, portItem := range portList { portItems, ok := portItem.(map[string]interface{}) if !ok { @@ -397,10 +420,11 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon continue } if portNumber != 0 { - portsService = append(portsService, portNumber) + servicePorts = append(servicePorts, portNumber) } } } + servicePortMapping[serviceName] = servicePorts } else { impl.logger.Warnw("spec doest not contain data", "spec", spec) continue @@ -435,6 +459,7 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("portsIfs not found in resource tree, unable to extract port no") continue } + endpointPorts := make([]int64, 0) for _, portsIf := range portsIfs { portsIfObj, ok := portsIf.(map[string]interface{}) if !ok { @@ -447,9 +472,10 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("port not found in resource tree, unable to extract port no") continue } - portsEndpoint = append(portsEndpoint, port) + endpointPorts = append(endpointPorts, port) } } + endpointPortMapping[serviceName] = endpointPorts } } } @@ -466,6 +492,7 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("endPointsSlicePorts not found in resource tree endpoint, unable to extract port no") continue } + endpointSlicePorts := make([]int64, 0) for _, val := range endPointsSlicePorts { portNumbers, ok := val.(map[string]interface{})[k8sCommonBean.Port] if !ok { @@ -478,9 +505,10 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon continue } if portNumber != 0 { - portEndpointSlice = append(portEndpointSlice, portNumber) + endpointSlicePorts = append(endpointSlicePorts, portNumber) } } + endpointSlicePortMapping[serviceName] = endpointSlicePorts } } } @@ -496,15 +524,25 @@ func (impl K8sCommonServiceImpl) PortNumberExtraction(resp []BatchResourceRespon impl.logger.Warnw("value not found in resourceTreeVal, unable to extract port no") continue } + serviceNameRes, ok := value[k8sCommonBean.K8sClusterResourceMetadataNameKey] + if !ok { + impl.logger.Warnw("service name not found in resourceTreeVal, unable to extract port no") + continue + } + serviceName, ok := serviceNameRes.(string) + if !ok { + impl.logger.Warnw("service name not found in resourceTreeVal, unable to extract port no") + continue + } for key, _type := range value { if key == k8sCommonBean.Kind && _type == k8sCommonBean.EndpointsKind { - value[k8sCommonBean.Port] = portsEndpoint + value[k8sCommonBean.Port] = endpointPortMapping[serviceName] } if key == k8sCommonBean.Kind && _type == k8sCommonBean.ServiceKind { - value[k8sCommonBean.Port] = portsService + value[k8sCommonBean.Port] = servicePortMapping[serviceName] } if key == k8sCommonBean.Kind && _type == k8sCommonBean.EndPointsSlice { - value[k8sCommonBean.Port] = portEndpointSlice + value[k8sCommonBean.Port] = endpointSlicePortMapping[serviceName] } } }