From 02789c78d178f47ea5146ce211c35d2038c812b0 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Mon, 10 Jul 2023 14:46:10 +0200 Subject: [PATCH] rest: Isolate Code-Hex/vz code from the rest code Code-Hex/vz requires cgo/native builds, so it's preferrable to separate it from the client rest code. This PR achieves this by introducing interfaces to help move the mac-specific code to a separate package --- cmd/vfkit/main.go | 4 +- pkg/rest/rest.go | 54 +++++---------------------- pkg/rest/{ => vf}/state_change.go | 0 pkg/rest/vf/vm_config.go | 62 +++++++++++++++++++++++++++++++ pkg/rest/vm_config.go | 14 ------- 5 files changed, 74 insertions(+), 60 deletions(-) rename pkg/rest/{ => vf}/state_change.go (100%) create mode 100644 pkg/rest/vf/vm_config.go delete mode 100644 pkg/rest/vm_config.go diff --git a/cmd/vfkit/main.go b/cmd/vfkit/main.go index 3c145d75..1a0641c0 100644 --- a/cmd/vfkit/main.go +++ b/cmd/vfkit/main.go @@ -32,6 +32,7 @@ import ( "github.com/crc-org/vfkit/pkg/cmdline" "github.com/crc-org/vfkit/pkg/config" "github.com/crc-org/vfkit/pkg/rest" + restvf "github.com/crc-org/vfkit/pkg/rest/vf" "github.com/crc-org/vfkit/pkg/vf" "github.com/docker/go-units" log "github.com/sirupsen/logrus" @@ -133,7 +134,8 @@ func runVFKit(vmConfig *config.VirtualMachine, opts *cmdline.Options) error { // Do not enable the rests server if user sets scheme to None if opts.RestfulURI != cmdline.DefaultRestfulURI { - srv, err := rest.NewServer(rest.NewVzVirtualMachine(vm, vzVMConfig), opts.RestfulURI) + restVM := restvf.NewVzVirtualMachine(vm, vzVMConfig) + srv, err := rest.NewServer(restVM, restVM, opts.RestfulURI) if err != nil { return err } diff --git a/pkg/rest/rest.go b/pkg/rest/rest.go index 3f69620a..9b1bd576 100644 --- a/pkg/rest/rest.go +++ b/pkg/rest/rest.go @@ -3,12 +3,10 @@ package rest import ( "errors" "fmt" - "net/http" "net/url" "strings" "github.com/crc-org/vfkit/pkg/cmdline" - "github.com/crc-org/vfkit/pkg/rest/define" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) @@ -73,7 +71,7 @@ func (v *VFKitService) Start() { } // NewServer creates a new restful service -func NewServer(vm *VzVirtualMachine, endpoint string) (*VFKitService, error) { +func NewServer(inspector VirtualMachineInspector, stateHandler VirtualMachineStateHandler, endpoint string) (*VFKitService, error) { r := gin.Default() ep, err := NewEndpoint(endpoint) if err != nil { @@ -85,53 +83,19 @@ func NewServer(vm *VzVirtualMachine, endpoint string) (*VFKitService, error) { } // Handlers for the restful service. This is where endpoints are defined. - r.GET("/vm/state", vm.getVMState) - r.POST("/vm/state", vm.setVMState) - r.GET("/vm/inspect", vm.inspect) + r.GET("/vm/state", stateHandler.GetVMState) + r.POST("/vm/state", stateHandler.SetVMState) + r.GET("/vm/inspect", inspector.Inspect) return &s, nil } -// inspect returns information about the virtual machine like hw resources -// and devices -func (vm *VzVirtualMachine) inspect(c *gin.Context) { - ii := define.InspectResponse{ - // TODO complete me - CPUs: 1, - Memory: 2048, - //Devices: vm.Devices, - } - c.JSON(http.StatusOK, ii) -} - -// getVMState retrieves the current vm state -func (vm *VzVirtualMachine) getVMState(c *gin.Context) { - current := vm.GetState() - c.JSON(http.StatusOK, gin.H{"state": current.String()}) +type VirtualMachineInspector interface { + Inspect(c *gin.Context) } -// setVMState requests a state change on a virtual machine. At this time only -// the following states are valid: -// Pause - pause a running machine -// Resume - resume a paused machine -// Stop - stops a running machine -// HardStop - forceably stops a running machine -func (vm *VzVirtualMachine) setVMState(c *gin.Context) { - var ( - s define.VMState - ) - - if err := c.ShouldBindJSON(&s); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - response := vm.ChangeState(define.StateChange(s.State)) - if response != nil { - logrus.Errorf("failed action %s: %q", s.State, response) - c.JSON(http.StatusInternalServerError, gin.H{"error": response.Error()}) - return - } - c.Status(http.StatusAccepted) +type VirtualMachineStateHandler interface { + GetVMState(c *gin.Context) + SetVMState(c *gin.Context) } // parseRestfulURI validates the input URI and returns an URL object diff --git a/pkg/rest/state_change.go b/pkg/rest/vf/state_change.go similarity index 100% rename from pkg/rest/state_change.go rename to pkg/rest/vf/state_change.go diff --git a/pkg/rest/vf/vm_config.go b/pkg/rest/vf/vm_config.go new file mode 100644 index 00000000..d893208b --- /dev/null +++ b/pkg/rest/vf/vm_config.go @@ -0,0 +1,62 @@ +package rest + +import ( + "net/http" + + "github.com/Code-Hex/vz/v3" + "github.com/crc-org/vfkit/pkg/rest/define" + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" +) + +type VzVirtualMachine struct { + VzVM *vz.VirtualMachine + config *vz.VirtualMachineConfiguration +} + +func NewVzVirtualMachine(vm *vz.VirtualMachine, config *vz.VirtualMachineConfiguration) *VzVirtualMachine { + return &VzVirtualMachine{config: config, VzVM: vm} +} + +// inspect returns information about the virtual machine like hw resources +// and devices +func (vm *VzVirtualMachine) Inspect(c *gin.Context) { + ii := define.InspectResponse{ + // TODO complete me + CPUs: 1, + Memory: 2048, + //Devices: vm.Devices, + } + c.JSON(http.StatusOK, ii) +} + +// getVMState retrieves the current vm state +func (vm *VzVirtualMachine) GetVMState(c *gin.Context) { + current := vm.GetState() + c.JSON(http.StatusOK, gin.H{"state": current.String()}) +} + +// setVMState requests a state change on a virtual machine. At this time only +// the following states are valid: +// Pause - pause a running machine +// Resume - resume a paused machine +// Stop - stops a running machine +// HardStop - forceably stops a running machine +func (vm *VzVirtualMachine) SetVMState(c *gin.Context) { + var ( + s define.VMState + ) + + if err := c.ShouldBindJSON(&s); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + response := vm.ChangeState(define.StateChange(s.State)) + if response != nil { + logrus.Errorf("failed action %s: %q", s.State, response) + c.JSON(http.StatusInternalServerError, gin.H{"error": response.Error()}) + return + } + c.Status(http.StatusAccepted) +} diff --git a/pkg/rest/vm_config.go b/pkg/rest/vm_config.go deleted file mode 100644 index 62364bbf..00000000 --- a/pkg/rest/vm_config.go +++ /dev/null @@ -1,14 +0,0 @@ -package rest - -import ( - "github.com/Code-Hex/vz/v3" -) - -type VzVirtualMachine struct { - VzVM *vz.VirtualMachine - config *vz.VirtualMachineConfiguration -} - -func NewVzVirtualMachine(vm *vz.VirtualMachine, config *vz.VirtualMachineConfiguration) *VzVirtualMachine { - return &VzVirtualMachine{config: config, VzVM: vm} -}