@@ -18,6 +18,7 @@ package main
18
18
19
19
import (
20
20
"fmt"
21
+ "github.com/fluxcd/helm-controller/internal/oomwatch"
21
22
"os"
22
23
"time"
23
24
@@ -84,25 +85,41 @@ func main() {
84
85
aclOptions acl.Options
85
86
leaderElectionOptions leaderelection.Options
86
87
rateLimiterOptions helper.RateLimiterOptions
88
+ oomWatchInterval time.Duration
89
+ oomWatchMemoryThreshold float64
87
90
)
88
91
89
- flag .StringVar (& metricsAddr , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
90
- flag .StringVar (& eventsAddr , "events-addr" , "" , "The address of the events receiver." )
91
- flag .StringVar (& healthAddr , "health-addr" , ":9440" , "The address the health endpoint binds to." )
92
- flag .IntVar (& concurrent , "concurrent" , 4 , "The number of concurrent HelmRelease reconciles." )
93
- flag .DurationVar (& requeueDependency , "requeue-dependency" , 30 * time .Second , "The interval at which failing dependencies are reevaluated." )
94
- flag .DurationVar (& gracefulShutdownTimeout , "graceful-shutdown-timeout" , 600 * time .Second , "The duration given to the reconciler to finish before forcibly stopping." )
92
+ flag .StringVar (& metricsAddr , "metrics-addr" , ":8080" ,
93
+ "The address the metric endpoint binds to." )
94
+ flag .StringVar (& eventsAddr , "events-addr" , "" ,
95
+ "The address of the events receiver." )
96
+ flag .StringVar (& healthAddr , "health-addr" , ":9440" ,
97
+ "The address the health endpoint binds to." )
98
+ flag .IntVar (& concurrent , "concurrent" , 4 ,
99
+ "The number of concurrent HelmRelease reconciles." )
100
+ flag .DurationVar (& requeueDependency , "requeue-dependency" , 30 * time .Second ,
101
+ "The interval at which failing dependencies are reevaluated." )
102
+ flag .DurationVar (& gracefulShutdownTimeout , "graceful-shutdown-timeout" , 600 * time .Second ,
103
+ "The duration given to the reconciler to finish before forcibly stopping." )
95
104
flag .BoolVar (& watchAllNamespaces , "watch-all-namespaces" , true ,
96
105
"Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace." )
97
- flag .IntVar (& httpRetry , "http-retry" , 9 , "The maximum number of retries when failing to fetch artifacts over HTTP." )
98
- flag .StringVar (& intkube .DefaultServiceAccountName , "default-service-account" , "" , "Default service account used for impersonation." )
106
+ flag .IntVar (& httpRetry , "http-retry" , 9 ,
107
+ "The maximum number of retries when failing to fetch artifacts over HTTP." )
108
+ flag .StringVar (& intkube .DefaultServiceAccountName , "default-service-account" , "" ,
109
+ "Default service account used for impersonation." )
110
+ flag .Float64Var (& oomWatchMemoryThreshold , "oom-watch-memory-threshold" , 95 ,
111
+ "The memory threshold in percentage at which the OOM watcher will trigger a graceful shutdown. Requires feature gate 'OOMWatch' to be enabled." )
112
+ flag .DurationVar (& oomWatchInterval , "oom-watch-interval" , 500 * time .Millisecond ,
113
+ "The interval at which the OOM watcher will check for memory usage. Requires feature gate 'OOMWatch' to be enabled." )
114
+
99
115
clientOptions .BindFlags (flag .CommandLine )
100
116
logOptions .BindFlags (flag .CommandLine )
101
117
aclOptions .BindFlags (flag .CommandLine )
102
118
leaderElectionOptions .BindFlags (flag .CommandLine )
103
119
rateLimiterOptions .BindFlags (flag .CommandLine )
104
120
kubeConfigOpts .BindFlags (flag .CommandLine )
105
121
featureGates .BindFlags (flag .CommandLine )
122
+
106
123
flag .Parse ()
107
124
108
125
ctrl .SetLogger (logger .NewLogger (logOptions ))
@@ -122,7 +139,7 @@ func main() {
122
139
watchNamespace = os .Getenv ("RUNTIME_NAMESPACE" )
123
140
}
124
141
125
- disableCacheFor := []ctrlclient.Object {}
142
+ var disableCacheFor []ctrlclient.Object
126
143
shouldCache , err := features .Enabled (features .CacheSecretsAndConfigMaps )
127
144
if err != nil {
128
145
setupLog .Error (err , "unable to check feature gate CacheSecretsAndConfigMaps" )
@@ -190,8 +207,19 @@ func main() {
190
207
}
191
208
// +kubebuilder:scaffold:builder
192
209
210
+ ctx := ctrl .SetupSignalHandler ()
211
+ if ok , _ := features .Enabled (features .OOMWatch ); ok {
212
+ setupLog .Info ("setting up OOM watcher" )
213
+ ow , err := oomwatch .NewDefault (oomWatchMemoryThreshold , oomWatchInterval , ctrl .Log .WithName ("OOMwatch" ))
214
+ if err != nil {
215
+ setupLog .Error (err , "unable to setup OOM watcher" )
216
+ os .Exit (1 )
217
+ }
218
+ ctx = ow .Watch (ctx )
219
+ }
220
+
193
221
setupLog .Info ("starting manager" )
194
- if err := mgr .Start (ctrl . SetupSignalHandler () ); err != nil {
222
+ if err := mgr .Start (ctx ); err != nil {
195
223
setupLog .Error (err , "problem running manager" )
196
224
os .Exit (1 )
197
225
}
0 commit comments