@@ -1040,17 +1040,81 @@ func (impl *K8sApplicationServiceImpl) TerminatePodEphemeralContainer(req bean5.
10401040	if  container  ==  nil  {
10411041		return  false , errors .New ("externally created ephemeral containers cannot be removed" )
10421042	}
1043+ 	// Check if pod exists and is running before attempting to execute command 
1044+ 	var  v1Client  * v1.CoreV1Client 
1045+ 	if  req .ExternalArgoAppIdentifier  !=  nil  {
1046+ 		_ , v1Client , err  =  impl .k8sCommonService .GetCoreClientByClusterIdForExternalArgoApps (& req )
1047+ 		if  err  !=  nil  {
1048+ 			impl .logger .Errorw ("error in getting coreV1 client by clusterId for external argo apps" , "err" , err , "req" , req )
1049+ 			return  false , err 
1050+ 		}
1051+ 	} else  {
1052+ 		_ , v1Client , err  =  impl .k8sCommonService .GetCoreClientByClusterId (req .ClusterId )
1053+ 		if  err  !=  nil  {
1054+ 			impl .logger .Errorw ("error in getting coreV1 client by clusterId" , "clusterId" , req .ClusterId , "err" , err )
1055+ 			return  false , err 
1056+ 		}
1057+ 	}
1058+ 
1059+ 	// Check if pod exists and is running 
1060+ 	pod , err  :=  impl .K8sUtil .GetPodByName (req .Namespace , req .PodName , v1Client )
1061+ 	if  err  !=  nil  {
1062+ 		if  k8s .IsResourceNotFoundErr (err ) {
1063+ 			impl .logger .Infow ("pod not found, ephemeral container termination not needed" , "podName" , req .PodName , "namespace" , req .Namespace , "clusterId" , req .ClusterId )
1064+ 			// Pod doesn't exist, so ephemeral container is already terminated 
1065+ 			err  =  impl .ephemeralContainerService .AuditEphemeralContainerAction (req , repository .ActionTerminate )
1066+ 			if  err  !=  nil  {
1067+ 				impl .logger .Errorw ("error in saving ephemeral container data" , "err" , err )
1068+ 				return  true , err 
1069+ 			}
1070+ 			return  true , nil 
1071+ 		}
1072+ 		impl .logger .Errorw ("error in getting pod" , "clusterId" , req .ClusterId , "namespace" , req .Namespace , "podName" , req .PodName , "err" , err )
1073+ 		return  false , err 
1074+ 	}
1075+ 
1076+ 	// Check if pod is in a terminated state 
1077+ 	if  pod .Status .Phase  ==  corev1 .PodSucceeded  ||  pod .Status .Phase  ==  corev1 .PodFailed  {
1078+ 		impl .logger .Infow ("pod is in terminated state, ephemeral container termination not needed" , "podName" , req .PodName , "namespace" , req .Namespace , "clusterId" , req .ClusterId , "podPhase" , pod .Status .Phase )
1079+ 		// Pod is terminated, so ephemeral container is already terminated 
1080+ 		err  =  impl .ephemeralContainerService .AuditEphemeralContainerAction (req , repository .ActionTerminate )
1081+ 		if  err  !=  nil  {
1082+ 			impl .logger .Errorw ("error in saving ephemeral container data" , "err" , err )
1083+ 			return  true , err 
1084+ 		}
1085+ 		return  true , nil 
1086+ 	}
1087+ 
1088+ 	// Check if the specific ephemeral container is still running 
1089+ 	ephemeralContainerRunning  :=  false 
1090+ 	for  _ , ecStatus  :=  range  pod .Status .EphemeralContainerStatuses  {
1091+ 		if  ecStatus .Name  ==  req .BasicData .ContainerName  &&  ecStatus .State .Running  !=  nil  {
1092+ 			ephemeralContainerRunning  =  true 
1093+ 			break 
1094+ 		}
1095+ 	}
1096+ 
1097+ 	if  ! ephemeralContainerRunning  {
1098+ 		impl .logger .Infow ("ephemeral container is not running, termination not needed" , "podName" , req .PodName , "namespace" , req .Namespace , "clusterId" , req .ClusterId , "containerName" , req .BasicData .ContainerName )
1099+ 		// Ephemeral container is not running, so it's already terminated 
1100+ 		err  =  impl .ephemeralContainerService .AuditEphemeralContainerAction (req , repository .ActionTerminate )
1101+ 		if  err  !=  nil  {
1102+ 			impl .logger .Errorw ("error in saving ephemeral container data" , "err" , err )
1103+ 			return  true , err 
1104+ 		}
1105+ 		return  true , nil 
1106+ 	}
10431107	containerKillCommand  :=  fmt .Sprintf ("kill -16 $(pgrep -f '%s' -o)" , fmt .Sprintf (k8sObjectUtils .EphemeralContainerStartingShellScriptFileName , terminalReq .ContainerName ))
10441108	cmds  :=  []string {"sh" , "-c" , containerKillCommand }
10451109	_ , errBuf , err  :=  impl .terminalSession .RunCmdInRemotePod (terminalReq , cmds )
10461110	if  err  !=  nil  {
10471111		impl .logger .Errorw ("failed to execute commands " , "err" , err , "commands" , cmds , "podName" , req .PodName , "namespace" , req .Namespace )
1048- 		return   false ,  err 
1112+ 		// not returning the error since websocket connection will break when running kill command 
10491113	}
10501114	errBufString  :=  errBuf .String ()
10511115	if  errBufString  !=  ""  {
10521116		impl .logger .Errorw ("error response on executing commands " , "err" , errBufString , "commands" , cmds , "podName" , req .Namespace , "namespace" , req .Namespace )
1053- 		return   false ,  err 
1117+ 		// not returning the error since websocket connection will break when running kill command 
10541118	}
10551119
10561120	if  err  ==  nil  {
0 commit comments