-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
264 lines (233 loc) · 9.5 KB
/
main.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/amazeeio/aergia-controller/controllers"
"github.com/amazeeio/aergia-controller/handlers/idler"
"github.com/amazeeio/aergia-controller/handlers/metrics"
"github.com/amazeeio/aergia-controller/handlers/unidler"
u "github.com/amazeeio/aergia-controller/handlers/unidler"
prometheusapi "github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/prometheus"
variables "github.com/uselagoon/machinery/utils/variables"
"gopkg.in/robfig/cron.v2"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
prometheus.MustRegister(requestCount)
prometheus.MustRegister(requestDuration)
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var debug bool
var refreshInterval int
var unidlerHTTPPort int
var dryRun bool
var selectorsFile string
var skipHitCheck bool
var cliCron string // interval for the cli idler.
var serviceCron string // interval for the service idler.
var prometheusAddress string
var prometheusCheckInterval string
var podCheckInterval string
var enableCLIIdler bool
var enableServiceIdler bool
var verifiedUnidling bool
var verifiedSecret string
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&debug, "debug", false, "Enable more verbose debug logs.")
flag.IntVar(&refreshInterval, "refresh-interval", 30,
"The default refresh interval for the unidle page to use when unidling a namespace.")
flag.BoolVar(&dryRun, "dry-run", false,
"Dry run will not perform any idling, just log the actions it would take.")
flag.StringVar(&selectorsFile, "selectors", "resources/selectors.yaml",
"The path to the file containing the label selectors for idling.")
flag.StringVar(&cliCron, "cli-idler-cron", "5,35 * * * *",
"The cron definition for how often to run the cli idling process.")
flag.StringVar(&serviceCron, "service-idler-cron", "0 */4 * * *",
"The cron definition for how often to run the service idling process.")
flag.StringVar(&prometheusAddress, "prometheus-endpoint", "http://monitoring-kube-prometheus-prometheus.monitoring.svc:9090",
"The address for the prometheus endpoint to check against")
flag.StringVar(&prometheusCheckInterval, "prometheus-interval", "4h",
"The time range interval for how long to check prometheus for (default: 4h)")
flag.StringVar(&podCheckInterval, "pod-check-interval", "4h",
"The time range interval for how long to check pod update (default: 4h)")
flag.BoolVar(&skipHitCheck, "skip-hit-check", false,
"Flag to determine if the idler should check the hit backend or not. If true, this overrides what is in the selectors file.")
flag.BoolVar(&enableCLIIdler, "enable-cli-idler", true, "Flag to enable cli idler.")
flag.BoolVar(&enableServiceIdler, "enable-service-idler", true, "Flag to enable service idler.")
flag.BoolVar(&verifiedUnidling, "verified-unidling", false,
"Flag to enable unidling requests to verify that they are a browser by having the first request do a callback to Aergia with verification.")
flag.StringVar(&verifiedSecret, "verify-secret", "super-secret-string",
"The secret to use for verifying unidling requests.")
flag.IntVar(&unidlerHTTPPort, "unidler-port", 5000, "Port for the unidler service to listen on.")
flag.Parse()
selectorsFile = variables.GetEnv("SELECTORS_YAML_FILE", selectorsFile)
verifiedUnidling = variables.GetEnvBool("VERIFIED_UNIDLING", verifiedUnidling)
verifiedSecret = variables.GetEnv("VERIFY_SECRET", verifiedSecret)
dryRun = variables.GetEnvBool("DRY_RUN", dryRun)
unidlerHTTPPort = variables.GetEnvInt("UNIDLER_PORT", unidlerHTTPPort)
cliCron = variables.GetEnv("CLI_CRON", cliCron)
serviceCron = variables.GetEnv("SERVICE_CRON", serviceCron)
enableServiceIdler = variables.GetEnvBool("ENABLE_SERVICE_IDLER", enableServiceIdler)
enableCLIIdler = variables.GetEnvBool("ENABLE_CLI_IDLER", enableCLIIdler)
podCheckInterval = variables.GetEnv("POD_CHECK_INTERVAL", podCheckInterval)
timePodCheckInterval, err := time.ParseDuration(podCheckInterval)
if err != nil {
// if the first parse fails, it may be because the user is using a single integer hour value from a previous release
// this handles the conversion from the previous integer value to the new time.Duration value support.
timePodCheckInterval, err = time.ParseDuration(fmt.Sprintf("%sh", podCheckInterval))
if err != nil {
setupLog.Error(err, "unable to decode pod check interval")
os.Exit(1)
}
}
prometheusAddress = variables.GetEnv("PROMETHEUS_ADDRESS", prometheusAddress)
prometheusCheckInterval = variables.GetEnv("PROMETHEUS_CHECK_INTERVAL", prometheusCheckInterval)
timePrometheusCheckInterval, err := time.ParseDuration(prometheusCheckInterval)
if err != nil {
setupLog.Error(err, "unable to decode prometheus check interval")
os.Exit(1)
}
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))
// read the selector file into idlerdata struct.
file, err := os.Open(selectorsFile)
if err != nil {
setupLog.Error(err, "unable to open selectors file")
os.Exit(1)
}
defer file.Close()
d := yaml.NewDecoder(file)
selectors := &idler.Data{}
if err := d.Decode(&selectors); err != nil {
setupLog.Error(err, "unable to decode selectors yaml")
os.Exit(1)
}
if skipHitCheck {
selectors.Service.SkipHitCheck = skipHitCheck
}
if dryRun {
setupLog.Info("dry-run enabled")
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "aergia-unidler-leader-election-helper",
Port: 9443,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// if a blockedagents file is found, provide them to the unidler to block agents from unidling environments
// provides nil if no file found
allowedAgents, _ := unidler.ReadSliceFromFile("/lists/allowedagents")
blockedAgents, _ := unidler.ReadSliceFromFile("/lists/blockedagents")
allowedIPs, _ := unidler.ReadSliceFromFile("/lists/allowedips")
blockedIPs, _ := unidler.ReadSliceFromFile("/lists/blockedips")
unidler := &unidler.Unidler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("aergia-controller").WithName("Unidler"),
RefreshInterval: refreshInterval,
Debug: debug,
RequestCount: requestCount,
RequestDuration: requestDuration,
AllowedUserAgents: allowedAgents,
BlockedUserAgents: blockedAgents,
AllowedIPs: allowedIPs,
BlockedIPs: blockedIPs,
UnidlerHTTPPort: unidlerHTTPPort,
VerifiedUnidling: verifiedUnidling,
VerifiedSecret: verifiedSecret,
}
prometheusClient, err := prometheusapi.NewClient(prometheusapi.Config{
Address: prometheusAddress,
})
if err != nil {
setupLog.Error(err, "error creating prometheus client")
os.Exit(1)
}
// setup the idler with the k8s and lagoon clients
idler := &idler.Idler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("aergia-controller").WithName("ServiceIdler"),
PodCheckInterval: timePodCheckInterval,
PrometheusClient: prometheusClient,
PrometheusCheckInterval: timePrometheusCheckInterval,
DryRun: dryRun,
Debug: debug,
Selectors: selectors,
}
// Set up the cron job intervals for the CLI and service idlers.
c := cron.New()
// add the cronjobs we need.
// CLI Idler
if enableCLIIdler {
setupLog.Info("starting cli idler")
c.AddFunc(cliCron, func() {
idler.CLIIdler()
})
}
// Service Idler
if enableServiceIdler {
setupLog.Info("starting service idler")
c.AddFunc(serviceCron, func() {
idler.ServiceIdler()
})
}
// start crons.
c.Start()
// +kubebuilder:scaffold:builder
setupLog.Info("starting unidler listening")
go u.Run(unidler, setupLog)
if err = (&controllers.IdlingReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Idling"),
Scheme: mgr.GetScheme(),
Idler: idler,
Unidler: unidler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Idling")
os.Exit(1)
}
setupLog.Info("starting aergia metrics server")
m := metrics.NewServer(setupLog, ":9912")
defer m.Shutdown(context.Background())
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}