Skip to content

Commit a4e4a75

Browse files
committed
feat: Parameterize config paths.
1 parent d5ab319 commit a4e4a75

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

internal/ingress/controller/nginx.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
704704
}
705705

706706
if klog.V(2).Enabled() {
707-
src, err := os.ReadFile(cfgPath)
707+
src, err := os.ReadFile(cfgPath())
708708
if err != nil {
709709
return err
710710
}
@@ -719,7 +719,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
719719
return err
720720
}
721721
//nolint:gosec //Ignore G204 error
722-
diffOutput, err := exec.Command("diff", "-I", "'# Configuration.*'", "-u", cfgPath, tmpfile.Name()).CombinedOutput()
722+
diffOutput, err := exec.Command("diff", "-I", "'# Configuration.*'", "-u", cfgPath(), tmpfile.Name()).CombinedOutput()
723723
if err != nil {
724724
if exitError, ok := err.(*exec.ExitError); ok {
725725
ws, ok := exitError.Sys().(syscall.WaitStatus)
@@ -740,7 +740,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
740740
}
741741
}
742742

743-
err = os.WriteFile(cfgPath, content, file.ReadWriteByUser)
743+
err = os.WriteFile(cfgPath(), content, file.ReadWriteByUser)
744744
if err != nil {
745745
return err
746746
}
@@ -1105,7 +1105,7 @@ func (n *NGINXController) createLuaConfig(cfg *ngx_config.Configuration) error {
11051105
if err != nil {
11061106
return err
11071107
}
1108-
return os.WriteFile(luaCfgPath, jsonCfg, file.ReadWriteByUser)
1108+
return os.WriteFile(luaCfgPath(), jsonCfg, file.ReadWriteByUser)
11091109
}
11101110

11111111
func cleanTempNginxCfg() error {

internal/ingress/controller/util.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ func rlimitMaxNumFiles() int {
9898
}
9999

100100
const (
101-
defBinary = "/usr/bin/nginx"
102-
cfgPath = "/etc/nginx/nginx.conf"
103-
luaCfgPath = "/etc/nginx/lua/cfg.json"
101+
defBinary = "/usr/bin/nginx"
104102
)
105103

106104
// NginxExecTester defines the interface to execute
@@ -115,6 +113,22 @@ type NginxCommand struct {
115113
Binary string
116114
}
117115

116+
func luaCfgPath() string {
117+
cfgPath := os.Getenv("NGINX_LUA_CONFIG_PATH")
118+
if cfgPath == "" {
119+
return "/etc/nginx/lua/cfg.json"
120+
}
121+
return cfgPath
122+
}
123+
124+
func cfgPath() string {
125+
cfgPath := os.Getenv("NGINX_CONFIG_PATH")
126+
if cfgPath == "" {
127+
return "/etc/nginx/nginx.conf"
128+
}
129+
return cfgPath
130+
}
131+
118132
// NewNginxCommand returns a new NginxCommand from which path
119133
// has been detected from environment variable NGINX_BINARY or default
120134
func NewNginxCommand() NginxCommand {
@@ -134,7 +148,7 @@ func NewNginxCommand() NginxCommand {
134148
func (nc NginxCommand) ExecCommand(args ...string) *exec.Cmd {
135149
cmdArgs := []string{}
136150

137-
cmdArgs = append(cmdArgs, "-c", cfgPath)
151+
cmdArgs = append(cmdArgs, "-c", cfgPath())
138152
cmdArgs = append(cmdArgs, args...)
139153
//nolint:gosec // Ignore G204 error
140154
return exec.Command(nc.Binary, cmdArgs...)

0 commit comments

Comments
 (0)