expose ready/stop channel

This commit is contained in:
Dominika Hodovska
2016-08-08 14:31:15 +02:00
parent ae584d8114
commit c5babe2396
4 changed files with 29 additions and 18 deletions

View File

@@ -41,6 +41,8 @@ type PortForwardOptions struct {
Client *client.Client
Ports []string
PortForwarder portForwarder
StopChannel chan struct{}
ReadyChannel chan struct{}
}
var (
@@ -88,19 +90,19 @@ func NewCmdPortForward(f *cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Comm
}
type portForwarder interface {
ForwardPorts(method string, url *url.URL, config *restclient.Config, ports []string, stopChan <-chan struct{}) error
ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
}
type defaultPortForwarder struct {
cmdOut, cmdErr io.Writer
}
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, config *restclient.Config, ports []string, stopChan <-chan struct{}) error {
dialer, err := remotecommand.NewExecutor(config, method, url)
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
dialer, err := remotecommand.NewExecutor(opts.Config, method, url)
if err != nil {
return err
}
fw, err := portforward.New(dialer, ports, stopChan, f.cmdOut, f.cmdErr)
fw, err := portforward.New(dialer, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.cmdOut, f.cmdErr)
if err != nil {
return err
}
@@ -138,6 +140,8 @@ func (o *PortForwardOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, ar
return err
}
o.StopChannel = make(chan struct{}, 1)
o.ReadyChannel = make(chan struct{})
return nil
}
@@ -165,17 +169,18 @@ func (o PortForwardOptions) RunPortForward() error {
}
if pod.Status.Phase != api.PodRunning {
return fmt.Errorf("Unable to execute command because pod is not running. Current status=%v", pod.Status.Phase)
return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)
stopCh := make(chan struct{}, 1)
go func() {
<-signals
close(stopCh)
if o.StopChannel != nil {
close(o.StopChannel)
}
}()
req := o.Client.RESTClient.Post().
@@ -184,5 +189,5 @@ func (o PortForwardOptions) RunPortForward() error {
Name(pod.Name).
SubResource("portforward")
return o.PortForwarder.ForwardPorts("POST", req.URL(), o.Config, o.Ports, stopCh)
return o.PortForwarder.ForwardPorts("POST", req.URL(), o)
}