Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
65f6bd3
add tests for runtime manager
miledxz Jul 7, 2024
b98942e
adding required tests update
miledxz Jul 7, 2024
266fdc0
resolving conflicts
miledxz Jul 7, 2024
676548c
adding resolving conflicts
miledxz Jul 7, 2024
88f656f
adding additional updates with Reload fnction tests
miledxz Feb 26, 2024
93157b9
adding required updates
miledxz Mar 12, 2024
df5ccbe
adding fixes
miledxz May 6, 2024
3dc727a
adding processHandler struct update
miledxz May 20, 2024
a0c0228
adding cleanup fix update
miledxz May 20, 2024
596efd3
adding gofumpt fix update
miledxz May 20, 2024
54e1efe
adding additional fix updates
miledxz May 20, 2024
39cc3d7
adding tests for runtime manager
miledxz Jun 27, 2024
7f6d1bf
resolving partionally comments in pr
miledxz Jun 29, 2024
9777319
Merge branch 'nginxinc:main' into test/add-runtime-manager-tests
miledxz Jul 7, 2024
0ecf5d1
adding first part of resolving final comments
miledxz Jul 7, 2024
ecc39c5
experimental commit for NewProcessHandlerImpl
miledxz Jul 7, 2024
948b9be
Merge branch 'nginxinc:main' into test/add-runtime-manager-tests
miledxz Jul 13, 2024
f146435
updating test desc
miledxz Jul 13, 2024
b7884d0
adding required updates regarding readFile
miledxz Jul 13, 2024
04f6e30
adding additional experimental ReadFile update
miledxz Jul 13, 2024
6963fd4
adding general fix for manager
miledxz Jul 14, 2024
547e903
adding small fix update
miledxz Aug 10, 2024
27fed56
adding manager fix update
miledxz Aug 20, 2024
8b94a05
quick upstream test update
miledxz Aug 20, 2024
eda39b2
Merge branch 'main' into test/add-runtime-manager-tests
kate-osborn Aug 20, 2024
7be17ee
experimental fix
miledxz Aug 21, 2024
1a56441
adding lint fix update
miledxz Aug 29, 2024
8913e20
Merge branch 'main' into test/add-runtime-manager-tests
sjberman Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func StartManager(cfg config.Config) error {
return fmt.Errorf("cannot clear NGINX configuration folders: %w", err)
}

var processHandler = &ngxruntime.ProcessHandlerImpl{}
processHandler := &ngxruntime.NewProcessHandlerImpl{}

// Ensure NGINX is running before registering metrics & starting the manager.
if err := processHandler.EnsureNginxRunning(ctx); err != nil {
Expand Down
38 changes: 17 additions & 21 deletions internal/mode/static/nginx/runtime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type (

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

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . nginxPlusClient
//counterfeiter:generate . nginxPlusClient

type nginxPlusClient interface {
UpdateHTTPServers(
Expand All @@ -49,21 +49,19 @@ type nginxPlusClient interface {
GetUpstreams() (*ngxclient.Upstreams, error)
}

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ProcessHandler
//counterfeiter:generate . ProcessHandler

type ProcessHandler interface {
FindMainProcess(
ctx context.Context,
checkFile CheckFileFunc,
readFile ReadFileFunc,
timeout time.Duration,
) (int, error)
ReadFile(file string) ([]byte, error)
readFile(file string) ([]byte, error)
Kill(pid int) error
EnsureNginxRunning(ctx context.Context) error
}

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
//counterfeiter:generate . Manager

// Manager manages the runtime of NGINX.
type Manager interface {
Expand All @@ -79,9 +77,9 @@ type Manager interface {
GetUpstreams() (ngxclient.Upstreams, error)
}

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . MetricsCollector

// MetricsCollector is an interface for the metrics of the NGINX runtime manager.
//
//counterfeiter:generate . MetricsCollector
type MetricsCollector interface {
IncReloadCount()
IncReloadErrors()
Expand Down Expand Up @@ -122,13 +120,13 @@ func (m *ManagerImpl) IsPlus() bool {
func (m *ManagerImpl) Reload(ctx context.Context, configVersion int) error {
start := time.Now()
// We find the main NGINX PID on every reload because it will change if the NGINX container is restarted.
pid, err := m.processHandler.FindMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout)
pid, err := m.processHandler.FindMainProcess(ctx, pidFileTimeout)
if err != nil {
return fmt.Errorf("failed to find NGINX main process: %w", err)
}

childProcFile := fmt.Sprintf(childProcPathFmt, pid)
previousChildProcesses, err := m.processHandler.ReadFile(childProcFile)
previousChildProcesses, err := m.processHandler.readFile(childProcFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,20 +189,18 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
return *upstreams, nil
}

type ProcessHandlerImpl struct{}
type NewProcessHandlerImpl struct{}

// EnsureNginxRunning ensures NGINX is running by locating the main process.
func (p *ProcessHandlerImpl) EnsureNginxRunning(ctx context.Context) error {
if _, err := p.FindMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout); err != nil {
func (p *NewProcessHandlerImpl) EnsureNginxRunning(ctx context.Context) error {
if _, err := p.FindMainProcess(ctx, pidFileTimeout); err != nil {
return fmt.Errorf("failed to find NGINX main process: %w", err)
}
return nil
}

func (p *ProcessHandlerImpl) FindMainProcess(
func (p *NewProcessHandlerImpl) FindMainProcess(
ctx context.Context,
checkFile CheckFileFunc,
readFile ReadFileFunc,
timeout time.Duration,
) (int, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
Expand All @@ -215,7 +211,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
500*time.Millisecond,
true, /* poll immediately */
func(_ context.Context) (bool, error) {
_, err := checkFile(PidFile)
_, err := p.checkFile(PidFile)
if err == nil {
return true, nil
}
Expand All @@ -228,7 +224,7 @@ func (p *ProcessHandlerImpl) FindMainProcess(
return 0, err
}

content, err := readFile(PidFile)
content, err := p.readFile(PidFile)
if err != nil {
return 0, err
}
Expand All @@ -241,10 +237,10 @@ func (p *ProcessHandlerImpl) FindMainProcess(
return pid, nil
}

func (p *ProcessHandlerImpl) ReadFile(file string) ([]byte, error) {
return os.ReadFile(file)
func (p *NewProcessHandlerImpl) readFile(file string) ([]byte, error) {
return p.readFile(file)
}

func (p *ProcessHandlerImpl) Kill(pid int) error {
func (p *NewProcessHandlerImpl) Kill(pid int) error {
return syscall.Kill(pid, syscall.SIGHUP)
}
4 changes: 2 additions & 2 deletions internal/mode/static/nginx/runtime/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func TestFindMainProcess(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)
p := runtime.ProcessHandlerImpl{}
result, err := p.FindMainProcess(test.ctx, test.checkFile, test.readFile, 2*time.Millisecond)
p := runtime.NewProcessHandlerImpl{}
result, err := p.FindMainProcess(test.ctx, 2*time.Millisecond)

if test.expectError {
g.Expect(err).To(HaveOccurred())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions internal/mode/static/nginx/runtime/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ type nginxConfigVerifier interface {
EnsureConfigVersion(ctx context.Context, expectedVersion int) error
}

// VerifyClientImpl is a client for verifying the config version.
type VerifyClientImpl struct {
// VerifyClient is a client for verifying the config version.
type VerifyClient struct {
client *http.Client
timeout time.Duration
}

// NewVerifyClient returns a new client pointed at the config version socket.
func NewVerifyClient(timeout time.Duration) *VerifyClientImpl {
return &VerifyClientImpl{
func NewVerifyClient(timeout time.Duration) *VerifyClient {
return &VerifyClient{
client: &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
Expand All @@ -55,7 +55,7 @@ func NewVerifyClient(timeout time.Duration) *VerifyClientImpl {

// GetConfigVersion gets the version number that we put in the nginx config to verify that we're using
// the correct config.
func (c *VerifyClientImpl) GetConfigVersion() (int, error) {
func (c *VerifyClient) GetConfigVersion() (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()

Expand Down Expand Up @@ -88,7 +88,7 @@ func (c *VerifyClientImpl) GetConfigVersion() (int, error) {
// WaitForCorrectVersion first ensures any new worker processes have been started, and then calls the config version
// endpoint until it gets the expectedVersion, which ensures that a new worker process has been started for that config
// version.
func (c *VerifyClientImpl) WaitForCorrectVersion(
func (c *VerifyClient) WaitForCorrectVersion(
ctx context.Context,
expectedVersion int,
childProcFile string,
Expand Down Expand Up @@ -119,7 +119,7 @@ func (c *VerifyClientImpl) WaitForCorrectVersion(
return nil
}

func (c *VerifyClientImpl) EnsureConfigVersion(ctx context.Context, expectedVersion int) error {
func (c *VerifyClient) EnsureConfigVersion(ctx context.Context, expectedVersion int) error {
return wait.PollUntilContextCancel(
ctx,
25*time.Millisecond,
Expand Down