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
259 lines (216 loc) · 5.71 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
package start
import (
"context"
"errors"
"runtime/debug"
"strings"
"time"
"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/internal/utils/orbgit"
"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) {
if conf.Recur {
orbiter.Metrics()
}
finishedChan := make(chan bool)
orbFile, err := orbconfig.ParseOrbConfig(conf.OrbConfigPath)
if err != nil {
panic(err)
}
pushEvents := func(_ []*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)
pushEvents = func(events []*ingestion.EventRequest) error {
_, err := ingc.PushEvents(ctx, &ingestion.EventsRequest{
Orb: orbFile.URL,
Events: events,
})
return err
}
}
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": &structpb.Value{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")
go func() {
takeoffChan := make(chan struct{})
go func() {
takeoffChan <- struct{}{}
}()
for range takeoffChan {
orbConfig, err := orbconfig.ParseOrbConfig(conf.OrbConfigPath)
if err != nil {
monitor.Error(err)
return
}
gitClientConf := &orbgit.Config{
Comitter: "orbiter",
Email: "orbiter@caos.ch",
OrbConfig: orbConfig,
Action: "iteration",
}
gitClient, cleanUp, err := orbgit.NewGitClient(ctx, monitor, gitClientConf)
if err != nil {
monitor.Error(err)
return
}
adaptFunc := orb.AdaptFunc(
orbFile,
conf.GitCommit,
!conf.Recur,
conf.Deploy)
takeoffConf := &orbiter.Config{
OrbiterCommit: conf.GitCommit,
GitClient: gitClient,
Adapt: adaptFunc,
FinishedChan: finishedChan,
PushEvents: pushEvents,
}
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()
takeoffChan <- struct{}{}
}()
cleanUp()
}
}()
finished := false
for !finished {
finished = <-finishedChan
}
return GetKubeconfigs(monitor, orbctlGit, orbFile)
}
func GetKubeconfigs(monitor mntr.Monitor, gitClient *git.Client, orbFile *orbconfig.Orb) ([]string, error) {
kubeconfigs := make([]string, 0)
orbTree, err := orbiter.Parse(gitClient, "orbiter.yml")
if err != nil {
return nil, errors.New("Failed to parse orbiter.yml")
}
orbDef, err := orb.ParseDesiredV0(orbTree[0])
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(orbFile),
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 {
boom.Metrics(monitor)
takeoffChan := make(chan struct{})
go func() {
takeoffChan <- struct{}{}
}()
for range takeoffChan {
orbConfig, err := orbconfig.ParseOrbConfig(orbConfigPath)
if err != nil {
monitor.Error(err)
return err
}
boomChan := make(chan struct{})
currentChan := make(chan struct{})
takeoff, takeoffCurrent := boom.Takeoff(
monitor,
orbConfig,
"/boom",
localmode,
version,
)
go func() {
started := time.Now()
takeoffCurrent()
monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
}).Info("Iteration done")
debug.FreeOSMemory()
currentChan <- struct{}{}
}()
go func() {
started := time.Now()
takeoff()
monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
}).Info("Iteration done")
debug.FreeOSMemory()
boomChan <- struct{}{}
}()
go func() {
<-currentChan
<-boomChan
takeoffChan <- struct{}{}
}()
}
return nil
}