Add option to run only VNC Proxy in virtctl
package vnc
import (
+ "encoding/json"
"fmt"
"io"
"net"
@@ -39,7 +40,7 @@ import (
)
const (
- LISTEN_TIMEOUT = 60 * time.Second
+ LISTEN_TIMEOUT = 180 * time.Second
FLAG = "vnc"
//#### Tiger VNC ####
@@ -59,6 +60,8 @@ const (
TIGER_VNC = "vncviewer"
)
+var proxyOnly bool
+
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "vnc (VMI)",
@@ -70,6 +73,7 @@ func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
return c.Run(cmd, args)
},
}
+ cmd.Flags().BoolVar(&proxyOnly, "proxy-only", proxyOnly, "--proxy-only=false: Setting this true will run only the virtctl vnc proxy and show the localhost port where VNC viewers can connect")
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
@@ -165,10 +169,47 @@ func (o *VNC) Run(cmd *cobra.Command, args []string) error {
listenResChan <- err
}()
- // execute VNC
+ port := ln.Addr().(*net.TCPAddr).Port
+
+ if proxyOnly {
+ defer close(doneChan)
+ optionString, err := json.Marshal(struct {
+ Port int `json:"port"`
+ }{port})
+ if err != nil {
+ return fmt.Errorf("Error encountered: %s", err.Error())
+ fmt.Println(string(optionString))
+ } else {
+ // execute VNC Viewer
+ go checkAndRunVNCViewer(doneChan, viewResChan, port)
+ }
+ }
+
go func() {
+ defer close(stopChan)
+ interrupt := make(chan os.Signal, 1)
+ signal.Notify(interrupt, os.Interrupt)
+ <-interrupt
+ }()
+
+ select {
+ case <-stopChan:
+ case err = <-readStop:
+ case err = <-writeStop:
+ case err = <-k8ResChan:
+ case err = <-viewResChan:
+ case err = <-listenResChan:
+ }
+
+ if err != nil {
+ return fmt.Errorf("Error encountered: %s", err.Error())
+ }
+ return nil
+}
+
+func checkAndRunVNCViewer(doneChan chan struct{}, viewResChan chan error, port int) {
defer close(doneChan)
- port := ln.Addr().(*net.TCPAddr).Port
+ var err error
args := []string{}
vncBin := ""
@@ -236,28 +277,6 @@ func (o *VNC) Run(cmd *cobra.Command, args []string) error {
}
}
viewResChan <- err
- }()
-
- go func() {
- defer close(stopChan)
- interrupt := make(chan os.Signal, 1)
- signal.Notify(interrupt, os.Interrupt)
- <-interrupt
- }()
-
- select {
- case <-stopChan:
- case err = <-readStop:
- case err = <-writeStop:
- case err = <-k8ResChan:
- case err = <-viewResChan:
- case err = <-listenResChan:
- }
-
- if err != nil {
- return fmt.Errorf("Error encountered: %s", err.Error())
- }
- return nil
}
func tigerVncArgs(port int) (args []string) {
