From e694c573f79d7055707109dc5ad25555dd9eede7 Mon Sep 17 00:00:00 2001 From: Ricardo Katz Date: Thu, 11 Jul 2024 22:40:15 +0000 Subject: [PATCH] Start using controlplane --- .../ingress/controller/template/crossplane.go | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 internal/ingress/controller/template/crossplane.go diff --git a/internal/ingress/controller/template/crossplane.go b/internal/ingress/controller/template/crossplane.go new file mode 100644 index 000000000..6b89b76f7 --- /dev/null +++ b/internal/ingress/controller/template/crossplane.go @@ -0,0 +1,111 @@ +package template + +import ( + "strconv" + + crossplane "github.com/nginxinc/nginx-go-crossplane" + "k8s.io/ingress-nginx/internal/ingress/controller/config" +) + +// On this case we will try to use the go crossplane to write the template instead of the template renderer + +type crossplaneTemplate struct { + options *crossplane.BuildOptions + config *crossplane.Config + tplConfig *config.TemplateConfig +} + +func NewCrossplaneTemplate() *crossplaneTemplate { + lua := crossplane.Lua{} + return &crossplaneTemplate{ + options: &crossplane.BuildOptions{ + Builders: []crossplane.RegisterBuilder{ + lua.RegisterBuilder(), + }, + }, + } +} + +func (c *crossplaneTemplate) Write(conf *config.TemplateConfig) ([]byte, error) { + c.tplConfig = conf + // Write basic directives + config := &crossplane.Config{ + Parsed: crossplane.Directives{ + buildDirective("pid", c.tplConfig.PID), + buildDirective("daemon", "off"), + buildDirective("worker_processes", c.tplConfig.Cfg.WorkerProcesses), + buildDirective("worker_rlimit_nofile", strconv.Itoa(c.tplConfig.Cfg.MaxWorkerOpenFiles)), + buildDirective("worker_shutdown_timeout", c.tplConfig.Cfg.WorkerShutdownTimeout), + }, + } + if c.tplConfig.Cfg.WorkerCPUAffinity != "" { + config.Parsed = append(config.Parsed, buildDirective("worker_cpu_affinity", c.tplConfig.Cfg.WorkerCPUAffinity)) + } + c.config = config + + // Load Modules + c.loadModules() + + // build events directive + c.buildEvents() + + // build http directive + c.buildHTTP() + + return nil, nil +} + +func buildDirective(directive string, args ...string) *crossplane.Directive { + return &crossplane.Directive{ + Directive: directive, + Args: args, + } +} + +func (c *crossplaneTemplate) loadModules() { + if c.tplConfig.Cfg.EnableBrotli { + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/ngx_http_brotli_filter_module.so")) + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/ngx_http_brotli_static_module.so")) + } + + if c.tplConfig.Cfg.UseGeoIP2 { + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/ngx_http_geoip2_module.so")) + } + + if shouldLoadAuthDigestModule(c.tplConfig.Servers) { + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/ngx_http_auth_digest_module.so")) + } + + if shouldLoadModSecurityModule(c.tplConfig.Cfg, c.tplConfig.Servers) { + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/ngx_http_modsecurity_module.so")) + } + + if shouldLoadOpentelemetryModule(c.tplConfig.Cfg, c.tplConfig.Servers) { + c.config.Parsed = append(c.config.Parsed, buildDirective("load_module", "/etc/nginx/modules/otel_ngx_module.so")) + } +} + +func (c *crossplaneTemplate) buildEvents() { + events := &crossplane.Directive{ + Directive: "events", + Block: crossplane.Directives{ + buildDirective("worker_connections", strconv.Itoa(c.tplConfig.Cfg.MaxWorkerConnections)), + buildDirective("use", "epool"), + buildDirective("use", "epool"), + buildDirective("multi_accept", boolToStr(c.tplConfig.Cfg.EnableMultiAccept)), + }, + } + for _, v := range c.tplConfig.Cfg.DebugConnections { + events.Block = append(events.Block, buildDirective("debug_connection", v)) + } +} + +func (c *crossplaneTemplate) buildHTTP() { +} + +func boolToStr(b bool) string { + if b { + return "on" + } + return "off" +}