Skip to content

Commit

Permalink
add service interface
Browse files Browse the repository at this point in the history
As services in KubeVirt grow, it's useful to standardize the interface
each service has to implement. This leads to more unified
implementation of things such as flag handling.

This is accomplished by creating `service.Service` interface and
compile-check that each service conforms to this interface.

Signed-off-by: Martin Polednik <[email protected]>
  • Loading branch information
mpolednik committed Nov 29, 2017
1 parent da9b3ff commit a701358
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cmd/virt-handler/virt-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ import (
const defaultWatchdogTimeout = 30 * time.Second

type virtHandlerApp struct {
Service *service.Service
Service *service.ServiceListen
HostOverride string
LibvirtUri string
VirtShareDir string
EphemeralDiskDir string
WatchdogTimeoutDuration time.Duration
}

var _ service.Service = &virtHandlerApp{}

func newVirtHandlerApp(host *string, port *int, hostOverride *string, libvirtUri *string, virtShareDir *string, ephemeralDiskDir *string, watchdogTimeoutDuration *time.Duration) *virtHandlerApp {
if *hostOverride == "" {
defaultHostName, err := os.Hostname()
Expand All @@ -77,7 +79,7 @@ func newVirtHandlerApp(host *string, port *int, hostOverride *string, libvirtUri
}

return &virtHandlerApp{
Service: service.NewService("virt-handler", *host, *port),
Service: service.NewServiceListen("virt-handler", host, port),
HostOverride: *hostOverride,
LibvirtUri: *libvirtUri,
VirtShareDir: *virtShareDir,
Expand Down
6 changes: 4 additions & 2 deletions cmd/virt-manifest/virt-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ import (
)

type virtManifestApp struct {
Service *service.Service
Service *service.ServiceListen
LibvirtUri string
}

func newVirtManifestApp(host *string, port *int, libvirtUri *string) *virtManifestApp {
return &virtManifestApp{
Service: service.NewService("virt-manifest", *host, *port),
Service: service.NewServiceListen("virt-manifest", host, port),
LibvirtUri: *libvirtUri,
}
}

var _ service.Service = &virtManifestApp{}

func (app *virtManifestApp) Run() {
logger := log.Log
logger.Info("Starting virt-manifest server")
Expand Down
12 changes: 8 additions & 4 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ import (
"strconv"
)

type Service struct {
type Service interface {
Run()
}

type ServiceListen struct {
Name string
Host string
Port string
}

func NewService(name string, host string, port int) *Service {
return &Service{
func NewServiceListen(name string, host *string, port *int) *ServiceListen {
return &ServiceListen{
Name: name,
Host: host,
Port: strconv.Itoa(port),
}
}

func (service *Service) Address() string {
func (service *ServiceListen) Address() string {
return fmt.Sprintf("%s:%s", service.Host, service.Port)
}
6 changes: 4 additions & 2 deletions pkg/virt-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import (
)

type virtAPIApp struct {
Service *service.Service
Service *service.ServiceListen
SwaggerUI string
}

func NewVirtAPIApp(host string, port int, swaggerUI string) *virtAPIApp {
return &virtAPIApp{
Service: service.NewService("virt-api", host, port),
Service: service.NewServiceListen("virt-api", host, port),
SwaggerUI: swaggerUI,
}
}

var _ service.Service = &virtAPIApp{}

func (app *virtAPIApp) Compose() {
ctx := context.Background()
vmGVR := schema.GroupVersionResource{Group: v1.GroupVersion.Group, Version: v1.GroupVersion.Version, Resource: "virtualmachines"}
Expand Down
3 changes: 3 additions & 0 deletions pkg/virt-controller/watch/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/log"
"kubevirt.io/kubevirt/pkg/registry-disk"
"kubevirt.io/kubevirt/pkg/service"
"kubevirt.io/kubevirt/pkg/virt-controller/leaderelectionconfig"
"kubevirt.io/kubevirt/pkg/virt-controller/rest"
"kubevirt.io/kubevirt/pkg/virt-controller/services"
Expand Down Expand Up @@ -60,6 +61,8 @@ type VirtControllerApp struct {
readyChan chan bool
}

var _ service.Service = &VirtControllerApp{}

func Execute() {
var err error
var app VirtControllerApp = VirtControllerApp{}
Expand Down

0 comments on commit a701358

Please sign in to comment.