-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreloader.go
More file actions
389 lines (330 loc) · 11.1 KB
/
reloader.go
File metadata and controls
389 lines (330 loc) · 11.1 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2025 Preferred Networks, Inc.
//
// 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"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func run(ctx context.Context, controllerName string, cfg *ProcessManagerConfig) error {
mgr, err := manager.New(cfg.kubeConfig, manager.Options{
Metrics: server.Options{BindAddress: "0"},
// Disable metrics and health check endpoints.
// Wrapped process should be responsible for them.
HealthProbeBindAddress: "0",
})
if err != nil {
return fmt.Errorf("create manager: %w", err)
}
procMgr := NewProcessManager(cfg)
if addErr := mgr.Add(procMgr); addErr != nil {
return fmt.Errorf("failed to add process manager: %w", addErr)
}
nsLbsl, err := metav1.ParseToLabelSelector(cfg.namespaceSelector)
if err != nil {
return fmt.Errorf("parse label selector from %s: %w", cfg.namespaceSelector, err)
}
nsLblPredicate, err := predicate.LabelSelectorPredicate(*nsLbsl)
if err != nil {
return fmt.Errorf("create namespace label predicate: %w", err)
}
nsSelector, err := metav1.LabelSelectorAsSelector(nsLbsl)
if err != nil {
return fmt.Errorf("convert label selector: %w", err)
}
if err := builder.ControllerManagedBy(mgr).
Named(controllerName).
For(&corev1.Namespace{}).
WithEventFilter(nsLblPredicate).
Complete(&NamespaceWatcher{
client: mgr.GetClient(),
nsSelector: nsSelector,
processManager: procMgr,
}); err != nil {
return fmt.Errorf("build controller: %w", err)
}
return mgr.Start(ctx)
}
// ProcessManagerConfig holds the configuration for [ProcessManager].
type ProcessManagerConfig struct {
logger logr.Logger
arguments []string
kubeConfig *rest.Config
namespaceSelector string
targetEnvVar string
terminationGracePeriod time.Duration
sigkillTimeout time.Duration
debouncePeriod time.Duration
}
// ProcessManager manages the lifecycle of a wrapped process,
type ProcessManager struct {
log logr.Logger
targetEnvVar string
arguments []string
terminationGracePeriod time.Duration
sigkillTimeout time.Duration
debouncePeriod time.Duration
// process holds the state of the currently running process. Subfields are
// protected by the embedded mutex.
process struct {
sync.RWMutex
// cmd is the currently running command (sub-process). It is nil if no
// process is running.
cmd *exec.Cmd
// done is closed when the process has exited.
done chan struct{}
}
// watchNamespaces holds the currently requested namespaces to watch.
watchNamespaces atomic.Value
// updateChan is used to signal that the watched namespaces have changed.
updateChan chan struct{}
}
// NewProcessManager creates a new [ProcessManager] with the given configuration.
func NewProcessManager(cfg *ProcessManagerConfig) *ProcessManager {
var watchNamespaces atomic.Value
// NOTE: Initialize with empty string to avoid nil dereference.
// This should still be set by Reconcile before use.
watchNamespaces.Store("")
pm := &ProcessManager{
arguments: cfg.arguments,
log: cfg.logger,
targetEnvVar: cfg.targetEnvVar,
watchNamespaces: watchNamespaces,
terminationGracePeriod: cfg.terminationGracePeriod,
sigkillTimeout: cfg.sigkillTimeout,
debouncePeriod: cfg.debouncePeriod,
updateChan: make(chan struct{}, 1),
}
// Initialize the process done channel as closed to indicate no process is
// running.
pm.process.done = make(chan struct{})
close(pm.process.done)
return pm
}
// UpdateNamespaces updates the namespaces to watch and triggers a process
// restart. This method may be called concurrently by Reconcile.
func (pm *ProcessManager) UpdateNamespaces(ns string) {
// Update the requested namespaces to watch.
if pm.watchNamespaces.Swap(ns) == ns {
return
}
select {
case pm.updateChan <- struct{}{}:
default:
}
}
// Start starts the process manager loop.
func (pm *ProcessManager) Start(ctx context.Context) error {
// Start the wrapped process immediately.
debounceTimer := time.NewTimer(0)
// Prevent the timer from firing immediately. We need to wait for the
// initial set of namespaces to be provided before starting the process.
// As of Go 1.23, the C channel will block after Stop is called.
debounceTimer.Stop()
lastReload := time.Now()
for {
select {
case <-ctx.Done():
pm.log.Info("Shutting down process manager")
return pm.stopProcess()
case <-debounceTimer.C:
curNS := os.Getenv(pm.targetEnvVar)
newNS := pm.watchNamespaces.Load().(string)
select {
case <-pm.Done():
// Process is not running. Restart is needed.
default:
// Process is running. If the namespaces are unchanged, do
// nothing.
if curNS == newNS {
continue // already current.
}
}
// Update the currently watched namespaces.
if err := os.Setenv(pm.targetEnvVar, newNS); err != nil {
return fmt.Errorf("failed to set %s: %w", pm.targetEnvVar, err)
}
if err := pm.stopProcess(); err != nil {
return err
}
if err := pm.startProcess(); err != nil {
return err
}
lastReload = time.Now()
case <-pm.updateChan:
var waitPeriod time.Duration
select {
case <-pm.Done():
// Process is not running. Restart immediately.
waitPeriod = 0
default:
// Process is running. Wait for the debounce period to elapse
// since the last reload.
waitPeriod = max(pm.debouncePeriod-time.Since(lastReload), 0)
}
// We can ignore the previous timer completely.
_ = debounceTimer.Reset(waitPeriod)
pm.log.Info("Namespace update received; scheduling restart", "waitPeriod", waitPeriod)
}
}
}
// currentPID returns the PID of the currently running process, or 0 if no
// process is running.
func (pm *ProcessManager) currentPID() int {
pm.process.RLock()
defer pm.process.RUnlock()
if pm.process.cmd == nil {
return 0
}
return pm.process.cmd.Process.Pid
}
// Done returns a channel that is closed when the wrapped process exits.
func (pm *ProcessManager) Done() chan struct{} {
pm.process.RLock()
defer pm.process.RUnlock()
return pm.process.done
}
// startProcess starts the wrapped process and returns immediately.
func (pm *ProcessManager) startProcess() error {
pm.process.Lock()
defer pm.process.Unlock()
// This may occur if startProcess is called concurrently with
// `waitForProcess`. This shouldn't happen.
if pm.process.cmd != nil {
panic(fmt.Sprintf("process %d is already started; this is a bug, please submit an issue at %s", pm.process.cmd.Process.Pid, issuesURL))
}
pm.process.cmd = exec.Command(pm.arguments[0], pm.arguments[1:]...)
pm.process.cmd.Stdout = os.Stdout
pm.process.cmd.Stderr = os.Stderr
if err := pm.process.cmd.Start(); err != nil {
return fmt.Errorf("failed to start process: %w", err)
}
pm.process.done = make(chan struct{})
pm.log.Info("Process started", "pid", pm.process.cmd.Process.Pid, "namespaces", os.Getenv(pm.targetEnvVar))
go pm.waitForProcess(pm.process.cmd, pm.process.done)
return nil
}
// waitForProcess waits for the wrapped process to exit and cleans up the
// state accordingly. This method will run concurrently with
// [ProcessManager.startProcess] and [ProcessManager.stopProcess].
func (pm *ProcessManager) waitForProcess(cmd *exec.Cmd, done chan struct{}) {
pm.log.Info("Waiting for process", "pid", cmd.Process.Pid)
err := cmd.Wait()
if err != nil {
pm.log.Error(err, "Process exited with error", "pid", cmd.Process.Pid)
} else {
pm.log.Info("Process exited", "pid", cmd.Process.Pid)
}
// The process has terminated. Clean up the cmd resource.
pm.process.Lock()
pm.process.cmd = nil
pm.process.Unlock()
// Signal that the process has exited.
close(done)
// Request that the process be restarted.
select {
case pm.updateChan <- struct{}{}:
default:
}
}
// stopProcess stops the wrapped process gracefully. stopProcess should not be
// running concurrently with [ProcessManager.startProcess].
func (pm *ProcessManager) stopProcess() error {
select {
case <-pm.Done():
// Process is already stopped.
return nil
default:
}
pid := pm.currentPID()
pm.log.Info("Stopping process", "pid", pid)
if err := pm.sendSignal(syscall.SIGTERM); err != nil {
return err
}
select {
case <-pm.Done():
// Process is already stopped.
return nil
case <-time.After(pm.terminationGracePeriod):
}
pm.log.Info("Process did not stop gracefully, sending SIGKILL", "pid", pid)
if err := pm.sendSignal(syscall.SIGKILL); err != nil {
return err
}
select {
case <-pm.Done():
// Process is already stopped.
return nil
case <-time.After(pm.sigkillTimeout):
}
// The process is not responding to SIGKILL. Completely bail out.
return fmt.Errorf("process %d did not respond to SIGKILL after %s", pid, pm.sigkillTimeout)
}
// sendSignal sends the given signal to terminate the wrapped process. If the
// process is not running, this is a no-op.
func (pm *ProcessManager) sendSignal(sig os.Signal) error {
pm.process.RLock()
defer pm.process.RUnlock()
if pm.process.cmd == nil {
return nil
}
if err := pm.process.cmd.Process.Signal(sig); err != nil {
if processExistsErr := pm.process.cmd.Process.Signal(syscall.Signal(0)); processExistsErr == nil {
// The process exists but we failed to send a signal.
// Something very wrong has happened.
return fmt.Errorf("failed to send %v to pid %d: %w", sig, pm.process.cmd.Process.Pid, err)
}
}
return nil
}
// NamespaceWatcher watches namespace resources and notifies the ProcessManager
type NamespaceWatcher struct {
client client.Client
nsSelector labels.Selector
processManager *ProcessManager
}
// Reconcile reconciles the namespace resources.
func (r *NamespaceWatcher) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var nslist corev1.NamespaceList
if err := r.client.List(ctx, &nslist, &client.ListOptions{
LabelSelector: r.nsSelector,
}); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to list namespaces: %w", err)
}
var namespaces []string
for _, ns := range nslist.Items {
namespaces = append(namespaces, ns.Name)
}
sort.Strings(namespaces)
r.processManager.UpdateNamespaces(strings.Join(namespaces, ","))
return reconcile.Result{}, nil
}