@@ -19,18 +19,36 @@ import (
19
19
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
20
20
21
21
const (
22
- pidFile = "/var/run/nginx/nginx.pid"
23
- pidFileTimeout = 10000 * time .Millisecond
24
- nginxReloadTimeout = 60000 * time .Millisecond
22
+ // PidFile specifies the location of the PID file for the Nginx process.
23
+ PidFile = "/var/run/nginx/nginx.pid"
24
+ // PidFileTimeout defines the timeout duration for accessing the PID file.
25
+ PidFileTimeout = 10000 * time .Millisecond
26
+ // NginxReloadTimeout sets the timeout duration for reloading the Nginx configuration.
27
+ NginxReloadTimeout = 60000 * time .Millisecond
25
28
)
26
29
27
30
type (
28
- readFileFunc func (string ) ([]byte , error )
29
- checkFileFunc func (string ) (fs.FileInfo , error )
31
+ ReadFileFunc func (string ) ([]byte , error )
32
+ CheckFileFunc func (string ) (fs.FileInfo , error )
30
33
)
31
34
32
35
var childProcPathFmt = "/proc/%[1]v/task/%[1]v/children"
33
36
37
+ //counterfeiter:generate . NginxPlusClient
38
+
39
+ type NginxPlusClient interface {
40
+ UpdateHTTPServers (
41
+ upstream string ,
42
+ servers []ngxclient.UpstreamServer ,
43
+ ) (
44
+ added []ngxclient.UpstreamServer ,
45
+ deleted []ngxclient.UpstreamServer ,
46
+ updated []ngxclient.UpstreamServer ,
47
+ err error ,
48
+ )
49
+ GetUpstreams () (* ngxclient.Upstreams , error )
50
+ }
51
+
34
52
//counterfeiter:generate . Manager
35
53
36
54
// Manager manages the runtime of NGINX.
@@ -48,6 +66,8 @@ type Manager interface {
48
66
}
49
67
50
68
// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
69
+ //
70
+ //counterfeiter:generate . MetricsCollector
51
71
type MetricsCollector interface {
52
72
IncReloadCount ()
53
73
IncReloadErrors ()
@@ -56,21 +76,25 @@ type MetricsCollector interface {
56
76
57
77
// ManagerImpl implements Manager.
58
78
type ManagerImpl struct {
59
- verifyClient * verifyClient
79
+ processHandler ProcessHandler
60
80
metricsCollector MetricsCollector
61
- ngxPlusClient * ngxclient.NginxClient
81
+ verifyClient nginxConfigVerifier
82
+ ngxPlusClient NginxPlusClient
62
83
logger logr.Logger
63
84
}
64
85
65
86
// NewManagerImpl creates a new ManagerImpl.
66
87
func NewManagerImpl (
67
- ngxPlusClient * ngxclient. NginxClient ,
88
+ ngxPlusClient NginxPlusClient ,
68
89
collector MetricsCollector ,
69
90
logger logr.Logger ,
91
+ processHandler ProcessHandler ,
92
+ verifyClient nginxConfigVerifier ,
70
93
) * ManagerImpl {
71
94
return & ManagerImpl {
72
- verifyClient : newVerifyClient ( nginxReloadTimeout ) ,
95
+ processHandler : processHandler ,
73
96
metricsCollector : collector ,
97
+ verifyClient : verifyClient ,
74
98
ngxPlusClient : ngxPlusClient ,
75
99
logger : logger ,
76
100
}
@@ -84,25 +108,25 @@ func (m *ManagerImpl) IsPlus() bool {
84
108
func (m * ManagerImpl ) Reload (ctx context.Context , configVersion int ) error {
85
109
start := time .Now ()
86
110
// We find the main NGINX PID on every reload because it will change if the NGINX container is restarted.
87
- pid , err := findMainProcess (ctx , os . Stat , os . ReadFile , pidFileTimeout )
111
+ pid , err := m . processHandler . FindMainProcess (ctx , PidFileTimeout )
88
112
if err != nil {
89
113
return fmt .Errorf ("failed to find NGINX main process: %w" , err )
90
114
}
91
115
92
116
childProcFile := fmt .Sprintf (childProcPathFmt , pid )
93
- previousChildProcesses , err := os .ReadFile (childProcFile )
117
+ previousChildProcesses , err := m . processHandler .ReadFile (childProcFile )
94
118
if err != nil {
95
119
return err
96
120
}
97
121
98
122
// send HUP signal to the NGINX main process reload configuration
99
123
// See https://nginx.org/en/docs/control.html
100
- if err := syscall . Kill (pid , syscall . SIGHUP ); err != nil {
124
+ if err := m . processHandler . Kill (pid ); err != nil {
101
125
m .metricsCollector .IncReloadErrors ()
102
126
return fmt .Errorf ("failed to send the HUP signal to NGINX main: %w" , err )
103
127
}
104
128
105
- if err = m .verifyClient .waitForCorrectVersion (
129
+ if err = m .verifyClient .WaitForCorrectVersion (
106
130
ctx ,
107
131
configVersion ,
108
132
childProcFile ,
@@ -153,18 +177,31 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
153
177
return * upstreams , nil
154
178
}
155
179
156
- // EnsureNginxRunning ensures NGINX is running by locating the main process.
157
- func EnsureNginxRunning (ctx context.Context ) error {
158
- if _ , err := findMainProcess (ctx , os .Stat , os .ReadFile , pidFileTimeout ); err != nil {
159
- return fmt .Errorf ("failed to find NGINX main process: %w" , err )
180
+ //counterfeiter:generate . ProcessHandler
181
+
182
+ type ProcessHandler interface {
183
+ FindMainProcess (
184
+ ctx context.Context ,
185
+ timeout time.Duration ,
186
+ ) (int , error )
187
+ ReadFile (file string ) ([]byte , error )
188
+ Kill (pid int ) error
189
+ }
190
+
191
+ type ProcessHandlerImpl struct {
192
+ readFile ReadFileFunc
193
+ checkFile CheckFileFunc
194
+ }
195
+
196
+ func NewProcessHandlerImpl (readFile ReadFileFunc , checkFile CheckFileFunc ) * ProcessHandlerImpl {
197
+ return & ProcessHandlerImpl {
198
+ readFile : readFile ,
199
+ checkFile : checkFile ,
160
200
}
161
- return nil
162
201
}
163
202
164
- func findMainProcess (
203
+ func ( p * ProcessHandlerImpl ) FindMainProcess (
165
204
ctx context.Context ,
166
- checkFile checkFileFunc ,
167
- readFile readFileFunc ,
168
205
timeout time.Duration ,
169
206
) (int , error ) {
170
207
ctx , cancel := context .WithTimeout (ctx , timeout )
@@ -175,7 +212,7 @@ func findMainProcess(
175
212
500 * time .Millisecond ,
176
213
true , /* poll immediately */
177
214
func (_ context.Context ) (bool , error ) {
178
- _ , err := checkFile (pidFile )
215
+ _ , err := p . checkFile (PidFile )
179
216
if err == nil {
180
217
return true , nil
181
218
}
@@ -188,7 +225,7 @@ func findMainProcess(
188
225
return 0 , err
189
226
}
190
227
191
- content , err := readFile (pidFile )
228
+ content , err := p . readFile (PidFile )
192
229
if err != nil {
193
230
return 0 , err
194
231
}
@@ -200,3 +237,11 @@ func findMainProcess(
200
237
201
238
return pid , nil
202
239
}
240
+
241
+ func (p * ProcessHandlerImpl ) ReadFile (file string ) ([]byte , error ) {
242
+ return p .readFile (file )
243
+ }
244
+
245
+ func (p * ProcessHandlerImpl ) Kill (pid int ) error {
246
+ return syscall .Kill (pid , syscall .SIGHUP )
247
+ }
0 commit comments