-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathmain.go
More file actions
173 lines (144 loc) · 5.37 KB
/
main.go
File metadata and controls
173 lines (144 loc) · 5.37 KB
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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/openshift/sippy/pkg/bigquery/bqlabel"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openshift/sippy/pkg/bigquery"
"github.com/openshift/sippy/pkg/dataloader/prowloader/gcs"
"github.com/openshift/sippy/pkg/dataloader/prowloader/github"
"github.com/openshift/sippy/pkg/flags"
"github.com/openshift/sippy/pkg/github/commenter"
"github.com/openshift/sippy/pkg/sippyserver"
"github.com/openshift/sippy/pkg/version"
)
var logLevel = "info"
type SippyDaemonFlags struct {
BigQueryFlags *flags.BigQueryFlags
CacheFlags *flags.CacheFlags
DBFlags *flags.PostgresFlags
GoogleCloudFlags *flags.GoogleCloudFlags
GithubCommenterFlags *flags.GithubCommenterFlags
MetricsAddr string
}
func NewSippyDaemonFlags() *SippyDaemonFlags {
return &SippyDaemonFlags{
DBFlags: flags.NewPostgresDatabaseFlags(),
BigQueryFlags: flags.NewBigQueryFlags(),
CacheFlags: flags.NewCacheFlags(),
GithubCommenterFlags: flags.NewGithubCommenterFlags(),
GoogleCloudFlags: flags.NewGoogleCloudFlags(),
}
}
func (f *SippyDaemonFlags) BindFlags(fs *pflag.FlagSet) {
f.BigQueryFlags.BindFlags(fs)
f.CacheFlags.BindFlags(fs)
f.DBFlags.BindFlags(fs)
f.GithubCommenterFlags.BindFlags(fs)
f.GoogleCloudFlags.BindFlags(fs)
fs.StringVar(&f.MetricsAddr, "listen-metrics", f.MetricsAddr, "The address to serve prometheus metrics on (default :2112)")
}
func (f *SippyDaemonFlags) Validate() error {
return f.GoogleCloudFlags.Validate()
}
func NewSippyDaemonCommand() *cobra.Command {
f := NewSippyDaemonFlags()
// rootCmd represents the base command when called without any subcommands
cmd := &cobra.Command{
Use: "sippy-daemon",
Short: "Sippy daemon is used for on-going tasks like monitoring git repos for reporting risk analysis.",
PersistentPreRun: func(c *cobra.Command, args []string) {
fmt.Fprintf(os.Stdout, "sippy built from %s\n", version.Get().GitCommit)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := f.Validate(); err != nil {
return errors.WithMessage(err, "error validating options")
}
processes := make([]sippyserver.DaemonProcess, 0)
if f.GithubCommenterFlags.CommentProcessing {
dbc, err := f.DBFlags.GetDBClient()
if err != nil {
return err
}
githubClient := github.New(context.TODO(), github.OpenshiftOrg)
ghCommenter, err := commenter.NewGitHubCommenter(githubClient,
dbc, f.GithubCommenterFlags.ExcludeReposCommenting, f.GithubCommenterFlags.IncludeReposCommenting)
if err != nil {
log.WithError(err).Error("CRITICAL error initializing GitHub commenter which prevents PR commenting")
return nil
}
cacheClient, err := f.CacheFlags.GetCacheClient()
if err != nil {
return errors.WithMessage(err, "couldn't get cache client")
}
opCtx := bqlabel.OperationalContext{
App: bqlabel.AppSippy,
Command: "sippy-daemon",
Environment: bqlabel.EnvDaemon,
}
var bigQueryClient *bigquery.Client
bigQueryClient, err = f.BigQueryFlags.GetBigQueryClient(context.Background(), opCtx, cacheClient, f.GoogleCloudFlags.ServiceAccountCredentialFile)
if err != nil {
return errors.WithMessage(err, "couldn't get bigquery client")
}
gcsClient, err := gcs.NewGCSClient(context.TODO(),
f.GoogleCloudFlags.ServiceAccountCredentialFile,
f.GoogleCloudFlags.OAuthClientCredentialFile,
)
if err != nil {
log.WithError(err).Error("CRITICAL error getting GCS client which prevents PR commenting")
return nil
}
// we only process one comment every 5 seconds,
// 4 potential GitHub calls per comment gives us a safe buffer
// get comment data, get existing comments, possible delete existing, and adding the comment
// could lower to 3 seconds if we need, most writes likely won't have to delete
processes = append(processes, sippyserver.NewWorkProcessor(dbc, bigQueryClient, gcsClient.Bucket(f.GoogleCloudFlags.StorageBucket), cacheClient, ghCommenter, 10, 5*time.Minute, 5*time.Second, f.GithubCommenterFlags.CommentProcessingDryRun))
}
daemonServer := sippyserver.NewDaemonServer(processes)
// Serve our metrics endpoint for prometheus to scrape
if f.MetricsAddr != "" {
go func() {
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(f.MetricsAddr, nil) //nolint
if err != nil {
panic(err)
}
}()
}
daemonServer.Serve()
return nil
},
}
f.BindFlags(cmd.Flags())
return cmd
}
func main() {
// Set log level
level, err := log.ParseLevel(logLevel)
if err != nil {
log.WithError(err).Fatal("cannot parse log-level")
}
log.SetLevel(level)
log.Debug("debug logging enabled")
// Add some millisecond precision to log timestamps, useful for debugging performance.
formatter := new(log.TextFormatter)
formatter.TimestampFormat = "2006-01-02T15:04:05.999Z07:00"
formatter.FullTimestamp = true
formatter.DisableColors = false
log.SetFormatter(formatter)
cmd := NewSippyDaemonCommand()
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info",
"Log level (trace,debug,info,warn,error) (default info)")
err = cmd.Execute()
if err != nil {
log.WithError(err).Fatal("could not execute root command")
}
}