@@ -17,25 +17,33 @@ import (
17
17
)
18
18
19
19
const (
20
- pidFile = "/var/run/nginx/nginx.pid"
20
+ PidFile = "/var/run/nginx/nginx.pid"
21
21
pidFileTimeout = 10000 * time .Millisecond
22
22
nginxReloadTimeout = 60000 * time .Millisecond
23
23
)
24
24
25
25
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 )
28
28
)
29
29
30
30
var childProcPathFmt = "/proc/%[1]v/task/%[1]v/children"
31
31
32
- //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . NginxPlusClient
32
+ //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . nginxPlusClient
33
33
34
- type NginxPlusClient interface {
34
+ type nginxPlusClient interface {
35
35
UpdateHTTPServers (upstream string , servers []ngxclient.UpstreamServer ) (added []ngxclient.UpstreamServer , deleted []ngxclient.UpstreamServer , updated []ngxclient.UpstreamServer , err error )
36
36
GetUpstreams () (* ngxclient.Upstreams , error )
37
37
}
38
38
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
+
39
47
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
40
48
41
49
// Manager manages the runtime of NGINX.
@@ -52,6 +60,8 @@ type Manager interface {
52
60
GetUpstreams () (ngxclient.Upstreams , error )
53
61
}
54
62
63
+ //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MetricsCollector
64
+
55
65
// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
56
66
type MetricsCollector interface {
57
67
IncReloadCount ()
@@ -61,23 +71,27 @@ type MetricsCollector interface {
61
71
62
72
// ManagerImpl implements Manager.
63
73
type ManagerImpl struct {
64
- verifyClient * verifyClient
74
+ verifyClient verifyClient
65
75
metricsCollector MetricsCollector
66
- ngxPlusClient NginxPlusClient
76
+ ngxPlusClient nginxPlusClient
67
77
logger logr.Logger
78
+ process processHandler
68
79
}
69
80
70
81
// NewManagerImpl creates a new ManagerImpl.
71
82
func NewManagerImpl (
72
- ngxPlusClient NginxPlusClient ,
83
+ ngxPlusClient nginxPlusClient ,
73
84
collector MetricsCollector ,
74
85
logger logr.Logger ,
86
+ process processHandler ,
87
+ verifyClient verifyClient ,
75
88
) * ManagerImpl {
76
89
return & ManagerImpl {
77
- verifyClient : newVerifyClient ( nginxReloadTimeout ) ,
90
+ verifyClient : verifyClient ,
78
91
metricsCollector : collector ,
79
92
ngxPlusClient : ngxPlusClient ,
80
93
logger : logger ,
94
+ process : process ,
81
95
}
82
96
}
83
97
@@ -89,25 +103,25 @@ func (m *ManagerImpl) IsPlus() bool {
89
103
func (m * ManagerImpl ) Reload (ctx context.Context , configVersion int ) error {
90
104
start := time .Now ()
91
105
// 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 )
93
107
if err != nil {
94
108
return fmt .Errorf ("failed to find NGINX main process: %w" , err )
95
109
}
96
110
97
111
childProcFile := fmt .Sprintf (childProcPathFmt , pid )
98
- previousChildProcesses , err := os .ReadFile (childProcFile )
112
+ previousChildProcesses , err := m . process .ReadFile (childProcFile )
99
113
if err != nil {
100
114
return err
101
115
}
102
116
103
117
// send HUP signal to the NGINX main process reload configuration
104
118
// 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 {
106
120
m .metricsCollector .IncReloadErrors ()
107
121
return fmt .Errorf ("failed to send the HUP signal to NGINX main: %w" , err )
108
122
}
109
123
110
- if err = m .verifyClient .waitForCorrectVersion (
124
+ if err = m .verifyClient .WaitForCorrectVersion (
111
125
ctx ,
112
126
configVersion ,
113
127
childProcFile ,
@@ -160,16 +174,16 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
160
174
161
175
// EnsureNginxRunning ensures NGINX is running by locating the main process.
162
176
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 {
164
178
return fmt .Errorf ("failed to find NGINX main process: %w" , err )
165
179
}
166
180
return nil
167
181
}
168
182
169
- func findMainProcess (
183
+ func FindMainProcess (
170
184
ctx context.Context ,
171
- checkFile checkFileFunc ,
172
- readFile readFileFunc ,
185
+ checkFile CheckFileFunc ,
186
+ readFile ReadFileFunc ,
173
187
timeout time.Duration ,
174
188
) (int , error ) {
175
189
ctx , cancel := context .WithTimeout (ctx , timeout )
@@ -180,7 +194,7 @@ func findMainProcess(
180
194
500 * time .Millisecond ,
181
195
true , /* poll immediately */
182
196
func (_ context.Context ) (bool , error ) {
183
- _ , err := checkFile (pidFile )
197
+ _ , err := checkFile (PidFile )
184
198
if err == nil {
185
199
return true , nil
186
200
}
@@ -193,7 +207,7 @@ func findMainProcess(
193
207
return 0 , err
194
208
}
195
209
196
- content , err := readFile (pidFile )
210
+ content , err := readFile (PidFile )
197
211
if err != nil {
198
212
return 0 , err
199
213
}
@@ -205,3 +219,21 @@ func findMainProcess(
205
219
206
220
return pid , nil
207
221
}
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