Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 49 additions & 3 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package console
import (
_ "embed"
"fmt"
"os"
"strconv"
"strings"
"text/template"

Expand All @@ -12,6 +14,18 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)

const (
// DefaultNginxWorkerProcesses is used when CONSOLE_NGINX_WORKER_PROCESSES
// is unset or invalid. A fixed value avoids OOM/FD exhaustion on high-CPU
// nodes where "auto" would spawn one worker per CPU.
DefaultNginxWorkerProcesses = "8"

// NginxWorkerProcessesEnvVar overrides DefaultNginxWorkerProcesses when set
// on the operator pod via Subscription spec.config.env. Accepted values are
// a positive integer or "auto".
NginxWorkerProcessesEnvVar = "CONSOLE_NGINX_WORKER_PROCESSES"
)

var (
DeploymentName = "ocs-client-operator-console"
pluginBasePath = "/"
Expand All @@ -30,8 +44,8 @@ var (
//go:embed nginx_proxy.tmpl
var nginxProxyConf string

//go:embed nginx_root.conf
var nginxRootConf string
//go:embed nginx_root.tmpl
var nginxRootTmplText string

func GetService(port int32, namespace string) *apiv1.Service {
return &apiv1.Service{
Expand Down Expand Up @@ -85,8 +99,40 @@ func GetConsolePlugin(consolePort int32, serviceNamespace string) *consolev1.Con
}
}

// GetNginxWorkerProcesses returns the nginx worker_processes value.
// Prefer CONSOLE_NGINX_WORKER_PROCESSES when it is a positive integer or "auto";
// otherwise fall back to DefaultNginxWorkerProcesses.
func GetNginxWorkerProcesses() string {
value := strings.TrimSpace(os.Getenv(NginxWorkerProcessesEnvVar))
if value == "" {
return DefaultNginxWorkerProcesses
}
if strings.EqualFold(value, "auto") {
return "auto"
}
n, err := strconv.Atoi(value)
if err != nil || n < 1 {
return DefaultNginxWorkerProcesses
}
return strconv.Itoa(n)
}

func GetNginxRootConf() string {
return nginxRootConf
data := struct {
WorkerProcesses string
}{
WorkerProcesses: GetNginxWorkerProcesses(),
}

t, err := template.New("nginxRoot").Parse(nginxRootTmplText)
if err != nil {
panic(fmt.Sprintf("failed to parse nginx root config template: %v", err))
}
var sb strings.Builder
if err := t.Execute(&sb, data); err != nil {
panic(fmt.Sprintf("failed to render nginx root config: %v", err))
}
return sb.String()
}

func GetNginxProxyConf(uniqueIdentifier, exposeAs, endpointURL, endpointHost, certsPath string) (string, error) {
Expand Down
63 changes: 63 additions & 0 deletions pkg/console/console_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package console

import (
"os"
"strings"
"testing"
)

func TestGetNginxWorkerProcesses(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
want string
}{
{name: "default when unset", setEnv: false, want: DefaultNginxWorkerProcesses},
{name: "default when empty", setEnv: true, envValue: "", want: DefaultNginxWorkerProcesses},
{name: "positive integer", setEnv: true, envValue: "4", want: "4"},
{name: "auto", setEnv: true, envValue: "auto", want: "auto"},
{name: "AUTO case insensitive", setEnv: true, envValue: "AUTO", want: "auto"},
{name: "invalid falls back", setEnv: true, envValue: "not-a-number", want: DefaultNginxWorkerProcesses},
{name: "zero falls back", setEnv: true, envValue: "0", want: DefaultNginxWorkerProcesses},
{name: "negative falls back", setEnv: true, envValue: "-1", want: DefaultNginxWorkerProcesses},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv {
t.Setenv(NginxWorkerProcessesEnvVar, tt.envValue)
} else {
t.Setenv(NginxWorkerProcessesEnvVar, "")
_ = os.Unsetenv(NginxWorkerProcessesEnvVar)
}
if got := GetNginxWorkerProcesses(); got != tt.want {
t.Errorf("GetNginxWorkerProcesses() = %q, want %q", got, tt.want)
}
})
}
}

func TestGetNginxRootConf_DefaultWorkerProcesses(t *testing.T) {
t.Setenv(NginxWorkerProcessesEnvVar, "")
_ = os.Unsetenv(NginxWorkerProcessesEnvVar)

conf := GetNginxRootConf()
if !strings.Contains(conf, "worker_processes 8;") {
t.Errorf("expected default worker_processes 8")
}
if strings.Contains(conf, "worker_processes auto;") {
t.Errorf("expected no worker_processes auto")
}
}

func TestGetNginxRootConf_WorkerProcessesFromEnv(t *testing.T) {
t.Setenv(NginxWorkerProcessesEnvVar, "2")
conf := GetNginxRootConf()
if !strings.Contains(conf, "worker_processes 2;") {
t.Errorf("expected worker_processes 2 from env")
}
if strings.Contains(conf, "worker_processes auto;") {
t.Errorf("expected no worker_processes auto")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Do not comment/un-comment without any reference.

worker_processes auto;
worker_processes {{.WorkerProcesses}};
error_log /var/log/nginx/error.log;
pid /var/lib/nginx/tmp/nginx.pid;

Expand Down
Loading