Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ee/agent-smith/cmd/run.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"net/http"
10
"os"
11
"os/signal"
12
"runtime"
13
"syscall"
14
"time"
15
16
"github.com/gitpod-io/gitpod/agent-smith/pkg/config"
17
18
"github.com/gitpod-io/gitpod/agent-smith/pkg/agent"
19
"github.com/gitpod-io/gitpod/common-go/log"
20
"github.com/gitpod-io/gitpod/common-go/pprof"
21
"github.com/prometheus/client_golang/prometheus"
22
"github.com/prometheus/client_golang/prometheus/promhttp"
23
"github.com/spf13/cobra"
24
)
25
26
// runCmd represents the run command
27
var runCmd = &cobra.Command{
28
Use: "run",
29
Short: "Starts agent smith",
30
Run: func(cmd *cobra.Command, args []string) {
31
cfg, err := config.GetConfig(cfgFile)
32
if err != nil {
33
log.WithError(err).Fatal("cannot get config")
34
}
35
36
if cfg.PProfAddr != "" {
37
go pprof.Serve(cfg.PProfAddr)
38
}
39
40
reg := prometheus.DefaultRegisterer
41
if cfg.PrometheusAddr != "" {
42
handler := http.NewServeMux()
43
handler.Handle("/metrics", promhttp.Handler())
44
45
go func() {
46
err := http.ListenAndServe(cfg.PrometheusAddr, handler)
47
if err != nil {
48
log.WithError(err).Error("Prometheus metrics server failed")
49
}
50
}()
51
log.WithField("addr", cfg.PrometheusAddr).Info("started Prometheus metrics server")
52
}
53
54
smith, err := agent.NewAgentSmith(cfg.Config)
55
if err != nil {
56
log.WithError(err).Fatal("cannot create agent smith")
57
}
58
59
err = reg.Register(smith)
60
if err != nil {
61
log.WithError(err).Fatal("cannot register metrics")
62
}
63
64
ctx := context.Background()
65
ctx, cancel := context.WithCancel(ctx)
66
defer cancel()
67
68
go smith.Start(ctx, func(violation agent.InfringingWorkspace, penalties []config.PenaltyKind) {
69
log.WithField("violation", violation).WithField("penalties", penalties).Info("Found violation")
70
})
71
72
if cfg.MaxSysMemMib > 0 {
73
go startMemoryWatchdog(cfg.MaxSysMemMib)
74
}
75
76
log.WithField("namespace", cfg.Namespace).Info("agent smith is up and running")
77
78
sigChan := make(chan os.Signal, 1)
79
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
80
81
select {
82
case <-ctx.Done():
83
return
84
case <-sigChan:
85
return
86
}
87
},
88
}
89
90
func init() {
91
rootCmd.AddCommand(runCmd)
92
}
93
94
func startMemoryWatchdog(maxSysMemMib uint64) {
95
t := time.NewTicker(30 * time.Second)
96
var m runtime.MemStats
97
for {
98
runtime.ReadMemStats(&m)
99
100
sysMemMib := m.Sys / 1024 / 1024
101
if sysMemMib > maxSysMemMib {
102
log.WithField("sysMemMib", sysMemMib).WithField("maxSysMemMib", maxSysMemMib).Fatal("reached maxmimum memory use - stopping")
103
}
104
105
<-t.C
106
}
107
}
108
109