Skip to content

Commit

Permalink
Print errors encountered during Attach()
Browse files Browse the repository at this point in the history
  • Loading branch information
zqureshi authored and fntlnz committed Nov 18, 2020
1 parent bb25231 commit 0457640
Showing 1 changed file with 51 additions and 45 deletions.
96 changes: 51 additions & 45 deletions pkg/attacher/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,61 @@ func (a *Attacher) AttachJob(traceJobID types.UID, namespace string) {
}

func (a *Attacher) Attach(selector, namespace string) {
go wait.ExponentialBackoff(wait.Backoff{
Duration: time.Second * 1,
Factor: 0.01,
Jitter: 0.0,
Steps: 100,
}, func() (bool, error) {
pl, err := a.CoreV1Client.Pods(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: selector,
go func() {
err := wait.ExponentialBackoff(wait.Backoff{
Duration: time.Second * 1,
Factor: 0.01,
Jitter: 0.0,
Steps: 100,
}, func() (bool, error) {
pl, err := a.CoreV1Client.Pods(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: selector,
})

if err != nil {
return false, err
}

if len(pl.Items) == 0 {
return false, fmt.Errorf(podNotFoundError)
}
pod := &pl.Items[0]
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
return false, fmt.Errorf(podPhaseNotAcceptedError, pod.Status.Phase)
}

if len(pod.Spec.Containers) != 1 {
return false, fmt.Errorf(invalidPodContainersSizeError)
}

restClient := a.CoreV1Client.RESTClient().(*restclient.RESTClient)
containerName := pod.Spec.Containers[0].Name

t, err := setupTTY(a.IOStreams.Out, a.IOStreams.In)
if err != nil {
return false, err
}
ao := attach{
restClient: restClient,
podName: pod.Name,
namespace: pod.Namespace,
containerName: containerName,
config: a.Config,
tty: t,
}
err = t.Safe(ao.defaultAttachFunc())

if err != nil {
// on error, just send false so that the backoff mechanism can do a new tentative
return false, nil
}
return true, nil
})

if err != nil {
return false, err
fmt.Fprintln(a.IOStreams.ErrOut, err)
}

if len(pl.Items) == 0 {
return false, fmt.Errorf(podNotFoundError)
}
pod := &pl.Items[0]
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
return false, fmt.Errorf(podPhaseNotAcceptedError, pod.Status.Phase)
}

if len(pod.Spec.Containers) != 1 {
return false, fmt.Errorf(invalidPodContainersSizeError)
}

restClient := a.CoreV1Client.RESTClient().(*restclient.RESTClient)
containerName := pod.Spec.Containers[0].Name

t, err := setupTTY(a.IOStreams.Out, a.IOStreams.In)
if err != nil {
return false, err
}
ao := attach{
restClient: restClient,
podName: pod.Name,
namespace: pod.Namespace,
containerName: containerName,
config: a.Config,
tty: t,
}
err = t.Safe(ao.defaultAttachFunc())

if err != nil {
// on error, just send false so that the backoff mechanism can do a new tentative
return false, nil
}
return true, nil
})
}()
<-a.ctx.Done()
}

Expand Down

0 comments on commit 0457640

Please sign in to comment.