Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch custom NGINX template from ConfigMap #303

Merged
merged 1 commit into from
Jul 12, 2018
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
54 changes: 54 additions & 0 deletions examples/custom-templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Custom Templates

The Ingress controller allows you to customize your templates through a [ConfigMap](https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/customization) via the following keys:
* `main-template` - Sets the main NGINX configuration template.
* `ingress-template` - Sets the Ingress NGINX configuration template for an Ingress resource.

## Example
```yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
namespace: nginx-ingress
data:
main-template: |
user nginx;
worker_processes {{.WorkerProcesses}};
...
include /etc/nginx/conf.d/*.conf;
}
ingress-template: |
{{range $upstream := .Upstreams}}
upstream {{$upstream.Name}} {
{{if $upstream.LBMethod }}{{$upstream.LBMethod}};{{end}}
...
}{{end}}
```
**Note:** the templates are truncated for the clarity of the example.

## Troubleshooting
* If a custom template contained within the ConfigMap is invalid on startup, the Ingress controller will fail to start, the error will be reported in the Ingress controller logs.

An example of an error from the logs:
```
Error updating NGINX main template: template: nginxTemplate:98: unexpected EOF
```

* If a custom template contained within the ConfigMap is invalid on update, the Ingress controller will not update the NGINX configuration, the error will be reported in the Ingress controller logs and an event with the error will be associated with the ConfigMap.

An example of an error from the logs:
```
Error when updating config from ConfigMap: Invalid nginx configuration detected, not reloading
```

An example of an event with an error (you can view events associated with the ConfigMap by running `kubectl describe -n nginx-ingress configmap nginx-config`):

```
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Updated 12s (x2 over 25s) nginx-ingress-controller Configuration from nginx-ingress/nginx-config was updated
Warning UpdatedWithError 10s nginx-ingress-controller Configuration from nginx-ingress/nginx-config was updated, but not applied: Error when parsing the main template: template: nginxTemplate:98: unexpected EOF
Warning UpdatedWithError 8s nginx-ingress-controller Configuration from nginx-ingress/nginx-config was updated, but not applied: Error when writing main Config
```
2 changes: 2 additions & 0 deletions examples/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The table below summarizes all of the options. For some of them, there are examp
| `nginx.org/server-tokens` | `server-tokens` | Enables or disables the [server_tokens](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens) directive. Additionally, with the NGINX Plus, you can specify a custom string value, including the empty string value, which disables the emission of the “Server” field. | `True`| |
| N/A | `main-snippets` | Sets a custom snippet in main context. | N/A | |
| N/A | `http-snippets` | Sets a custom snippet in http context. | N/A | |
| N/A | `main-template` | Sets the main NGINX configuration template. | By default the template is read from the file in the container. | [Custom Templates](../custom-templates). |
| N/A | `ingress-template` | Sets the NGINX configuration template for an Ingress resource. | By default the template is read from the file on the container. | [Custom Templates](../custom-templates). |
| `nginx.org/location-snippets` | `location-snippets` | Sets a custom snippet in location context. | N/A | |
| `nginx.org/server-snippets` | `server-snippets` | Sets a custom snippet in server context. | N/A | |
| `nginx.org/lb-method` | `lb-method` | Sets the [load balancing method](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#choosing-a-load-balancing-method). To use the round-robin method, specify `"round_robin"`. | `"least_conn"` | |
Expand Down
4 changes: 2 additions & 2 deletions nginx-controller/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func getMergableDefaults() (cafeMaster, coffeeMinion, teaMinion extensions.Ingre
cafeMasterIngEx, _ := lbc.createIngress(&cafeMaster)
ingExMap["default-cafe-master"] = cafeMasterIngEx

cnf := nginx.NewConfigurator(&nginx.NginxController{}, &nginx.Config{}, &plus.NginxAPIController{})
cnf := nginx.NewConfigurator(&nginx.NginxController{}, &nginx.Config{}, &plus.NginxAPIController{}, &nginx.TemplateExecutor{})

// edit private field ingresses to use in testing
pointerVal := reflect.ValueOf(cnf)
Expand Down Expand Up @@ -781,7 +781,7 @@ func TestFindProbeForPods(t *testing.T) {

func TestGetServicePortForIngressPort(t *testing.T) {
fakeClient := fake.NewSimpleClientset()
cnf := nginx.NewConfigurator(&nginx.NginxController{}, &nginx.Config{}, &plus.NginxAPIController{})
cnf := nginx.NewConfigurator(&nginx.NginxController{}, &nginx.Config{}, &plus.NginxAPIController{}, &nginx.TemplateExecutor{})
lbc := LoadBalancerController{
client: fakeClient,
ingressClass: "nginx",
Expand Down
25 changes: 23 additions & 2 deletions nginx-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func main() {
nginxConfTemplatePath = "nginx-plus.tmpl"
nginxIngressTemplatePath = "nginx-plus.ingress.tmpl"
}
templateExecutor, err := nginx.NewTemplateExecutor(nginxConfTemplatePath, nginxIngressTemplatePath, *healthStatus)
if err != nil {
glog.Fatalf("Error creating TemplateExecutor: %v", err)
}
ngxc, _ := nginx.NewNginxController("/etc/nginx/", local, *healthStatus, nginxConfTemplatePath, nginxIngressTemplatePath)

if *defaultServerSecret != "" {
Expand Down Expand Up @@ -150,9 +154,26 @@ func main() {
cfg.MainServerSSLDHParam = fileName
}
}
if cfg.MainTemplate != nil {
err = templateExecutor.UpdateMainTemplate(cfg.MainTemplate)
if err != nil {
glog.Fatalf("Error updating NGINX main template: %v", err)
}
}
if cfg.IngressTemplate != nil {
err = templateExecutor.UpdateIngressTemplate(cfg.IngressTemplate)
if err != nil {
glog.Fatalf("Error updating ingress template: %v", err)
}
}
}

ngxConfig := nginx.GenerateNginxMainConfig(cfg)
ngxc.UpdateMainConfigFile(ngxConfig)
content, err := templateExecutor.ExecuteMainConfigTemplate(ngxConfig)
if err != nil {
glog.Fatalf("Error generating NGINX main config: %v", err)
}
ngxc.UpdateMainConfigFile(content)

nginxDone := make(chan error, 1)
ngxc.Start(nginxDone)
Expand All @@ -166,7 +187,7 @@ func main() {
}
}

cnf := nginx.NewConfigurator(ngxc, cfg, nginxAPI)
cnf := nginx.NewConfigurator(ngxc, cfg, nginxAPI, templateExecutor)
lbc := controller.NewLoadBalancerController(kubeClient, 30*time.Second, *watchNamespace, cnf, *nginxConfigMaps, *defaultServerSecret, *nginxPlus, *ingressClass, *useIngressClassOnly)
go handleTermination(lbc, ngxc, nginxDone)
lbc.Run()
Expand Down
9 changes: 9 additions & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type Config struct {
MainServerSSLDHParam string
MainServerSSLDHParamFileContent *string

MainTemplate *string
IngressTemplate *string

JWTRealm string
JWTKey string
JWTToken string
Expand Down Expand Up @@ -337,5 +340,11 @@ func ParseConfigMap(cfgm *api_v1.ConfigMap, nginxPlus bool) *Config {
if failTimeout, exists := cfgm.Data["fail-timeout"]; exists {
cfg.FailTimeout = failTimeout
}
if mainTemplate, exists := cfgm.Data["main-template"]; exists {
cfg.MainTemplate = &mainTemplate
}
if ingressTemplate, exists := cfgm.Data["ingress-template"]; exists {
cfg.IngressTemplate = &ingressTemplate
}
return cfg
}
68 changes: 51 additions & 17 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ const JWTKeyAnnotation = "nginx.com/jwt-key"

// Configurator transforms an Ingress resource into NGINX Configuration
type Configurator struct {
nginx *NginxController
config *Config
nginxAPI *plus.NginxAPIController
ingresses map[string]*IngressEx
nginx *NginxController
config *Config
nginxAPI *plus.NginxAPIController
ingresses map[string]*IngressEx
templateExecutor *TemplateExecutor
}

// NewConfigurator creates a new Configurator
func NewConfigurator(nginx *NginxController, config *Config, nginxAPI *plus.NginxAPIController) *Configurator {
func NewConfigurator(nginx *NginxController, config *Config, nginxAPI *plus.NginxAPIController, templateExecutor *TemplateExecutor) *Configurator {
cnf := Configurator{
nginx: nginx,
config: config,
nginxAPI: nginxAPI,
ingresses: make(map[string]*IngressEx),
nginx: nginx,
config: config,
nginxAPI: nginxAPI,
ingresses: make(map[string]*IngressEx),
templateExecutor: templateExecutor,
}

return &cnf
Expand All @@ -60,12 +62,17 @@ func (cnf *Configurator) AddOrUpdateIngress(ingEx *IngressEx) error {
return nil
}

func (cnf *Configurator) addOrUpdateIngress(ingEx *IngressEx) {
func (cnf *Configurator) addOrUpdateIngress(ingEx *IngressEx) error {
pems, jwtKeyFileName := cnf.updateSecrets(ingEx)
nginxCfg := cnf.generateNginxCfg(ingEx, pems, jwtKeyFileName)
name := objectMetaToFileName(&ingEx.Ingress.ObjectMeta)
cnf.nginx.AddOrUpdateIngress(name, nginxCfg)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return fmt.Errorf("Error generating Ingress Config %v: %v", name, err)
}
cnf.nginx.UpdateIngressConfigFile(name, content)
cnf.ingresses[name] = ingEx
return nil
}

// AddOrUpdateMergableIngress adds or updates NGINX configuration for the Ingress resources with Mergeable Types
Expand All @@ -78,11 +85,16 @@ func (cnf *Configurator) AddOrUpdateMergableIngress(mergeableIngs *MergeableIngr
return nil
}

func (cnf *Configurator) addOrUpdateMergableIngress(mergeableIngs *MergeableIngresses) {
func (cnf *Configurator) addOrUpdateMergableIngress(mergeableIngs *MergeableIngresses) error {
nginxCfg := cnf.generateNginxCfgForMergeableIngresses(mergeableIngs)
name := objectMetaToFileName(&mergeableIngs.Master.Ingress.ObjectMeta)
cnf.nginx.AddOrUpdateIngress(name, nginxCfg)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return fmt.Errorf("Error generating Ingress Config %v: %v", name, err)
}
cnf.nginx.UpdateIngressConfigFile(name, content)
cnf.ingresses[name] = mergeableIngs.Master
return nil
}

func (cnf *Configurator) generateNginxCfgForMergeableIngresses(mergeableIngs *MergeableIngresses) IngressNginxConfig {
Expand Down Expand Up @@ -1047,15 +1059,37 @@ func (cnf *Configurator) UpdateConfig(config *Config, ingExes []*IngressEx, merg
}
config.MainServerSSLDHParam = fileName
}

if config.MainTemplate != nil {
err := cnf.templateExecutor.UpdateMainTemplate(config.MainTemplate)
if err != nil {
return fmt.Errorf("Error when parsing the main template: %v", err)
}
}
if config.IngressTemplate != nil {
err := cnf.templateExecutor.UpdateIngressTemplate(config.IngressTemplate)
if err != nil {
return fmt.Errorf("Error when parsing the ingress template: %v", err)
}
}

mainCfg := GenerateNginxMainConfig(config)
cnf.nginx.UpdateMainConfigFile(mainCfg)

for _, ingEx := range ingExes {
cnf.addOrUpdateIngress(ingEx)
mainCfgContent, err := cnf.templateExecutor.ExecuteMainConfigTemplate(mainCfg)
if err != nil {
return fmt.Errorf("Error when writing main Config")
}
cnf.nginx.UpdateMainConfigFile(mainCfgContent)

for _, ingEx := range ingExes {
if err := cnf.addOrUpdateIngress(ingEx); err != nil {
return err
}
}
for _, mergeableIng := range mergeableIngs {
cnf.addOrUpdateMergableIngress(mergeableIng)
if err := cnf.addOrUpdateMergableIngress(mergeableIng); err != nil {
return err
}
}

if err := cnf.nginx.Reload(); err != nil {
Expand Down
Loading