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
/
takeoff.go
96 lines (79 loc) · 2.81 KB
/
takeoff.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
package boom
import (
"net/http"
"path/filepath"
"strings"
"time"
"github.com/caos/orbos/internal/git"
"github.com/caos/orbos/internal/orb"
"github.com/caos/orbos/internal/operator/boom/app"
gconfig "github.com/caos/orbos/internal/operator/boom/application/applications/grafana/config"
gitcrdconfig "github.com/caos/orbos/internal/operator/boom/gitcrd/config"
"github.com/caos/orbos/internal/utils/clientgo"
"github.com/caos/orbos/mntr"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func Metrics(monitor mntr.Monitor) {
metricsport := "2112"
http.Handle("/metrics", promhttp.Handler())
address := strings.Join([]string{":", metricsport}, "")
go func() {
if err := http.ListenAndServe(address, nil); err != nil {
monitor.Error(errors.Wrap(err, "error while serving metrics endpoint"))
}
monitor.WithFields(map[string]interface{}{
"port": metricsport,
"endpoint": "/metrics",
}).Info("Started metrics")
}()
}
func Takeoff(monitor mntr.Monitor, toolsDirectoryPath string, localMode bool, orbpath string, ensureClient, queryClient *git.Client) (func(), func()) {
gitcrdMonitor := monitor.WithField("type", "gitcrd")
if localMode {
clientgo.InConfig = false
}
gconfig.DashboardsDirectoryPath = filepath.Join(toolsDirectoryPath, "dashboards")
appStruct := app.New(monitor, toolsDirectoryPath)
currentStruct := app.New(monitor, toolsDirectoryPath)
return task(
monitor,
orbpath,
gitConf(gitcrdMonitor.WithField("task", "ensure"), ensureClient, toolsDirectoryPath, !localMode),
appStruct.ReadSpecs,
appStruct.Reconcile),
task(
monitor,
orbpath,
gitConf(gitcrdMonitor.WithField("task", "query"), queryClient, toolsDirectoryPath, !localMode),
currentStruct.ReadSpecs,
currentStruct.WriteBackCurrentState)
}
func gitConf(monitor mntr.Monitor, client *git.Client, toolsDirectoryPath string, deploy bool) gitcrdconfig.Config {
return gitcrdconfig.Config{
Monitor: monitor,
CrdDirectoryPath: filepath.Join(toolsDirectoryPath, "crd"),
Git: client,
Deploy: deploy,
}
}
func task(monitor mntr.Monitor, orbpath string, gitcrdConf gitcrdconfig.Config, readSpecs func(gitCrdConf *gitcrdconfig.Config, repoURL string, repoKey []byte) error, do func() error) func() {
return func() {
// TODO: use a function scoped error variable
started := time.Now()
orbConfig, goErr := orb.ParseOrbConfig(orbpath)
if goErr != nil {
monitor.Error(goErr)
return
}
if err := readSpecs(&gitcrdConf, orbConfig.URL, []byte(orbConfig.Repokey)); err != nil {
monitor.Error(errors.Wrap(err, "unable to start supervised crd"))
}
goErr = do()
recMonitor := monitor.WithFields(map[string]interface{}{
"took": time.Since(started),
})
recMonitor.Error(goErr)
recMonitor.Info("Reconciling iteration done")
}
}