Skip to content

Commit

Permalink
VirtControllerApp structure
Browse files Browse the repository at this point in the history
Move the global variables from the vir-controller package into
a struct in order to control the scope and lifetime.

Signed-off-by: Adam Young <[email protected]>
  • Loading branch information
admiyo committed Jul 21, 2017
1 parent 03fc14f commit 7fab9ae
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 143 deletions.
84 changes: 39 additions & 45 deletions pkg/virt-controller/watch/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ import (
"kubevirt.io/kubevirt/pkg/virt-controller/services"
)

var (
flags Flags = Flags{}
host string
port int
type VirtControllerApp struct {
clientSet *kubernetes.Clientset
templateService services.TemplateService
restClient *clientrest.RESTClient
clientSet *kubernetes.Clientset
vmService services.VMService
informerFactory kubeinformers.KubeInformerFactory
podInformer cache.SharedIndexInformer
Expand All @@ -41,9 +38,7 @@ var (
vmController *VMController
vmInformer cache.SharedIndexInformer
vmQueue workqueue.RateLimitingInterface
)

type Flags struct {
host string
port int
launcherImage string
Expand All @@ -52,19 +47,19 @@ type Flags struct {

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

DefineFlags()
app.DefineFlags()

logging.InitializeLogging("virt-controller")
logger := logging.DefaultLogger()

clientSet, err = kubecli.Get()
app.clientSet, err = kubecli.Get()

if err != nil {
golog.Fatal(err)
}

restClient, err = kubecli.GetRESTClient()
app.restClient, err = kubecli.GetRESTClient()
if err != nil {
golog.Fatal(err)
}
Expand All @@ -73,57 +68,56 @@ func Execute() {

// Bootstrapping. From here on the initialization order is important

informerFactory = kubeinformers.NewKubeInformerFactory(restClient, clientSet)

vmInformer = informerFactory.VM()
migrationInformer = informerFactory.Migration()
podInformer = informerFactory.KubeVirtPod()
app.informerFactory = kubeinformers.NewKubeInformerFactory(app.restClient, app.clientSet)

vmQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
vmCache = vmInformer.GetStore()
vmInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForWorkqueue(vmQueue))
podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(vmLabelHandler(vmQueue)))
app.vmInformer = app.informerFactory.VM()
app.migrationInformer = app.informerFactory.Migration()
app.podInformer = app.informerFactory.KubeVirtPod()

migrationQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
migrationInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForWorkqueue(migrationQueue))
podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(migrationJobLabelHandler(migrationQueue)))
podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(migrationPodLabelHandler(migrationQueue)))
migrationCache = migrationInformer.GetStore()
app.vmQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
app.vmCache = app.vmInformer.GetStore()
app.vmInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForWorkqueue(app.vmQueue))
app.podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(vmLabelHandler(app.vmQueue)))

initCommon()
app.migrationQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
app.migrationInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForWorkqueue(app.migrationQueue))
app.podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(migrationJobLabelHandler(app.migrationQueue)))
app.podInformer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForFunc(migrationPodLabelHandler(app.migrationQueue)))
app.migrationCache = app.migrationInformer.GetStore()

app.initCommon()
app.Run()
}
func (vca *VirtControllerApp) Run() {
logger := logging.DefaultLogger()
stop := make(chan struct{})
defer close(stop)
informerFactory.Start(stop)

go vmController.Run(1, stop)

vca.informerFactory.Start(stop)
go vca.vmController.Run(1, stop)
//FIXME when we have more than one worker, we need a lock on the VM
go migrationController.Run(1, stop)

go vca.migrationController.Run(1, stop)
httpLogger := logger.With("service", "http")

httpLogger.Info().Log("action", "listening", "interface", host, "port", port)
if err := http.ListenAndServe(host+":"+strconv.Itoa(port), nil); err != nil {
httpLogger.Info().Log("action", "listening", "interface", vca.host, "port", vca.port)
if err := http.ListenAndServe(vca.host+":"+strconv.Itoa(vca.port), nil); err != nil {
golog.Fatal(err)
}
}

func initCommon() {
func (vca *VirtControllerApp) initCommon() {
var err error
templateService, err = services.NewTemplateService(flags.launcherImage, flags.migratorImage)
vca.templateService, err = services.NewTemplateService(vca.launcherImage, vca.migratorImage)
if err != nil {
golog.Fatal(err)
}
vmService = services.NewVMService(clientSet, restClient, templateService)
vmController = NewVMController(restClient, vmService, vmQueue, vmCache, vmInformer, podInformer, nil, clientSet)
migrationController = NewMigrationController(restClient, vmService, clientSet, migrationQueue, migrationInformer, podInformer, migrationCache)
vca.vmService = services.NewVMService(vca.clientSet, vca.restClient, vca.templateService)
vca.vmController = NewVMController(vca.restClient, vca.vmService, vca.vmQueue, vca.vmCache, vca.vmInformer, vca.podInformer, nil, vca.clientSet)
vca.migrationController = NewMigrationController(vca.restClient, vca.vmService, vca.clientSet, vca.migrationQueue, vca.migrationInformer, vca.podInformer, vca.migrationCache)
}

func DefineFlags() {
flag.StringVar(&flags.host, "listen", "0.0.0.0", "Address and port where to listen on")
flag.IntVar(&flags.port, "port", 8182, "Port to listen on")
flag.StringVar(&flags.launcherImage, "launcher-image", "virt-launcher", "Shim container for containerized VMs")
flag.StringVar(&flags.migratorImage, "migrator-image", "virt-handler", "Container which orchestrates a VM migration")
func (vca *VirtControllerApp) DefineFlags() {
flag.StringVar(&vca.host, "listen", "0.0.0.0", "Address and port where to listen on")
flag.IntVar(&vca.port, "port", 8182, "Port to listen on")
flag.StringVar(&vca.launcherImage, "launcher-image", "virt-launcher", "Shim container for containerized VMs")
flag.StringVar(&vca.migratorImage, "migrator-image", "virt-handler", "Container which orchestrates a VM migration")
flag.Parse()
}
Loading

0 comments on commit 7fab9ae

Please sign in to comment.