Skip to content

Commit

Permalink
fix: port service mapping (#4132)
Browse files Browse the repository at this point in the history
* wip

* port list fixed

* replaced strings with const

* constant changed
  • Loading branch information
ashishdevtron committed Oct 20, 2023
1 parent 6557737 commit 50ac85e
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions pkg/k8s/K8sCommonService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
}
Expand All @@ -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 {
Expand All @@ -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
}
}
}
Expand All @@ -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]
}
}
}
Expand Down

0 comments on commit 50ac85e

Please sign in to comment.