This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
start.go
281 lines (232 loc) · 6.25 KB
/
start.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package start
import (
"context"
"errors"
"runtime/debug"
"strings"
"time"
"github.com/caos/orbos/internal/api"
"github.com/caos/orbos/internal/executables"
"github.com/caos/orbos/internal/git"
"github.com/caos/orbos/internal/ingestion"
"github.com/caos/orbos/internal/operator/boom"
"github.com/caos/orbos/internal/operator/orbiter"
"github.com/caos/orbos/internal/operator/orbiter/kinds/orb"
"github.com/caos/orbos/internal/operator/secretfuncs"
orbconfig "github.com/caos/orbos/internal/orb"
"github.com/caos/orbos/internal/secret"
"github.com/caos/orbos/mntr"
"github.com/golang/protobuf/ptypes"
structpb "github.com/golang/protobuf/ptypes/struct"
"google.golang.org/grpc"
)
type OrbiterConfig struct {
Recur bool
Destroy bool
Deploy bool
Verbose bool
Version string
OrbConfigPath string
GitCommit string
IngestionAddress string
}
func Orbiter(ctx context.Context, monitor mntr.Monitor, conf *OrbiterConfig, orbctlGit *git.Client) ([]string, error) {
go checks(monitor, orbctlGit)
finishedChan := make(chan struct{})
takeoffChan := make(chan struct{})
on := func() { takeoffChan <- struct{}{} }
go on()
var initialized bool
loop:
for {
select {
case <-finishedChan:
break loop
case <-takeoffChan:
iterate(conf, orbctlGit, !initialized, ctx, monitor, finishedChan, func(iterated bool) {
if iterated {
initialized = true
}
go on()
})
}
}
return GetKubeconfigs(monitor, orbctlGit)
}
func iterate(conf *OrbiterConfig, gitClient *git.Client, firstIteration bool, ctx context.Context, monitor mntr.Monitor, finishedChan chan struct{}, done func(iterated bool)) {
orbFile, err := orbconfig.ParseOrbConfig(conf.OrbConfigPath)
if err != nil {
monitor.Error(err)
done(false)
return
}
if err := gitClient.Configure(orbFile.URL, []byte(orbFile.Repokey)); err != nil {
monitor.Error(err)
done(false)
return
}
if err := gitClient.Clone(); err != nil {
monitor.Error(err)
return
}
pushEvents := func(events []*ingestion.EventRequest) error { return nil }
if conf.IngestionAddress != "" {
conn, err := grpc.Dial(conf.IngestionAddress, grpc.WithInsecure())
if err != nil {
panic(err)
}
ingc := ingestion.NewIngestionServiceClient(conn)
defer conn.Close()
pushEvents = func(events []*ingestion.EventRequest) error {
_, err := ingc.PushEvents(ctx, &ingestion.EventsRequest{
Orb: orbFile.URL,
Events: events,
})
return err
}
}
if firstIteration {
if conf.Recur {
orbiter.Metrics()
}
if err := pushEvents([]*ingestion.EventRequest{{
CreationDate: ptypes.TimestampNow(),
Type: "orbiter.tookoff",
Data: &structpb.Struct{
Fields: map[string]*structpb.Value{
"commit": &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: conf.GitCommit}},
},
},
}}); err != nil {
panic(err)
}
started := float64(time.Now().UTC().Unix())
go func() {
for range time.Tick(time.Minute) {
pushEvents([]*ingestion.EventRequest{{
CreationDate: ptypes.TimestampNow(),
Type: "orbiter.running",
Data: &structpb.Struct{
Fields: map[string]*structpb.Value{
"since": {Kind: &structpb.Value_NumberValue{NumberValue: started}},
},
},
}})
}
}()
executables.Populate()
monitor.WithFields(map[string]interface{}{
"version": conf.Version,
"commit": conf.GitCommit,
"destroy": conf.Destroy,
"verbose": conf.Verbose,
"repoURL": orbFile.URL,
}).Info("Orbiter took off")
}
adaptFunc := orb.AdaptFunc(
orbFile,
conf.GitCommit,
!conf.Recur,
conf.Deploy)
takeoffConf := &orbiter.Config{
OrbiterCommit: conf.GitCommit,
GitClient: gitClient,
Adapt: adaptFunc,
FinishedChan: finishedChan,
PushEvents: pushEvents,
OrbConfig: *orbFile,
}
takeoff := orbiter.Takeoff(monitor, takeoffConf)
go func() {
started := time.Now()
takeoff()
monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
}).Info("Iteration done")
debug.FreeOSMemory()
done(true)
}()
}
func GetKubeconfigs(monitor mntr.Monitor, gitClient *git.Client) ([]string, error) {
kubeconfigs := make([]string, 0)
orbTree, err := api.ReadOrbiterYml(gitClient)
if err != nil {
return nil, errors.New("Failed to parse orbiter.yml")
}
orbDef, err := orb.ParseDesiredV0(orbTree)
if err != nil {
return nil, errors.New("Failed to parse orbiter.yml")
}
for clustername, _ := range orbDef.Clusters {
path := strings.Join([]string{"orbiter", clustername, "kubeconfig"}, ".")
value, err := secret.Read(
monitor,
gitClient,
secretfuncs.GetSecrets(),
path)
if err != nil || value == "" {
return nil, errors.New("Failed to get kubeconfig")
}
monitor.Info("Read kubeconfig for boom deployment")
kubeconfigs = append(kubeconfigs, value)
}
return kubeconfigs, nil
}
func Boom(monitor mntr.Monitor, orbConfigPath string, localmode bool, version string) error {
ensureClient := gitClient(monitor, "ensure")
queryClient := gitClient(monitor, "query")
// We don't need to check both clients
go checks(monitor, queryClient)
boom.Metrics(monitor)
takeoffChan := make(chan struct{})
go func() {
takeoffChan <- struct{}{}
}()
for range takeoffChan {
ensureChan := make(chan struct{})
queryChan := make(chan struct{})
ensure, query := boom.Takeoff(
monitor,
"/boom",
localmode,
orbConfigPath,
ensureClient,
queryClient,
)
go func() {
started := time.Now()
query()
monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
}).Info("Iteration done")
debug.FreeOSMemory()
queryChan <- struct{}{}
}()
go func() {
started := time.Now()
ensure()
monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
}).Info("Iteration done")
debug.FreeOSMemory()
ensureChan <- struct{}{}
}()
go func() {
<-queryChan
<-ensureChan
takeoffChan <- struct{}{}
}()
}
return nil
}
func gitClient(monitor mntr.Monitor, task string) *git.Client {
return git.New(context.Background(), monitor.WithField("task", task), "Boom", "boom@caos.ch")
}
func checks(monitor mntr.Monitor, client *git.Client) {
t := time.NewTicker(13 * time.Hour)
for range t.C {
if err := client.Check(); err != nil {
monitor.Error(err)
}
}
}