cli: handle panic caused by `linkerd metrics` port-forward failure (#4007)

* cli: handle `linkerd metrics` port-forward gracefully

- add return for routine in func `Init()` in case of error
- add return from func `getMetrics()` if error from `portforward.Init()`

* Remove select block at pkg/k8s/portforward.go

- It is now the caller's responsibility to call pf.Stop()

Signed-off-by: Mayank Shah <mayankshah1614@gmail.com>
This commit is contained in:
Mayank Shah 2020-02-20 11:14:37 +05:30 committed by GitHub
parent df2011dbb2
commit 7cff974a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 8 deletions

View File

@ -165,6 +165,7 @@ func getMetrics(
defer portforward.Stop()
if err = portforward.Init(); err != nil {
fmt.Fprintf(os.Stderr, "Error running port-forward: %s", err)
return nil, err
}
metricsURL := portforward.URLFor("/metrics")

View File

@ -177,14 +177,7 @@ func (pf *PortForward) Init() error {
go func() {
if err := pf.run(); err != nil {
failure <- err
}
select {
case <-pf.GetStop():
// stopCh was closed, do nothing
default:
// pf.run() returned for some other reason, close stopCh
pf.Stop()
return
}
}()
@ -203,6 +196,7 @@ func (pf *PortForward) Init() error {
}
// Stop terminates the port-forward connection.
// It is the caller's responsibility to call Stop even in case of errors
func (pf *PortForward) Stop() {
close(pf.stopCh)
}