-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
354 lines (300 loc) · 12 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Copyright 2021.
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"
"github.com/medik8s/poison-pill/pkg/utils"
"os"
"strconv"
"time"
"github.com/pkg/errors"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
machinev1beta1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
poisonpillv1alpha1 "github.com/medik8s/poison-pill/api/v1alpha1"
"github.com/medik8s/poison-pill/controllers"
"github.com/medik8s/poison-pill/pkg/apicheck"
"github.com/medik8s/poison-pill/pkg/certificates"
"github.com/medik8s/poison-pill/pkg/peerhealth"
"github.com/medik8s/poison-pill/pkg/peers"
"github.com/medik8s/poison-pill/pkg/reboot"
"github.com/medik8s/poison-pill/pkg/watchdog"
//+kubebuilder:scaffold:imports
)
const (
nodeNameEnvVar = "MY_NODE_NAME"
peerHealthDefaultPort = 30001
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(poisonpillv1alpha1.AddToScheme(scheme))
utilruntime.Must(machinev1beta1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var isManager bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&isManager, "is-manager", false,
"Used to differentiate between the poison pill agents that runs in a daemonset to the 'manager' that only"+
"reconciles the config CRD and installs the DS")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "547f6cb6.medik8s.io",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if isManager {
initPoisonPillManager(mgr)
} else {
initPoisonPillAgent(mgr)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func initPoisonPillManager(mgr manager.Manager) {
setupLog.Info("Starting as a manager that installs the daemonset")
ns, err := getDeploymentNamespace()
if err != nil {
setupLog.Error(err, "failed to get deployed namespace from env var")
os.Exit(1)
}
if err := (&controllers.PoisonPillConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("PoisonPillConfig"),
Scheme: mgr.GetScheme(),
InstallFileFolder: "./install",
DefaultPpcCreator: newConfigIfNotExist,
Namespace: ns,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PoisonPillConfig")
os.Exit(1)
}
if err := newConfigIfNotExist(mgr.GetClient()); err != nil {
setupLog.Error(err, "failed to create a default poison pill config CR")
os.Exit(1)
}
if err := newDefaultTemplateIfNotExist(mgr.GetClient()); err != nil {
setupLog.Error(err, "failed to create default remediation template")
os.Exit(1)
}
}
func getDurEnvVarOrDie(varName string) time.Duration {
intVar := getIntEnvVarOrDie(varName)
return time.Duration(intVar)
}
func getIntEnvVarOrDie(varName string) int {
// determine safe reboot time
varVal := os.Getenv(varName)
intVar, err := strconv.Atoi(varVal)
if err != nil {
setupLog.Error(err, "failed to convert env variable to int", "var name", varName, "var value", varVal)
os.Exit(1)
}
return intVar
}
func initPoisonPillAgent(mgr manager.Manager) {
setupLog.Info("Starting as a poison pill agent that should run as part of the daemonset")
myNodeName := os.Getenv(nodeNameEnvVar)
if myNodeName == "" {
setupLog.Error(errors.New("failed to get own node name"), "node name was empty",
"env var name", nodeNameEnvVar)
}
ns, err := getDeploymentNamespace()
if err != nil {
setupLog.Error(err, "unable to get the deployment namespace")
os.Exit(1)
}
wasWatchdogInitiated := false
wd, err := watchdog.NewLinux(ctrl.Log.WithName("watchdog"))
if err != nil {
setupLog.Error(err, "failed to init watchdog, using soft reboot")
}
if wd != nil {
if err = mgr.Add(wd); err != nil {
setupLog.Error(err, "failed to add watchdog to the manager")
os.Exit(1)
}
wasWatchdogInitiated = true
}
if err = utils.UpdateNodeWithIsRebootCapableAnnotation(wasWatchdogInitiated, myNodeName, mgr); err != nil {
setupLog.Error(err, "failed to update node's annotation", "annotation", utils.IsRebootCapableAnnotation)
os.Exit(1)
}
// it's fine when the watchdog is nil!
rebooter := reboot.NewWatchdogRebooter(wd, ctrl.Log.WithName("rebooter"))
// TODO make the interval configurable
peerUpdateInterval := getDurEnvVarOrDie("PEER_UPDATE_INTERVAL")
peerApiServerTimeout := getDurEnvVarOrDie("PEER_API_SERVER_TIMEOUT")
myPeers := peers.New(myNodeName, peerUpdateInterval, mgr.GetClient(), ctrl.Log.WithName("peers"), peerApiServerTimeout)
if err = mgr.Add(myPeers); err != nil {
setupLog.Error(err, "failed to add peers to the manager")
os.Exit(1)
}
// TODO make the interval and error threshold configurable?
apiCheckInterval := getDurEnvVarOrDie("API_CHECK_INTERVAL") //the frequency for api-server connectivity check
maxErrorThreshold := getIntEnvVarOrDie("MAX_API_ERROR_THRESHOLD") //after this threshold, the node will start contacting its peers
apiServerTimeout := getDurEnvVarOrDie("API_SERVER_TIMEOUT") //timeout for each api-connectivity check
peerDialTimeout := getDurEnvVarOrDie("PEER_DIAL_TIMEOUT") //timeout for establishing connection to peer
peerRequestTimeout := getDurEnvVarOrDie("PEER_REQUEST_TIMEOUT") //timeout for each peer request
// init certificate reader
certReader := certificates.NewSecretCertStorage(mgr.GetClient(), ctrl.Log.WithName("SecretCertStorage"), ns)
apiConnectivityCheckConfig := &apicheck.ApiConnectivityCheckConfig{
Log: ctrl.Log.WithName("api-check"),
MyNodeName: myNodeName,
CheckInterval: apiCheckInterval,
MaxErrorsThreshold: maxErrorThreshold,
Peers: myPeers,
Rebooter: rebooter,
Cfg: mgr.GetConfig(),
CertReader: certReader,
ApiServerTimeout: apiServerTimeout,
PeerDialTimeout: peerDialTimeout,
PeerRequestTimeout: peerRequestTimeout,
PeerHealthPort: peerHealthDefaultPort,
}
apiChecker := apicheck.New(apiConnectivityCheckConfig)
if err = mgr.Add(apiChecker); err != nil {
setupLog.Error(err, "failed to add api-check to the manager")
os.Exit(1)
}
// determine safe reboot time
timeToAssumeNodeRebooted := getDurEnvVarOrDie("TIME_TO_ASSUME_NODE_REBOOTED")
// but the reboot time needs be at least the time we know we need for determining a node issue and trigger the reboot!
// 1. time for determing node issue
minTimeToAssumeNodeRebooted := (apiCheckInterval + apiServerTimeout) * time.Duration(maxErrorThreshold)
// 2. time for asking peers (10% batches + 1st smaller batch)
minTimeToAssumeNodeRebooted += (10 + 1) * (peerDialTimeout + peerRequestTimeout)
// 3. watchdog timeout
if wd != nil {
minTimeToAssumeNodeRebooted += wd.GetTimeout()
}
// 4. some buffer
minTimeToAssumeNodeRebooted += 15 * time.Second
if timeToAssumeNodeRebooted < minTimeToAssumeNodeRebooted {
timeToAssumeNodeRebooted = minTimeToAssumeNodeRebooted
}
setupLog.Info("Time to assume that unhealthy node has been rebooted", "time", timeToAssumeNodeRebooted)
restoreNodeAfter := 90 * time.Second
pprReconciler := &controllers.PoisonPillRemediationReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("PoisonPillRemediation"),
Scheme: mgr.GetScheme(),
Rebooter: rebooter,
SafeTimeToAssumeNodeRebooted: timeToAssumeNodeRebooted,
MyNodeName: myNodeName,
RestoreNodeAfter: restoreNodeAfter,
}
if err = pprReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PoisonPillRemediation")
os.Exit(1)
}
setupLog.Info("init grpc server")
// TODO make port configurable?
server, err := peerhealth.NewServer(pprReconciler, mgr.GetConfig(), ctrl.Log.WithName("peerhealth").WithName("server"), peerHealthDefaultPort, certReader)
if err != nil {
setupLog.Error(err, "failed to init grpc server")
os.Exit(1)
}
if err = mgr.Add(server); err != nil {
setupLog.Error(err, "failed to add grpc server to the manager")
os.Exit(1)
}
}
// newConfigIfNotExist creates a new PoisonPillConfig object
// to initialize the rest of the deployment objects creation.
func newConfigIfNotExist(c client.Client) error {
ns, err := getDeploymentNamespace()
if err != nil {
return errors.Wrap(err, "unable to get the deployment namespace")
}
config := poisonpillv1alpha1.NewDefaultPoisonPillConfig()
config.SetNamespace(ns)
err = c.Create(context.Background(), &config, &client.CreateOptions{})
if err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrap(err, "failed to create a default poison pill config CR")
}
return nil
}
// newDefaultTemplateIfNotExist creates a new PoisonPillRemediationTemplate object
func newDefaultTemplateIfNotExist(c client.Client) error {
ns, err := getDeploymentNamespace()
if err != nil {
return errors.Wrap(err, "unable to get the deployment namespace")
}
pprt := poisonpillv1alpha1.NewDefaultRemediationTemplate()
pprt.SetNamespace(ns)
err = c.Create(context.Background(), &pprt, &client.CreateOptions{})
if err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrap(err, "failed to create a default poison pill template CR")
}
return nil
}
// getDeploymentNamespace returns the Namespace this operator is deployed on.
func getDeploymentNamespace() (string, error) {
// deployNamespaceEnvVar is the constant for env variable DEPLOYMENT_NAMESPACE
// which specifies the Namespace to watch.
// An empty value means the operator is running with cluster scope.
var deployNamespaceEnvVar = "DEPLOYMENT_NAMESPACE"
ns, found := os.LookupEnv(deployNamespaceEnvVar)
if !found {
return "", fmt.Errorf("%s must be set", deployNamespaceEnvVar)
}
return ns, nil
}