Skip to content

Commit

Permalink
Produce ini or JSON based on Accept header for spice endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rmohr committed Jan 25, 2017
1 parent 82cc364 commit 2582958
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
4 changes: 2 additions & 2 deletions cluster/kubectl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fi
if [ "x$1" == "xspice" ]; then
viewer=${SPICE_VIEWER:-remote\-viewer}
if [ "x$3" == "x--details" ]; then
curl http://${master_ip}:8184/apis/kubevirt.io/v1alpha1/namespaces/default/vms/$2/spice
curl http://${master_ip}:8184/apis/kubevirt.io/v1alpha1/namespaces/default/vms/$2/spice -H"Accept:text/plain"
else
curl http://${master_ip}:8184/apis/kubevirt.io/v1alpha1/namespaces/default/vms/$2/spice > ${KUBEVIRT_PATH}cluster/.console.vv
curl http://${master_ip}:8184/apis/kubevirt.io/v1alpha1/namespaces/default/vms/$2/spice > ${KUBEVIRT_PATH}cluster/.console.vv -H"Accept:text/plain"
echo $viewer
$viewer ${KUBEVIRT_PATH}cluster/.console.vv
fi
Expand Down
18 changes: 12 additions & 6 deletions cmd/virt-api/virt-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"flag"
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful/swagger"
kithttp "github.com/go-kit/kit/transport/http"
"golang.org/x/net/context"
"gopkg.in/ini.v1"
"k8s.io/client-go/pkg/runtime/schema"
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
Expand All @@ -19,6 +21,7 @@ import (
func main() {

logging.InitializeLogging("virt-api")
ini.PrettyFormat = false
swaggerui := flag.String("swagger-ui", "third_party/swagger-ui", "swagger-ui location")
host := flag.String("listen", "0.0.0.0", "Address and port where to listen on")
port := flag.Int("port", 8183, "Port to listen on")
Expand All @@ -44,16 +47,19 @@ func main() {
}

// TODO, allow Encoder and Decoders per type and combine the endpoint logic
spiceINISubresource := endpoints.MakeGoRestfulWrapper(endpoints.NewHandlerBuilder().Get().
Endpoint(rest.NewSpiceINIEndpoint(cli, coreCli, vmGVR)).Encoder(endpoints.EncodePlainTextGetResponse).Build(ctx))
spiceJSONResource := endpoints.MakeGoRestfulWrapper(endpoints.NewHandlerBuilder().Get().
Endpoint(rest.NewSpiceJSONEndpoint(cli, coreCli, vmGVR)).Build(ctx))
spice := endpoints.MakeGoRestfulWrapper(endpoints.NewHandlerBuilder().Get().
Endpoint(rest.NewSpiceEndpoint(cli, coreCli, vmGVR)).Encoder(
endpoints.NewMimeTypeAwareEncoder(endpoints.EncodeGetResponse,
map[string]kithttp.EncodeResponseFunc{
"text/plain": endpoints.EncodeINIGetResponse,
"application/json": endpoints.EncodeGetResponse,
})).Build(ctx))

rest.WebService.Route(rest.WebService.GET(rest.SubResourcePath(vmGVR, "spice")).
To(spiceINISubresource).Produces("text/plain").
To(spice).Produces("text/plain", "application/json").
Doc("Returns a remote-viewer configuration file. Run `man 1 remote-viewer` to learn more about the configuration format."))
rest.WebService.Route(rest.WebService.GET(rest.ResourcePath(spiceGVR)).
To(spiceJSONResource).Produces("application/json").
To(spice).Produces("application/json", "text/plain").
Doc("Returns SPICE connection details as JSON."))

config := swagger.Config{
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ cluster/kubectl.sh spice testvm --details
To direclty query the config, do
```bash
curl $HOST/apis/kubevirt.io/v1alpha1/namespaces/default/vms/testvm/spice
curl $HOST/apis/kubevirt.io/v1alpha1/namespaces/default/vms/testvm/spice -H"Accept:text/plain"
```
### Accessing the Domain via the SPICE primay resource
Expand Down
34 changes: 1 addition & 33 deletions pkg/virt-api/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,7 @@ func init() {
flag.StringVar(&spiceProxy, "spice-proxy", "", "Spice proxy to use when spice access is requested")
}

func NewSpiceINIEndpoint(cli *rest.RESTClient, coreCli *kubernetes.Clientset, gvr schema.GroupVersionResource) endpoint.Endpoint {
return func(ctx context.Context, payload interface{}) (interface{}, error) {
metadata := payload.(*endpoints.Metadata)
obj, err := cli.Get().Namespace(metadata.Namespace).Resource(gvr.Resource).Name(metadata.Name).Do().Get()
if err != nil {
return nil, middleware.NewInternalServerError(err)
}

vm := obj.(*v1.VM)
spice, err := spiceFromVM(vm, coreCli)
if err != nil {
return nil, err

}

return toPlainText(spice), nil
}
}

func NewSpiceJSONEndpoint(cli *rest.RESTClient, coreCli *kubernetes.Clientset, gvr schema.GroupVersionResource) endpoint.Endpoint {
func NewSpiceEndpoint(cli *rest.RESTClient, coreCli *kubernetes.Clientset, gvr schema.GroupVersionResource) endpoint.Endpoint {
return func(ctx context.Context, payload interface{}) (interface{}, error) {
metadata := payload.(*endpoints.Metadata)
obj, err := cli.Get().Namespace(metadata.Namespace).Resource(gvr.Resource).Name(metadata.Name).Do().Get()
Expand Down Expand Up @@ -111,19 +92,6 @@ func spiceFromVM(vm *v1.VM, coreCli *kubernetes.Clientset) (*v1.Spice, error) {
return nil, middleware.NewResourceNotFoundError("No spice device attached to the VM found.")
}

func toPlainText(spice *v1.Spice) string {

config := "[virt-viewer]\n" +
fmt.Sprintf("type=%s\n", spice.Info.Type) +
fmt.Sprintf("host=%s\n", spice.Info.Host) +
fmt.Sprintf("port=%d\n", spice.Info.Port)

if len(spiceProxy) > 0 {
config = config + fmt.Sprintf("proxy=%s\n", spice.Info.Proxy)
}
return config
}

// TODO for now just copied from VMService
func unfinishedVMPodSelector(vm *v1.VM) kubev1.ListOptions {
fieldSelector := fields.ParseSelectorOrDie(
Expand Down
4 changes: 2 additions & 2 deletions tests/spice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("Vmlifecycle", func() {
}
return false
}).Watch()
raw, err := restClient.Get().Resource("vms").SubResource("spice").Namespace(kubev1.NamespaceDefault).Name(vm.GetObjectMeta().GetName()).Do().Raw()
raw, err := restClient.Get().Resource("vms").SetHeader("Accept", "text/plain").SubResource("spice").Namespace(kubev1.NamespaceDefault).Name(vm.GetObjectMeta().GetName()).Do().Raw()
Expect(err).To(BeNil())
spice := strings.Split(string(raw), "\n")

Expand Down Expand Up @@ -101,7 +101,7 @@ var _ = Describe("Vmlifecycle", func() {
}
return false
}).Watch()
raw, err := restClient.Get().Resource("vms").SubResource("spice").Namespace(kubev1.NamespaceDefault).Name(vm.GetObjectMeta().GetName()).Do().Raw()
raw, err := restClient.Get().Resource("vms").SetHeader("Accept", "text/plain").SubResource("spice").Namespace(kubev1.NamespaceDefault).Name(vm.GetObjectMeta().GetName()).Do().Raw()
Expect(err).To(BeNil())
spice := strings.Split(string(raw), "\n")

Expand Down

0 comments on commit 2582958

Please sign in to comment.