Skip to content

Commit 724f615

Browse files
committed
adding additional updates with Reload fnction tests
1 parent 8c396cd commit 724f615

10 files changed

+895
-177
lines changed

internal/mode/static/manager.go

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ func StartManager(cfg config.Config) error {
210210
ngxPlusClient,
211211
ngxruntimeCollector,
212212
cfg.Logger.WithName("nginxRuntimeManager"),
213+
nil,
214+
nil,
213215
),
214216
statusUpdater: statusUpdater,
215217
eventRecorder: recorder,

internal/mode/static/nginx/runtime/manager.go

+51-19
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,33 @@ import (
1717
)
1818

1919
const (
20-
pidFile = "/var/run/nginx/nginx.pid"
20+
PidFile = "/var/run/nginx/nginx.pid"
2121
pidFileTimeout = 10000 * time.Millisecond
2222
nginxReloadTimeout = 60000 * time.Millisecond
2323
)
2424

2525
type (
26-
readFileFunc func(string) ([]byte, error)
27-
checkFileFunc func(string) (fs.FileInfo, error)
26+
ReadFileFunc func(string) ([]byte, error)
27+
CheckFileFunc func(string) (fs.FileInfo, error)
2828
)
2929

3030
var childProcPathFmt = "/proc/%[1]v/task/%[1]v/children"
3131

32-
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . NginxPlusClient
32+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . nginxPlusClient
3333

34-
type NginxPlusClient interface {
34+
type nginxPlusClient interface {
3535
UpdateHTTPServers(upstream string, servers []ngxclient.UpstreamServer) (added []ngxclient.UpstreamServer, deleted []ngxclient.UpstreamServer, updated []ngxclient.UpstreamServer, err error)
3636
GetUpstreams() (*ngxclient.Upstreams, error)
3737
}
3838

39+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . processHandler
40+
41+
type processHandler interface {
42+
FindMainProcess(ctx context.Context, checkFile CheckFileFunc, readFile ReadFileFunc, timeout time.Duration) (int, error)
43+
ReadFile(file string) ([]byte, error)
44+
SysCallKill(pid int, signum syscall.Signal) error
45+
}
46+
3947
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
4048

4149
// Manager manages the runtime of NGINX.
@@ -52,6 +60,8 @@ type Manager interface {
5260
GetUpstreams() (ngxclient.Upstreams, error)
5361
}
5462

63+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MetricsCollector
64+
5565
// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
5666
type MetricsCollector interface {
5767
IncReloadCount()
@@ -61,23 +71,27 @@ type MetricsCollector interface {
6171

6272
// ManagerImpl implements Manager.
6373
type ManagerImpl struct {
64-
verifyClient *verifyClient
74+
verifyClient verifyClient
6575
metricsCollector MetricsCollector
66-
ngxPlusClient NginxPlusClient
76+
ngxPlusClient nginxPlusClient
6777
logger logr.Logger
78+
process processHandler
6879
}
6980

7081
// NewManagerImpl creates a new ManagerImpl.
7182
func NewManagerImpl(
72-
ngxPlusClient NginxPlusClient,
83+
ngxPlusClient nginxPlusClient,
7384
collector MetricsCollector,
7485
logger logr.Logger,
86+
process processHandler,
87+
verifyClient verifyClient,
7588
) *ManagerImpl {
7689
return &ManagerImpl{
77-
verifyClient: newVerifyClient(nginxReloadTimeout),
90+
verifyClient: verifyClient,
7891
metricsCollector: collector,
7992
ngxPlusClient: ngxPlusClient,
8093
logger: logger,
94+
process: process,
8195
}
8296
}
8397

@@ -89,25 +103,25 @@ func (m *ManagerImpl) IsPlus() bool {
89103
func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
90104
start := time.Now()
91105
// We find the main NGINX PID on every reload because it will change if the NGINX container is restarted.
92-
pid, err := findMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout)
106+
pid, err := m.process.FindMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout)
93107
if err != nil {
94108
return fmt.Errorf("failed to find NGINX main process: %w", err)
95109
}
96110

97111
childProcFile := fmt.Sprintf(childProcPathFmt, pid)
98-
previousChildProcesses, err := os.ReadFile(childProcFile)
112+
previousChildProcesses, err := m.process.ReadFile(childProcFile)
99113
if err != nil {
100114
return err
101115
}
102116

103117
// send HUP signal to the NGINX main process reload configuration
104118
// See https://nginx.org/en/docs/control.html
105-
if err := syscall.Kill(pid, syscall.SIGHUP); err != nil {
119+
if err := m.process.SysCallKill(pid, syscall.SIGHUP); err != nil {
106120
m.metricsCollector.IncReloadErrors()
107121
return fmt.Errorf("failed to send the HUP signal to NGINX main: %w", err)
108122
}
109123

110-
if err = m.verifyClient.waitForCorrectVersion(
124+
if err = m.verifyClient.WaitForCorrectVersion(
111125
ctx,
112126
configVersion,
113127
childProcFile,
@@ -160,16 +174,16 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
160174

161175
// EnsureNginxRunning ensures NGINX is running by locating the main process.
162176
func EnsureNginxRunning(ctx context.Context) error {
163-
if _, err := findMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout); err != nil {
177+
if _, err := FindMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout); err != nil {
164178
return fmt.Errorf("failed to find NGINX main process: %w", err)
165179
}
166180
return nil
167181
}
168182

169-
func findMainProcess(
183+
func FindMainProcess(
170184
ctx context.Context,
171-
checkFile checkFileFunc,
172-
readFile readFileFunc,
185+
checkFile CheckFileFunc,
186+
readFile ReadFileFunc,
173187
timeout time.Duration,
174188
) (int, error) {
175189
ctx, cancel := context.WithTimeout(ctx, timeout)
@@ -180,7 +194,7 @@ func findMainProcess(
180194
500*time.Millisecond,
181195
true, /* poll immediately */
182196
func(_ context.Context) (bool, error) {
183-
_, err := checkFile(pidFile)
197+
_, err := checkFile(PidFile)
184198
if err == nil {
185199
return true, nil
186200
}
@@ -193,7 +207,7 @@ func findMainProcess(
193207
return 0, err
194208
}
195209

196-
content, err := readFile(pidFile)
210+
content, err := readFile(PidFile)
197211
if err != nil {
198212
return 0, err
199213
}
@@ -205,3 +219,21 @@ func findMainProcess(
205219

206220
return pid, nil
207221
}
222+
223+
func ReadFile(file string) ([]byte, error) {
224+
content, err := os.ReadFile(file)
225+
if err != nil {
226+
return nil, err
227+
}
228+
229+
return content, nil
230+
}
231+
232+
func SysCallKill(pid int, signum syscall.Signal) error {
233+
err := syscall.Kill(pid, syscall.SIGHUP)
234+
if err != nil {
235+
return err
236+
}
237+
238+
return nil
239+
}

0 commit comments

Comments
 (0)