Skip to content

Commit

Permalink
hooks: make hooks manager a singleton
Browse files Browse the repository at this point in the history
This will make it easier to use it from various places of kubevirt
without a need to pass the manager everywhere or recollect sidecar
sockets.
  • Loading branch information
phoracek committed Jul 2, 2018
1 parent debf62e commit 434c21e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 4 additions & 3 deletions cmd/virt-launcher/virt-launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,10 @@ func main() {
log.InitializeLogging("virt-launcher")

// Block until all requested hookSidecars are ready
if *requestedHooks != 0 {
hookManager := hooks.NewManager()
hookManager.Collect(*requestedHooks)
hookManager := hooks.GetManager()
err := hookManager.Collect(*requestedHooks)
if err != nil {
panic(err)
}

vm := v1.NewVMIReferenceFromNameWithNS(*namespace, *name)
Expand Down
13 changes: 11 additions & 2 deletions pkg/hooks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"sort"
"strings"
"sync"
"time"

"google.golang.org/grpc"
Expand All @@ -23,12 +24,19 @@ type hookClient struct {
hookPoints []*hooksInfo.HookPoint
}

var manager *Manager
var once sync.Once

type Manager struct {
collected bool
callbacksPerHookPoint map[string][]*hookClient
}

func NewManager() *Manager {
return &Manager{}
func GetManager() *Manager {
once.Do(func() {
manager = &Manager{collected: false}
})
return manager
}

func (m *Manager) Collect(numberOfRequestedHookSidecars uint) error {
Expand All @@ -41,6 +49,7 @@ func (m *Manager) Collect(numberOfRequestedHookSidecars uint) error {
sortCallbacksPerHookPoint(callbacksPerHookPoint)
log.Log.Infof("Sorted all collected sidecar sockets per hook point based on their priority and name: %v", callbacksPerHookPoint)

m.collected = true
m.callbacksPerHookPoint = callbacksPerHookPoint

return nil
Expand Down

0 comments on commit 434c21e

Please sign in to comment.