Skip to content

Commit

Permalink
FIX:issue57, if target pod is not ready or completed/failed, delete t…
Browse files Browse the repository at this point in the history
…he agent pod (#63)
  • Loading branch information
calmkart authored and aylei committed Sep 26, 2019
1 parent 6a30d78 commit 64720bf
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/plugin/cmd.go
Expand Up @@ -361,11 +361,13 @@ func (o *DebugOptions) Run() error {
}

if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
o.deleteAgent(agentPod)
return fmt.Errorf("cannot debug in a completed pod; current phase is %s", pod.Status.Phase)
}

containerID, err := o.getContainerIDByName(pod, containerName)
if err != nil {
o.deleteAgent(agentPod)
return err
}

Expand Down Expand Up @@ -410,6 +412,7 @@ func (o *DebugOptions) Run() error {
fmt.Fprintf(o.Out, "pod %s PodIP %s, agentPodIP %s\n", o.PodName, pod.Status.PodIP, agent.Status.HostIP)
err = o.runPortForward(agent)
if err != nil {
o.deleteAgent(agentPod)
return err
}
// client can't access the node ip in the k8s cluster sometimes,
Expand Down Expand Up @@ -466,11 +469,7 @@ func (o *DebugOptions) Run() error {
// delete agent pod
if o.AgentLess && agentPod != nil {
fmt.Fprintf(o.Out, "Start deleting agent pod %s \n\r", pod.Name)
err := o.CoreClient.Pods(agentPod.Namespace).Delete(agentPod.Name, v1.NewDeleteOptions(0))
if err != nil {
// we may leak pod here, but we have nothing to do except noticing the user
fmt.Fprintf(o.ErrOut, "failed to delete agent pod[Name:%s, Namespace: %s], consider manual deletion.\n", agentPod.Name, agentPod.Namespace)
}
o.deleteAgent(agentPod)
}
}).Run(fn)
}
Expand Down Expand Up @@ -740,3 +739,11 @@ func (o *DebugOptions) auth(pod *corev1.Pod) error {
}
return nil
}

// delete the agent pod
func (o *DebugOptions) deleteAgent(agentPod *corev1.Pod) {
err := o.CoreClient.Pods(agentPod.Namespace).Delete(agentPod.Name, v1.NewDeleteOptions(0))
if err != nil {
fmt.Fprintf(o.ErrOut, "failed to delete agent pod[Name:%s, Namespace: %s], consider manual deletion.\n", agentPod.Name, agentPod.Namespace)
}
}

0 comments on commit 64720bf

Please sign in to comment.