Skip to content

Commit

Permalink
Add dynamic modules to NGINX Info (#654)
Browse files Browse the repository at this point in the history
* add Dynamic modules
  • Loading branch information
aphralG authored May 1, 2024
1 parent de0fd11 commit a66a4e2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/command.pb.go

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

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/common.pb.go

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

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/files.pb.go

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

23 changes: 21 additions & 2 deletions internal/service/instance/nginx_instance_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Info struct {
ConfigureArgs map[string]interface{}
ExePath string
LoadableModules []string
DynamicModules []string
}

type Nginx struct {
Expand All @@ -47,6 +48,11 @@ type NginxParameters struct {
executer exec.ExecInterface
}

const (
withWithPrefix = "with-"
withModuleSuffix = "module"
)

func NewNginx(parameters NginxParameters) *Nginx {
if parameters.executer == nil {
parameters.executer = &exec.Exec{}
Expand Down Expand Up @@ -116,6 +122,8 @@ func (n *Nginx) getInfo(ctx context.Context, nginxProcess *model.Process) (*Info
loadableModules := getLoadableModules(nginxInfo)
nginxInfo.LoadableModules = loadableModules

nginxInfo.DynamicModules = getDynamicModules(nginxInfo)

return nginxInfo, err
}

Expand All @@ -135,7 +143,7 @@ func convertInfoToProcess(nginxInfo Info) *v1.Instance {
AccessLogs: []string{},
ErrorLogs: []string{},
LoadableModules: nginxInfo.LoadableModules,
DynamicModules: []string{},
DynamicModules: nginxInfo.DynamicModules,
},
},
}
Expand All @@ -150,7 +158,7 @@ func convertInfoToProcess(nginxInfo Info) *v1.Instance {
AccessLogs: []string{},
ErrorLogs: []string{},
LoadableModules: nginxInfo.LoadableModules,
DynamicModules: []string{},
DynamicModules: nginxInfo.DynamicModules,
PlusApi: "",
},
},
Expand Down Expand Up @@ -297,6 +305,17 @@ func getLoadableModules(nginxInfo *Info) (modules []string) {
return modules
}

func getDynamicModules(nginxInfo *Info) (modules []string) {
configArgs := nginxInfo.ConfigureArgs
for arg := range configArgs {
if strings.HasPrefix(arg, withWithPrefix) && strings.HasSuffix(arg, withModuleSuffix) {
modules = append(modules, strings.TrimPrefix(arg, withWithPrefix))
}
}

return modules
}

// readDirectory returns a list of all files in the directory which match the extension
func readDirectory(dir, extension string) (files []string, err error) {
dirInfo, err := os.ReadDir(dir)
Expand Down
23 changes: 19 additions & 4 deletions internal/service/instance/nginx_instance_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"bytes"
"context"
"fmt"
"log/slog"
"path"
"path/filepath"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -78,17 +77,21 @@ func TestGetInstances(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
modulePath := tempDir + "/usr/lib/nginx/modules"
noModulesPath := t.TempDir() + "/usr/lib/nginx/modules"

helpers.CreateDirWithErrorCheck(t, modulePath)
defer helpers.RemoveFileWithErrorCheck(t, modulePath)

helpers.CreateDirWithErrorCheck(t, noModulesPath)
defer helpers.RemoveFileWithErrorCheck(t, noModulesPath)

testModule := helpers.CreateFileWithErrorCheck(t, modulePath, "test.so")
defer helpers.RemoveFileWithErrorCheck(t, testModule.Name())

plusArgs := fmt.Sprintf(plusConfigArgs, modulePath)
ossArgs := fmt.Sprintf(ossConfigArgs, modulePath)
noModuleArgs := path.Join(ossConfigArgs, t.TempDir()+"/usr/lib/nginx/modules")
slog.Info("", "", noModuleArgs)
noModuleArgs := fmt.Sprintf(ossConfigArgs, noModulesPath)

expectedModules := strings.ReplaceAll(filepath.Base(testModule.Name()), ".so", "")

processes := []*model.Process{
Expand Down Expand Up @@ -165,6 +168,13 @@ func TestGetInstances(t *testing.T) {
n := NewNginx(NginxParameters{executer: mockExec})
result := n.GetInstances(ctx, processes)

for _, instance := range result {
if instance.GetInstanceRuntime().GetNginxRuntimeInfo() != nil {
sort.Strings(instance.GetInstanceRuntime().GetNginxRuntimeInfo().GetDynamicModules())
} else {
sort.Strings(instance.GetInstanceRuntime().GetNginxPlusRuntimeInfo().GetDynamicModules())
}
}
assert.Equal(tt, test.expected, result)
})
}
Expand Down Expand Up @@ -255,6 +265,8 @@ func TestGetInfo(t *testing.T) {
"with-stream_ssl_preread_module": true,
},
LoadableModules: []string{expectedModules},
DynamicModules: protos.GetNginxOssInstance([]string{}).GetInstanceRuntime().GetNginxRuntimeInfo().
GetDynamicModules(),
},
},
{
Expand Down Expand Up @@ -328,6 +340,8 @@ func TestGetInfo(t *testing.T) {
"with-threads": true,
},
LoadableModules: []string{expectedModules},
DynamicModules: protos.GetNginxPlusInstance([]string{}).GetInstanceRuntime().GetNginxPlusRuntimeInfo().
GetDynamicModules(),
},
},
}
Expand All @@ -339,6 +353,7 @@ func TestGetInfo(t *testing.T) {

n := NewNginx(NginxParameters{executer: mockExec})
result, err := n.getInfo(ctx, test.process)
sort.Strings(result.DynamicModules)

assert.Equal(tt, test.expected, result)
require.NoError(tt, err)
Expand Down
23 changes: 20 additions & 3 deletions test/protos/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ func GetNginxOssInstance(expectedModules []string) *v1.Instance {
AccessLogs: []string{},
ErrorLogs: []string{},
LoadableModules: expectedModules,
DynamicModules: []string{},
DynamicModules: []string{
"http_addition_module", "http_auth_request_module", "http_dav_module",
"http_degradation_module", "http_flv_module", "http_gunzip_module", "http_gzip_static_module",
"http_mp4_module", "http_random_index_module", "http_realip_module", "http_secure_link_module",
"http_slice_module", "http_ssl_module", "http_stub_status_module", "http_sub_module",
"http_v2_module", "mail_ssl_module", "stream_realip_module", "stream_ssl_module",
"stream_ssl_preread_module",
},
},
},
},
Expand All @@ -61,8 +68,18 @@ func GetNginxPlusInstance(expectedModules []string) *v1.Instance {
AccessLogs: []string{},
ErrorLogs: []string{},
LoadableModules: expectedModules,
DynamicModules: []string{},
PlusApi: "",
DynamicModules: []string{
"http_addition_module", "http_auth_jwt_module", "http_auth_request_module", "http_dav_module",
"http_f4f_module", "http_flv_module", "http_gunzip_module", "http_gzip_static_module",
"http_hls_module", "http_mp4_module", "http_proxy_protocol_vendor_module",
"http_random_index_module", "http_realip_module", "http_secure_link_module",
"http_session_log_module", "http_slice_module", "http_ssl_module", "http_stub_status_module",
"http_sub_module", "http_v2_module", "http_v3_module", "mail_ssl_module",
"stream_mqtt_filter_module", "stream_mqtt_preread_module",
"stream_proxy_protocol_vendor_module", "stream_realip_module", "stream_ssl_module",
"stream_ssl_preread_module",
},
PlusApi: "",
},
},
},
Expand Down

0 comments on commit a66a4e2

Please sign in to comment.