Skip to content

Commit 92f9a47

Browse files
committed
cleanup of ephemeral container
1 parent 053abc9 commit 92f9a47

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

docker/entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ case "$ROLE" in
3636
echo "$SSH_PUBLIC_KEY" > /home/ve/.ssh/authorized_keys
3737
chmod 600 /home/ve/.ssh/id_rsa
3838
/usr/sbin/sshd -e -p $SSH_PORT
39-
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -N -R 2137:localhost:2137 ve@${PROXY_POD_IP} -p 6666
39+
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -N -R 2137:localhost:2137 ve@${PROXY_POD_IP} -p 6666 &
4040
tail -f /dev/null
4141
;;
4242
*)

pkg/plugin/clean.go

+28-19
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package plugin
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"os"
87
"os/exec"
98
"runtime"
109

1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"k8s.io/apimachinery/pkg/types"
1311
"k8s.io/client-go/kubernetes"
1412
)
1513

@@ -60,37 +58,48 @@ func Clean(namespace, pvcName, localMountPoint string) error {
6058
}
6159
fmt.Printf("Port-forward process for pod %s killed successfully\n", podName)
6260

63-
// Remove the ephemeral container
64-
err = removeEphemeralContainer(clientset, namespace, podName)
65-
if err != nil {
66-
return fmt.Errorf("failed to remove ephemeral container: %v", err)
61+
// Check for original pod
62+
originalPodName := podList.Items[0].Labels["originalPodName"]
63+
if originalPodName != "" {
64+
err = killProcessInEphemeralContainer(clientset, namespace, originalPodName)
65+
if err != nil {
66+
return fmt.Errorf("failed to kill process in ephemeral container: %v", err)
67+
}
68+
fmt.Printf("Process in ephemeral container killed successfully in pod %s\n", originalPodName)
6769
}
68-
fmt.Printf("Ephemeral container in pod %s removed successfully\n", podName)
6970

70-
// Delete the pod
71+
// Delete the proxy pod
7172
err = podClient.Delete(ctx, podName, metav1.DeleteOptions{})
7273
if err != nil {
7374
return fmt.Errorf("failed to delete pod: %v", err)
7475
}
75-
fmt.Printf("Pod %s deleted successfully\n", podName)
76+
fmt.Printf("Proxy pod %s deleted successfully\n", podName)
7677

7778
return nil
7879
}
7980

80-
func removeEphemeralContainer(clientset *kubernetes.Clientset, namespace, podName string) error {
81-
patchData, err := json.Marshal(map[string]interface{}{
82-
"spec": map[string]interface{}{
83-
"ephemeralContainers": []interface{}{},
84-
},
85-
})
81+
func killProcessInEphemeralContainer(clientset *kubernetes.Clientset, namespace, podName string) error {
82+
// Retrieve the existing pod to get the ephemeral container name
83+
existingPod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
8684
if err != nil {
87-
return fmt.Errorf("failed to marshal patch data: %v", err)
85+
return fmt.Errorf("failed to get existing pod: %v", err)
8886
}
8987

90-
_, err = clientset.CoreV1().Pods(namespace).Patch(context.TODO(), podName, types.StrategicMergePatchType, patchData, metav1.PatchOptions{}, "ephemeralcontainers")
91-
if err != nil {
92-
return fmt.Errorf("failed to patch pod to remove ephemeral container: %v", err)
88+
if len(existingPod.Spec.EphemeralContainers) == 0 {
89+
return fmt.Errorf("no ephemeral containers found in pod %s", podName)
9390
}
9491

92+
ephemeralContainerName := existingPod.Spec.EphemeralContainers[0].Name
93+
fmt.Printf("Ephemeral container name is %s\n", ephemeralContainerName)
94+
95+
// Command to kill the process (adjust the process name or ID as necessary)
96+
killCmd := []string{"pkill", "-f", "tail"} // Replace "process_name" with the actual process name or use a specific PID
97+
98+
cmd := exec.Command("kubectl", append([]string{"exec", podName, "-n", namespace, "-c", ephemeralContainerName, "--"}, killCmd...)...)
99+
cmd.Stdout = os.Stdout
100+
cmd.Stderr = os.Stderr
101+
if err := cmd.Run(); err != nil {
102+
return fmt.Errorf("failed to kill process in container %s of pod %s: %v", ephemeralContainerName, podName, err)
103+
}
95104
return nil
96105
}

pkg/plugin/mount.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func handleMount(clientset *kubernetes.Clientset, namespace, pvcName, localMount
6060
return err
6161
}
6262

63-
podName, port, err := setupPod(clientset, namespace, pvcName, sshKey, "standalone", 2137)
63+
podName, port, err := setupPod(clientset, namespace, pvcName, sshKey, "standalone", 2137, "")
6464
if err != nil {
6565
return err
6666
}
@@ -89,7 +89,7 @@ func handleRWOConflict(clientset *kubernetes.Clientset, namespace, pvcName, loca
8989
return err
9090
}
9191

92-
podName, port, err := setupPod(clientset, namespace, pvcName, publicKey, "proxy", 6666)
92+
podName, port, err := setupPod(clientset, namespace, pvcName, publicKey, "proxy", 6666, podUsingPVC)
9393
if err != nil {
9494
return err
9595
}
@@ -263,9 +263,9 @@ func checkPVCUsage(clientset *kubernetes.Clientset, namespace, pvcName string) (
263263
return pvc, nil
264264
}
265265

266-
func setupPod(clientset *kubernetes.Clientset, namespace, pvcName, sshKey, role string, sshPort int) (string, int, error) {
266+
func setupPod(clientset *kubernetes.Clientset, namespace, pvcName, sshKey, role string, sshPort int, originalPodName string) (string, int, error) {
267267
podName, port := generatePodNameAndPort(pvcName, role)
268-
pod := createPodSpec(podName, port, pvcName, sshKey, role, sshPort)
268+
pod := createPodSpec(podName, port, pvcName, sshKey, role, sshPort, originalPodName)
269269
if _, err := clientset.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{}); err != nil {
270270
return "", 0, fmt.Errorf("failed to create pod: %v", err)
271271
}
@@ -322,7 +322,7 @@ func generatePodNameAndPort(pvcName, role string) (string, int) {
322322
return podName, port
323323
}
324324

325-
func createPodSpec(podName string, port int, pvcName, sshKey, role string, sshPort int) *corev1.Pod {
325+
func createPodSpec(podName string, port int, pvcName, sshKey, role string, sshPort int, originalPodName string) *corev1.Pod {
326326
envVars := []corev1.EnvVar{
327327
{
328328
Name: "SSH_KEY",
@@ -366,14 +366,21 @@ func createPodSpec(podName string, port int, pvcName, sshKey, role string, sshPo
366366
},
367367
}
368368

369+
labels := map[string]string{
370+
"app": "volume-exposer",
371+
"pvcName": pvcName,
372+
"portNumber": fmt.Sprintf("%d", port),
373+
}
374+
375+
// Add the original pod name label if provided
376+
if originalPodName != "" {
377+
labels["originalPodName"] = originalPodName
378+
}
379+
369380
podSpec := &corev1.Pod{
370381
ObjectMeta: metav1.ObjectMeta{
371-
Name: podName,
372-
Labels: map[string]string{
373-
"app": "volume-exposer",
374-
"pvcName": pvcName,
375-
"portNumber": fmt.Sprintf("%d", port),
376-
},
382+
Name: podName,
383+
Labels: labels,
377384
},
378385
Spec: corev1.PodSpec{
379386
Containers: []corev1.Container{container},

0 commit comments

Comments
 (0)