Skip to content

Commit 201ebcb

Browse files
author
Fernando Diaz
committed
Correct Mergeable Types misspelling and optimize blacklists
Corrects incorrect spelling of 'mergeable' in different locations. Also optimizes filtering and merging functions by converting the blacklists to maps instead of arrays. Fixes nginx#269
1 parent 56665db commit 201ebcb

File tree

11 files changed

+68
-67
lines changed

11 files changed

+68
-67
lines changed

examples/mergable-ingress-types/README.md renamed to examples/mergeable-ingress-types/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ for easier management when using a large amount of paths.
55

66
## Syntax and Rules
77

8-
A Master is declared using `nginx.org/mergible-ingress-type: master`. A Master will process all configurations at the
8+
A Master is declared using `nginx.org/mergeable-ingress-type: master`. A Master will process all configurations at the
99
host level, which includes the TLS configuration, and any annotations which will be applied for the complete host. There
1010
can only be one ingress resource on a unique host that contains the master value. Paths cannot be part of the
1111
ingress resource.
@@ -16,7 +16,7 @@ Masters cannot contain the following annotations:
1616
* nginx.org/websocket-services
1717
* nginx.com/sticky-cookie-services
1818

19-
A Minion is declared using `nginx.org/mergible-ingress-type: minion`. A Minion will be used to append different
19+
A Minion is declared using `nginx.org/mergeable-ingress-type: minion`. A Minion will be used to append different
2020
locations to an ingress resource with the Master value. TLS configurations are not allowed. Multiple minions can be
2121
applied per master as long as they do not have conflicting paths. If a conflicting path is present then the path defined
2222
on the oldest minion will be used.
@@ -42,7 +42,7 @@ Note: Ingress Resources with more than one host cannot be used.
4242
## Example
4343

4444
In this example we deploy the NGINX Ingress controller, a simple web application and then configure
45-
load balancing for that application using Ingress resources with the `nginx.org/mergible-ingress-type` annotations.
45+
load balancing for that application using Ingress resources with the `nginx.org/mergeable-ingress-type` annotations.
4646

4747
## Running the Example
4848

examples/mergable-ingress-types/cafe-master.yaml renamed to examples/mergeable-ingress-types/cafe-master.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: cafe-ingress-master
55
annotations:
66
kubernetes.io/ingress.class: "nginx"
7-
nginx.org/mergible-ingress-type: "master"
7+
nginx.org/mergeable-ingress-type: "master"
88
spec:
99
tls:
1010
- hosts:

examples/mergable-ingress-types/coffee-minion.yaml renamed to examples/mergeable-ingress-types/coffee-minion.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: cafe-ingress-coffee-minion
55
annotations:
66
kubernetes.io/ingress.class: "nginx"
7-
nginx.org/mergible-ingress-type: "minion"
7+
nginx.org/mergeable-ingress-type: "minion"
88
spec:
99
rules:
1010
- host: cafe.example.com

examples/mergable-ingress-types/tea-minion.yaml renamed to examples/mergeable-ingress-types/tea-minion.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: cafe-ingress-tea-minion
55
annotations:
66
kubernetes.io/ingress.class: "nginx"
7-
nginx.org/mergible-ingress-type: "minion"
7+
nginx.org/mergeable-ingress-type: "minion"
88
spec:
99
rules:
1010
- host: cafe.example.com

nginx-controller/controller/controller.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -783,21 +783,21 @@ func (lbc *LoadBalancerController) syncIng(task Task) {
783783
ing := obj.(*extensions.Ingress)
784784

785785
if isMaster(ing) {
786-
mergableIngExs, err := lbc.createMergableIngresses(ing)
786+
mergeableIngExs, err := lbc.createMergableIngresses(ing)
787787
if err != nil {
788788
lbc.syncQueue.requeueAfter(task, err, 5*time.Second)
789789
lbc.recorder.Eventf(ing, api_v1.EventTypeWarning, "Rejected", "%v was rejected: %v", key, err)
790790
return
791791
}
792-
err = lbc.cnf.AddOrUpdateMergableIngress(mergableIngExs)
792+
err = lbc.cnf.AddOrUpdateMergableIngress(mergeableIngExs)
793793
if err != nil {
794794
lbc.recorder.Eventf(ing, api_v1.EventTypeWarning, "AddedOrUpdatedWithError", "Configuration for %v(Master) was added or updated, but not applied: %v", key, err)
795-
for _, minion := range mergableIngExs.Minions {
795+
for _, minion := range mergeableIngExs.Minions {
796796
lbc.recorder.Eventf(ing, api_v1.EventTypeWarning, "AddedOrUpdatedWithError", "Configuration for %v/%v(Minion) was added or updated, but not applied: %v", minion.Ingress.Namespace, minion.Ingress.Name, err)
797797
}
798798
} else {
799799
lbc.recorder.Eventf(ing, api_v1.EventTypeNormal, "AddedOrUpdated", "Configuration for %v(Master) was added or updated", key)
800-
for _, minion := range mergableIngExs.Minions {
800+
for _, minion := range mergeableIngExs.Minions {
801801
lbc.recorder.Eventf(ing, api_v1.EventTypeNormal, "AddedOrUpdated", "Configuration for %v/%v(Minion) was added or updated", minion.Ingress.Namespace, minion.Ingress.Name)
802802
}
803803
}
@@ -1233,18 +1233,18 @@ func (lbc *LoadBalancerController) getMinionsForMaster(master *nginx.IngressEx)
12331233
continue
12341234
}
12351235
if len(ings.Items[i].Spec.Rules) != 1 {
1236-
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation must contain only one host", ings.Items[i].Namespace, ings.Items[i].Name)
1236+
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation must contain only one host", ings.Items[i].Namespace, ings.Items[i].Name)
12371237
continue
12381238
}
12391239
if ings.Items[i].Spec.Rules[0].HTTP == nil {
1240-
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation set to 'minion' must contain a Path", ings.Items[i].Namespace, ings.Items[i].Name)
1240+
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation set to 'minion' must contain a Path", ings.Items[i].Namespace, ings.Items[i].Name)
12411241
continue
12421242
}
12431243

12441244
uniquePaths := []extensions.HTTPIngressPath{}
12451245
for _, path := range ings.Items[i].Spec.Rules[0].HTTP.Paths {
12461246
if val, ok := minionPaths[path.Path]; ok {
1247-
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation set to 'minion' cannot contain the same path as another ingress resource, %v/%v.",
1247+
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation set to 'minion' cannot contain the same path as another ingress resource, %v/%v.",
12481248
ings.Items[i].Namespace, ings.Items[i].Name, val.Namespace, val.Name)
12491249
glog.Errorf("Path %s for Ingress Resource %v/%v will be ignored", path.Path, val.Namespace, val.Name)
12501250
} else {
@@ -1260,7 +1260,7 @@ func (lbc *LoadBalancerController) getMinionsForMaster(master *nginx.IngressEx)
12601260
continue
12611261
}
12621262
if len(ingEx.TLSSecrets) > 0 || ingEx.JWTKey != nil {
1263-
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation set to 'minion' cannot contain TLSSecrets or JWTKeys", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
1263+
glog.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation set to 'minion' cannot contain TLSSecrets or JWTKeys", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
12641264
continue
12651265
}
12661266
minions = append(minions, ingEx)
@@ -1300,15 +1300,15 @@ func (lbc *LoadBalancerController) createMergableIngresses(master *extensions.In
13001300
mergeableIngresses := nginx.MergeableIngresses{}
13011301

13021302
if len(master.Spec.Rules) != 1 {
1303-
err := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation must contain only one host", master.Namespace, master.Name)
1303+
err := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation must contain only one host", master.Namespace, master.Name)
13041304
return &mergeableIngresses, err
13051305
}
13061306

13071307
var empty extensions.HTTPIngressRuleValue
13081308
if master.Spec.Rules[0].HTTP != nil {
13091309
if master.Spec.Rules[0].HTTP != &empty {
13101310
if len(master.Spec.Rules[0].HTTP.Paths) != 0 {
1311-
err := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation set to 'master' cannot contain Paths", master.Namespace, master.Name)
1311+
err := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation set to 'master' cannot contain Paths", master.Namespace, master.Name)
13121312
return &mergeableIngresses, err
13131313
}
13141314
}
@@ -1337,14 +1337,14 @@ func (lbc *LoadBalancerController) createMergableIngresses(master *extensions.In
13371337
}
13381338

13391339
func isMinion(ing *extensions.Ingress) bool {
1340-
if ing.Annotations["nginx.org/mergible-ingress-type"] == "minion" {
1340+
if ing.Annotations["nginx.org/mergeable-ingress-type"] == "minion" {
13411341
return true
13421342
}
13431343
return false
13441344
}
13451345

13461346
func isMaster(ing *extensions.Ingress) bool {
1347-
if ing.Annotations["nginx.org/mergible-ingress-type"] == "master" {
1347+
if ing.Annotations["nginx.org/mergeable-ingress-type"] == "master" {
13481348
return true
13491349
}
13501350
return false

nginx-controller/controller/controller_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,21 @@ func TestCreateMergableIngresses(t *testing.T) {
161161
lbc.ingLister.Add(&coffeeMinion)
162162
lbc.ingLister.Add(&teaMinion)
163163

164-
mergableIngresses, err := lbc.createMergableIngresses(&cafeMaster)
164+
mergeableIngresses, err := lbc.createMergableIngresses(&cafeMaster)
165165
if err != nil {
166166
t.Errorf("Error creating Mergable Ingresses: %v", err)
167167
}
168-
if mergableIngresses.Master.Ingress.Name != cafeMaster.Name && mergableIngresses.Master.Ingress.Namespace != cafeMaster.Namespace {
168+
if mergeableIngresses.Master.Ingress.Name != cafeMaster.Name && mergeableIngresses.Master.Ingress.Namespace != cafeMaster.Namespace {
169169
t.Errorf("Master %s not set properly", cafeMaster.Name)
170170
}
171171

172-
if len(mergableIngresses.Minions) != 2 {
173-
t.Errorf("Invalid amount of minions in mergableIngresses: %v", mergableIngresses.Minions)
172+
if len(mergeableIngresses.Minions) != 2 {
173+
t.Errorf("Invalid amount of minions in mergeableIngresses: %v", mergeableIngresses.Minions)
174174
}
175175

176176
coffeeCount := 0
177177
teaCount := 0
178-
for _, minion := range mergableIngresses.Minions {
178+
for _, minion := range mergeableIngresses.Minions {
179179
if minion.Ingress.Name == coffeeMinion.Name {
180180
coffeeCount++
181181
} else if minion.Ingress.Name == teaMinion.Name {
@@ -220,7 +220,7 @@ func TestCreateMergableIngressesInvalidMaster(t *testing.T) {
220220
}
221221
lbc.ingLister.Add(&cafeMaster)
222222

223-
expected := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergible-ingress-type' annotation set to 'master' cannot contain Paths", cafeMaster.Namespace, cafeMaster.Name)
223+
expected := fmt.Errorf("Ingress Resource %v/%v with the 'nginx.org/mergeable-ingress-type' annotation set to 'master' cannot contain Paths", cafeMaster.Namespace, cafeMaster.Name)
224224
_, err := lbc.createMergableIngresses(&cafeMaster)
225225
if !reflect.DeepEqual(err, expected) {
226226
t.Errorf("Error Validating the Ingress Resource: \n Expected: %s \n Obtained: %s", expected, err)
@@ -467,8 +467,8 @@ func getMergableDefaults() (cafeMaster, coffeeMinion, teaMinion extensions.Ingre
467467
Name: "cafe-master",
468468
Namespace: "default",
469469
Annotations: map[string]string{
470-
"kubernetes.io/ingress.class": "nginx",
471-
"nginx.org/mergible-ingress-type": "master",
470+
"kubernetes.io/ingress.class": "nginx",
471+
"nginx.org/mergeable-ingress-type": "master",
472472
},
473473
},
474474
Spec: extensions.IngressSpec{
@@ -486,8 +486,8 @@ func getMergableDefaults() (cafeMaster, coffeeMinion, teaMinion extensions.Ingre
486486
Name: "coffee-minion",
487487
Namespace: "default",
488488
Annotations: map[string]string{
489-
"kubernetes.io/ingress.class": "nginx",
490-
"nginx.org/mergible-ingress-type": "minion",
489+
"kubernetes.io/ingress.class": "nginx",
490+
"nginx.org/mergeable-ingress-type": "minion",
491491
},
492492
},
493493
Spec: extensions.IngressSpec{
@@ -520,8 +520,8 @@ func getMergableDefaults() (cafeMaster, coffeeMinion, teaMinion extensions.Ingre
520520
Name: "tea-minion",
521521
Namespace: "default",
522522
Annotations: map[string]string{
523-
"kubernetes.io/ingress.class": "nginx",
524-
"nginx.org/mergible-ingress-type": "minion",
523+
"kubernetes.io/ingress.class": "nginx",
524+
"nginx.org/mergeable-ingress-type": "minion",
525525
},
526526
},
527527
Spec: extensions.IngressSpec{

nginx-controller/nginx/configurator.go

+11-17
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (cnf *Configurator) addOrUpdateMergableIngress(mergeableIngs *MergeableIngr
8787

8888
removedAnnotations = filterMasterAnnotations(mergeableIngs.Master.Ingress.Annotations)
8989
if len(removedAnnotations) != 0 {
90-
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.com/mergeable-ingress-type' set to 'master' cannot contain the '%v' annotation(s). They will be ignored",
90+
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.org/mergeable-ingress-type' set to 'master' cannot contain the '%v' annotation(s). They will be ignored",
9191
mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, strings.Join(removedAnnotations, ","))
9292
}
9393

@@ -116,7 +116,7 @@ func (cnf *Configurator) addOrUpdateMergableIngress(mergeableIngs *MergeableIngr
116116

117117
removedAnnotations = filterMinionAnnotations(minion.Ingress.Annotations)
118118
if len(removedAnnotations) != 0 {
119-
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.com/mergeable-ingress-type' set to 'minion' cannot contain the %v annotation(s). They will be ignored",
119+
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.org/mergeable-ingress-type' set to 'minion' cannot contain the %v annotation(s). They will be ignored",
120120
minion.Ingress.Namespace, minion.Ingress.Name, strings.Join(removedAnnotations, ","))
121121
}
122122

@@ -807,10 +807,10 @@ func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) error {
807807
func filterMasterAnnotations(annotations map[string]string) []string {
808808
var removedAnnotations []string
809809

810-
for _, blacklistAnn := range masterBlacklist {
811-
if _, ok := annotations[blacklistAnn]; ok {
812-
removedAnnotations = append(removedAnnotations, blacklistAnn)
813-
delete(annotations, blacklistAnn)
810+
for key, _ := range annotations {
811+
if _, ok := masterBlacklist[key]; ok {
812+
removedAnnotations = append(removedAnnotations, key)
813+
delete(annotations, key)
814814
}
815815
}
816816

@@ -820,10 +820,10 @@ func filterMasterAnnotations(annotations map[string]string) []string {
820820
func filterMinionAnnotations(annotations map[string]string) []string {
821821
var removedAnnotations []string
822822

823-
for _, blacklistAnn := range minionBlacklist {
824-
if _, ok := annotations[blacklistAnn]; ok {
825-
removedAnnotations = append(removedAnnotations, blacklistAnn)
826-
delete(annotations, blacklistAnn)
823+
for key, _ := range annotations {
824+
if _, ok := minionBlacklist[key]; ok {
825+
removedAnnotations = append(removedAnnotations, key)
826+
delete(annotations, key)
827827
}
828828
}
829829

@@ -832,14 +832,8 @@ func filterMinionAnnotations(annotations map[string]string) []string {
832832

833833
func mergeMasterAnnotationsIntoMinion(minionAnnotations map[string]string, masterAnnotations map[string]string) {
834834
for key, val := range masterAnnotations {
835-
isBlacklisted := false
836835
if _, ok := minionAnnotations[key]; !ok {
837-
for _, blacklistAnn := range minionBlacklist {
838-
if blacklistAnn == key {
839-
isBlacklisted = true
840-
}
841-
}
842-
if !isBlacklisted {
836+
if _, ok := minionBlacklist[key]; !ok {
843837
minionAnnotations[key] = val
844838
}
845839
}

nginx-controller/nginx/configurator_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nginx
22

33
import (
44
"reflect"
5+
"sort"
56
"testing"
67
)
78

@@ -83,6 +84,9 @@ func TestFilterMasterAnnotations(t *testing.T) {
8384
"nginx.org/ssl-services",
8485
}
8586

87+
sort.Strings(removedAnnotations)
88+
sort.Strings(expectedRemovedAnnotations)
89+
8690
if !reflect.DeepEqual(expectedfilteredMasterAnnotations, masterAnnotations) {
8791
t.Errorf("filterMasterAnnotations returned %v, but expected %v", masterAnnotations, expectedfilteredMasterAnnotations)
8892
}
@@ -111,6 +115,9 @@ func TestFilterMinionAnnotations(t *testing.T) {
111115
"nginx.org/hsts-include-subdomains",
112116
}
113117

118+
sort.Strings(removedAnnotations)
119+
sort.Strings(expectedRemovedAnnotations)
120+
114121
if !reflect.DeepEqual(expectedfilteredMinionAnnotations, minionAnnotations) {
115122
t.Errorf("filterMinionAnnotations returned %v, but expected %v", minionAnnotations, expectedfilteredMinionAnnotations)
116123
}

nginx-controller/nginx/ingress.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ type MergeableIngresses struct {
1919
Minions []*IngressEx
2020
}
2121

22-
var masterBlacklist = []string{
23-
"nginx.org/rewrites",
24-
"nginx.org/ssl-services",
25-
"nginx.org/websocket-services",
26-
"nginx.com/sticky-cookie-services",
22+
var masterBlacklist = map[string]bool{
23+
"nginx.org/rewrites": true,
24+
"nginx.org/ssl-services": true,
25+
"nginx.org/websocket-services": true,
26+
"nginx.com/sticky-cookie-services": true,
2727
}
2828

29-
var minionBlacklist = []string{
30-
"nginx.org/proxy-hide-headers",
31-
"nginx.org/proxy-pass-headers",
32-
"nginx.org/redirect-to-https",
33-
"ingress.kubernetes.io/ssl-redirect",
34-
"nginx.org/hsts",
35-
"nginx.org/hsts-max-age",
36-
"nginx.org/hsts-include-subdomains",
37-
"nginx.org/server-tokens",
38-
"nginx.org/listen-ports",
39-
"nginx.org/listen-ports-ssl",
40-
"nginx.com/jwt-key",
41-
"nginx.com/jwt-realm",
42-
"nginx.com/jwt-token",
43-
"nginx.com/jwt-login-url",
29+
var minionBlacklist = map[string]bool{
30+
"nginx.org/proxy-hide-headers": true,
31+
"nginx.org/proxy-pass-headers": true,
32+
"nginx.org/redirect-to-https": true,
33+
"ingress.kubernetes.io/ssl-redirect": true,
34+
"nginx.org/hsts": true,
35+
"nginx.org/hsts-max-age": true,
36+
"nginx.org/hsts-include-subdomains": true,
37+
"nginx.org/server-tokens": true,
38+
"nginx.org/listen-ports": true,
39+
"nginx.org/listen-ports-ssl": true,
40+
"nginx.com/jwt-key": true,
41+
"nginx.com/jwt-realm": true,
42+
"nginx.com/jwt-token": true,
43+
"nginx.com/jwt-login-url": true,
4444
}

0 commit comments

Comments
 (0)