Skip to content

Commit

Permalink
Allow client side spice proxy override
Browse files Browse the repository at this point in the history
Add "--proxy" option to "virtctl spice" and make the proxy transport
protocol configurable.

Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Dec 4, 2017
1 parent b844354 commit 3975198
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifests/virt-api.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
- "--port"
- "8183"
- "--spice-proxy"
- "{{ master_ip }}:3128"
- "http://{{ master_ip }}:3128"
ports:
- containerPort: 8183
name: "virt-api"
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-api/rest/spice.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func spiceFromVM(vm *v1.VirtualMachine) (*v1.Spice, error) {
Port: d.Port,
}
if spiceProxy != "" {
spice.Info.Proxy = fmt.Sprintf("http://%s", spiceProxy)
spice.Info.Proxy = fmt.Sprintf("%s", spiceProxy)
}
return spice, nil
}
Expand Down
41 changes: 31 additions & 10 deletions pkg/virtctl/spice/spice.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
kubev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"

"gopkg.in/ini.v1"

"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
)

Expand All @@ -40,29 +43,32 @@ const TEMP_PREFIX = "spice"
type Spice struct {
}

func DownloadSpice(namespace string, vm string, restClient *rest.RESTClient) (string, error) {
body, err := restClient.Get().
Resource("virtualmachines").SetHeader("Accept", "text/plain").
func DownloadSpice(namespace string, vm string, restClient *rest.RESTClient) (*v1.Spice, error) {
spice := &v1.Spice{}
err := restClient.Get().
Resource("virtualmachines").SetHeader("Accept", "application/json").
SubResource("spice").
Namespace(namespace).
Name(vm).Do().Raw()
Name(vm).Do().Into(spice)
if err != nil {
return "", errors.New(fmt.Sprintf("Can't read body: %s\n", err.Error()))
return nil, errors.New(fmt.Sprintf("Can't fetch connection details: %s\n", err.Error()))
}
return fmt.Sprintf("%s", body), nil
return spice, nil
}

func (o *Spice) FlagSet() *flag.FlagSet {

cf := flag.NewFlagSet(FLAG, flag.ExitOnError)
cf.BoolP("details", "d", false, "If present, print SPICE console to stdout, otherwise run remote-viewer")
cf.StringP("proxy", "p", "", "If given, will override any given proxy from the server")
return cf
}

func (o *Spice) Run(flags *flag.FlagSet) int {
server, _ := flags.GetString("server")
kubeconfig, _ := flags.GetString("kubeconfig")
details, _ := flags.GetBool("details")
proxy, _ := flags.GetString("proxy")
namespace, _ := flags.GetString("namespace")
if namespace == "" {
namespace = kubev1.NamespaceDefault
Expand All @@ -80,13 +86,26 @@ func (o *Spice) Run(flags *flag.FlagSet) int {
log.Println(err)
return 1
}
body, err := DownloadSpice(namespace, vm, virtClient.RestClient())
spice, err := DownloadSpice(namespace, vm, virtClient.RestClient())
if err != nil {
log.Fatalf(err.Error())
return 1
}
if proxy != "" {
spice.Info.Proxy = proxy
}
cfg := ini.Empty()
err = ini.ReflectFrom(cfg, spice)
if err != nil {
log.Fatalf("Can't serialize spice struct to ini")
return 1
}
if details {
fmt.Printf("%s", body)
_, err := cfg.WriteTo(os.Stdout)
if err != nil {
log.Fatalf("Failed to write to stdout")
return 1
}
} else {
f, err := ioutil.TempFile("", TEMP_PREFIX)

Expand All @@ -97,7 +116,7 @@ func (o *Spice) Run(flags *flag.FlagSet) int {
defer os.Remove(f.Name())
defer f.Close()

_, err = f.WriteString(body)
_, err = cfg.WriteTo(f)
if err != nil {
log.Fatalf("Can't write to file: %s", err.Error())
return 1
Expand All @@ -117,12 +136,14 @@ func (o *Spice) Run(flags *flag.FlagSet) int {
}

func (o *Spice) Usage() string {
usage := "virtctl can connect via remote-viewer to VM, or show SPICE connection details\n\n"
usage := "virtctl can connect via remote-viewer to a VM, or can show SPICE connection details\n\n"
usage += "Examples:\n"
usage += "# Show SPICE connection details of the VM testvm\n"
usage += "./virtctl spice testvm --details\n\n"
usage += "# Connect to testvm via remote-viewer\n"
usage += "./virtctl spice testvm\n\n"
usage += "# Connect to testvm via remote-viewer using a proxy\n"
usage += "./virtctl spice testvm --proxy http://192.168.200.2:1234\n\n"
usage += "Options:\n"
usage += o.FlagSet().FlagUsages()
return usage
Expand Down

0 comments on commit 3975198

Please sign in to comment.