forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_provider.go
117 lines (101 loc) · 3.87 KB
/
service_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package services
import (
"reflect"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/plugin"
"github.com/gaia-pipeline/gaia/security"
"github.com/gaia-pipeline/gaia/store"
"github.com/gaia-pipeline/gaia/workers/scheduler"
)
// storeService is an instance of store.
// Use this to talk to the store.
var storeService store.GaiaStore
// schedulerService is an instance of scheduler.
var schedulerService scheduler.GaiaScheduler
// certificateService is the singleton holding the certificate manager.
var certificateService security.CAAPI
// vaultService is an instance of the internal Vault.
var vaultService security.VaultAPI
// StorageService initializes and keeps track of a storage service.
// If the internal storage service is a singleton. This function retruns an error
// but most of the times we don't care about it, because it's only ever
// initialized once in the main.go. If it wouldn't work, main would
// os.Exit(1) and the rest of the application would just stop.
func StorageService() (store.GaiaStore, error) {
if storeService != nil && !reflect.ValueOf(storeService).IsNil() {
return storeService, nil
}
storeService = store.NewBoltStore()
err := storeService.Init()
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize store", "error", err.Error())
return storeService, err
}
return storeService, nil
}
// MockStorageService sets the internal store singleton to the give
// mock implementation. A mock needs to be created in the test. The
// provider will make sure that everything that would use the store
// will use the mock instead.
func MockStorageService(store store.GaiaStore) {
storeService = store
}
// SchedulerService initializes keeps track of the scheduler service.
// The internal service is a singleton. This function retruns an error
// but most of the times we don't care about it, because it's only ever
// initialized once in the main.go. If it wouldn't work, main would
// os.Exit(1) and the rest of the application would just stop.
func SchedulerService() (scheduler.GaiaScheduler, error) {
if schedulerService != nil && !reflect.ValueOf(schedulerService).IsNil() {
return schedulerService, nil
}
pS := &plugin.Plugin{}
schedulerService = scheduler.NewScheduler(storeService, pS, certificateService, vaultService)
err := schedulerService.Init()
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize scheduler:", "error", err.Error())
return schedulerService, err
}
return schedulerService, nil
}
// MockSchedulerService which replaces the scheduler service
// with a mocked one.
func MockSchedulerService(scheduler scheduler.GaiaScheduler) {
schedulerService = scheduler
}
// CertificateService creates a certificate manager service.
func CertificateService() (security.CAAPI, error) {
if certificateService != nil && !reflect.ValueOf(certificateService).IsNil() {
return certificateService, nil
}
c, err := security.InitCA()
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize certificate manager:", "error", err.Error())
return nil, err
}
certificateService = c
return certificateService, nil
}
// MockCertificateService provides a way to create and set a mock
// for the internal certificate service manager.
func MockCertificateService(service security.CAAPI) {
certificateService = service
}
// VaultService creates a vault manager service.
func VaultService(vaultStore security.VaultStorer) (security.VaultAPI, error) {
if vaultService != nil && !reflect.ValueOf(vaultService).IsNil() {
return vaultService, nil
}
v, err := security.NewVault(certificateService, vaultStore)
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize vault manager:", "error", err.Error())
return nil, err
}
vaultService = v
return vaultService, nil
}
// MockVaultService provides a way to create and set a mock
// for the internal vault service manager.
func MockVaultService(service security.VaultAPI) {
vaultService = service
}