Skip to content

Commit

Permalink
feat: Parameterize config paths. Fixes #12911.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel committed Feb 28, 2025
1 parent d5ab319 commit b164870
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
}

if klog.V(2).Enabled() {
src, err := os.ReadFile(cfgPath)
src, err := os.ReadFile(cfgPath())
if err != nil {
return err
}
Expand All @@ -719,7 +719,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
return err
}
//nolint:gosec //Ignore G204 error
diffOutput, err := exec.Command("diff", "-I", "'# Configuration.*'", "-u", cfgPath, tmpfile.Name()).CombinedOutput()
diffOutput, err := exec.Command("diff", "-I", "'# Configuration.*'", "-u", cfgPath(), tmpfile.Name()).CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws, ok := exitError.Sys().(syscall.WaitStatus)
Expand All @@ -740,7 +740,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
}
}

err = os.WriteFile(cfgPath, content, file.ReadWriteByUser)
err = os.WriteFile(cfgPath(), content, file.ReadWriteByUser)
if err != nil {
return err
}
Expand Down Expand Up @@ -1105,7 +1105,7 @@ func (n *NGINXController) createLuaConfig(cfg *ngx_config.Configuration) error {
if err != nil {
return err
}
return os.WriteFile(luaCfgPath, jsonCfg, file.ReadWriteByUser)
return os.WriteFile(luaCfgPath(), jsonCfg, file.ReadWriteByUser)
}

func cleanTempNginxCfg() error {
Expand Down
22 changes: 18 additions & 4 deletions internal/ingress/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func rlimitMaxNumFiles() int {
}

const (
defBinary = "/usr/bin/nginx"
cfgPath = "/etc/nginx/nginx.conf"
luaCfgPath = "/etc/nginx/lua/cfg.json"
defBinary = "/usr/bin/nginx"
)

// NginxExecTester defines the interface to execute
Expand All @@ -115,6 +113,22 @@ type NginxCommand struct {
Binary string
}

func luaCfgPath() string {
cfgPath := os.Getenv("NGINX_LUA_CONFIG_PATH")
if cfgPath == "" {
return "/etc/nginx/lua/cfg.json"
}
return cfgPath
}

func cfgPath() string {
cfgPath := os.Getenv("NGINX_CONFIG_PATH")
if cfgPath == "" {
return "/etc/nginx/nginx.conf"
}
return cfgPath
}

// NewNginxCommand returns a new NginxCommand from which path
// has been detected from environment variable NGINX_BINARY or default
func NewNginxCommand() NginxCommand {
Expand All @@ -134,7 +148,7 @@ func NewNginxCommand() NginxCommand {
func (nc NginxCommand) ExecCommand(args ...string) *exec.Cmd {
cmdArgs := []string{}

cmdArgs = append(cmdArgs, "-c", cfgPath)
cmdArgs = append(cmdArgs, "-c", cfgPath())
cmdArgs = append(cmdArgs, args...)
//nolint:gosec // Ignore G204 error
return exec.Command(nc.Binary, cmdArgs...)
Expand Down

0 comments on commit b164870

Please sign in to comment.