-
Notifications
You must be signed in to change notification settings - Fork 25
/
selenosis.go
169 lines (152 loc) · 4.74 KB
/
selenosis.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package selenosis
import (
"time"
"github.com/alcounit/selenosis/config"
"github.com/alcounit/selenosis/platform"
"github.com/alcounit/selenosis/storage"
log "github.com/sirupsen/logrus"
)
//Configuration ....
type Configuration struct {
SelenosisHost string
ServiceName string
SidecarPort string
SessionLimit int
SessionRetryCount int
BrowserWaitTimeout time.Duration
SessionIdleTimeout time.Duration
BuildVersion string
}
//App ...
type App struct {
logger *log.Logger
client platform.Platform
browsers *config.BrowsersConfig
selenosisHost string
serviceName string
sidecarPort string
sessionLimit int
sessionRetryCount int
sessionIdleTimeout time.Duration
browserWaitTimeout time.Duration
buildVersion string
stats *storage.Storage
}
//New ...
func New(logger *log.Logger, client platform.Platform, browsers *config.BrowsersConfig, cfg Configuration) *App {
storage := storage.New()
state, err := client.State()
if err != nil {
logger.Errorf("failed to get cluster state: %v", err)
}
for _, service := range state.Services {
storage.Sessions().Put(service.SessionID, service)
}
for _, worker := range state.Workers {
storage.Workers().Put(worker.Name, worker)
}
limit := cfg.SessionLimit
currentTotal := func() int64 {
return int64(storage.Workers().Len() + limit)
}
var quota platform.Quota
if quota, err = client.Quota().Get(); err != nil {
quota, err = client.Quota().Create(currentTotal())
if err != nil {
logger.Fatalf("failed to create quota resource: %v", err)
}
}
if quota.CurrentMaxLimit != int64(currentTotal()) {
quota, err = client.Quota().Update(currentTotal())
if err != nil {
logger.Warnf("failed to update quota resource %v:", err)
}
}
storage.Quota().Put(quota)
logger.Infof("current cluster state: sessions - %d, workers - %d, session limit - %d", storage.Sessions().Len(), storage.Workers().Len(), limit)
ch := client.Watch()
go func() {
for {
select {
case event := <- ch:
switch event.PlatformObject.(type) {
case platform.Service:
service := event.PlatformObject.(platform.Service)
switch event.Type {
case platform.Added:
storage.Sessions().Put(service.SessionID, service)
case platform.Updated:
storage.Sessions().Put(service.SessionID, service)
case platform.Deleted:
storage.Sessions().Delete(service.SessionID)
}
case platform.Worker:
worker := event.PlatformObject.(platform.Worker)
switch event.Type {
case platform.Added:
storage.Workers().Put(worker.Name, worker)
result, err := client.Quota().Update(currentTotal())
if err != nil {
logger.Warnf("failed to update resource quota: %v", err)
break
}
storage.Quota().Put(result)
logger.Infof("selenosis worker: %s added, current namespace quota limit: %d", worker.Name, storage.Quota().Get().CurrentMaxLimit)
case platform.Deleted:
storage.Workers().Delete(worker.Name)
result, err := client.Quota().Update(currentTotal())
if err != nil {
logger.Warnf("failed to update resource quota: %v", err)
break
}
storage.Quota().Put(result)
logger.Infof("selenosis worker: %s removed, current namespace quota limit: %d", worker.Name, storage.Quota().Get().CurrentMaxLimit)
}
case platform.Quota:
quota := event.PlatformObject.(platform.Quota)
switch event.Type {
case platform.Added:
if quota.CurrentMaxLimit != currentTotal() {
quota, err = client.Quota().Update(currentTotal())
if err != nil {
logger.Warnf("failed to update quota resource %v:", err)
break
}
storage.Quota().Put(quota)
}
case platform.Updated:
if quota.CurrentMaxLimit != currentTotal() {
quota, err = client.Quota().Update(currentTotal())
if err != nil {
logger.Warnf("failed to update quota resource %v:", err)
break
}
storage.Quota().Put(quota)
}
case platform.Deleted:
quota, err = client.Quota().Create(currentTotal())
if err != nil {
logger.Warnf("failed to update quota resource %v:", err)
break
}
storage.Quota().Put(quota)
}
}
}
}
}()
return &App{
logger: logger,
client: client,
browsers: browsers,
selenosisHost: cfg.SelenosisHost,
serviceName: cfg.ServiceName,
sidecarPort: cfg.SidecarPort,
sessionLimit: cfg.SessionLimit,
sessionRetryCount: cfg.SessionRetryCount,
browserWaitTimeout: cfg.BrowserWaitTimeout,
sessionIdleTimeout: cfg.SessionIdleTimeout,
buildVersion: cfg.BuildVersion,
stats: storage,
}
}