Skip to content

Commit 560b7df

Browse files
committed
Added handling of ingress.class annotation for NGINX
1 parent 37e66f5 commit 560b7df

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Every time the number of pods of services you expose via Ingress changes, the In
4444
NGINX Plus provides you with [advanced statistics](https://www.nginx.com/products/live-activity-monitoring/), which you can access either through the API or via the built-in dashboard. This can give you insights into how NGINX Plus and your applications are performing.
4545
* **Session Persistence** When enabled, NGINX Plus makes sure that all the requests from the same client are always passed to the same backend container using the *sticky cookie* method. Refer to the [session persistence examples](examples/session-persistence) to find out how to configure it.
4646

47+
## Using Multiple Ingress Controllers
48+
49+
You can run multiple Ingress controllers at the same time. For example, if your Kubernetes cluster is deployed in cloud, you can run the NGINX controller and the corresponding cloud HTTP load balancing controller. Refer to the [example](examples/multiple-ingress-controllers) to learn more.
50+
4751
## Advanced load balancing (beyond Ingress)
4852

4953
When your requirements go beyond what Ingress offers, you can use NGINX and
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Using Multiple Ingress Controllers
2+
3+
With the *kubernetes.io/ingress.class* annotation it is possible to smoothly run multiple Ingress controllers at the same time. This could be the case when you deploy your Kubernetes cluster in a cloud provider, for example, Google Compute Engine (GCE). A cloud provider can provide its own Ingress controller enabled by default.
4+
5+
If the annotation is not present in an Ingress resource, which is the usual case, each Ingress controller deployed in the cluster will handle that Ingress. Using the annotation you can specify which Ingress controller must handle which Ingress resource.
6+
7+
To designate that a particular Ingress resource must be handled *only* by the NGINX or NGINX Plus controller add the following annotation along with the value to the Ingress resource:
8+
```
9+
kubernetes.io/ingress.class: "nginx"
10+
```
11+
12+
In this case other Ingress controllers will ignore that Ingress.
13+
14+
To summarize, the NGINX or NGINX Plus controller *will* handle an Ingress resource, if one of the following is true:
15+
* The annotation is not present in the resource
16+
* The annotation is present and its value is either the `nginx` or the empty string
17+
18+
Any other value of the annotation, for example, `gce`, makes the NGINX or NGINX Plus controller ignore the Ingress resource.
19+
20+
Here is an example of an Ingress resource that will be handled *only* by the NGINX or NGINX Plus controller:
21+
```yaml
22+
apiVersion: extensions/v1beta1
23+
kind: Ingress
24+
metadata:
25+
name: cafe-ingress-nginx
26+
annotations:
27+
kubernetes.io/ingress.class: "nginx"
28+
spec:
29+
tls:
30+
- hosts:
31+
- cafe.example.com
32+
secretName: cafe-secret
33+
rules:
34+
- host: cafe.example.com
35+
http:
36+
paths:
37+
- path: /tea
38+
backend:
39+
serviceName: tea-svc
40+
servicePort: 80
41+
- path: /coffee
42+
backend:
43+
serviceName: coffee-svc
44+
servicePort: 80
45+
```

nginx-controller/controller/controller.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import (
3434
"k8s.io/kubernetes/pkg/watch"
3535
)
3636

37+
const (
38+
ingressClassKey = "kubernetes.io/ingress.class"
39+
nginxIngressClass = "nginx"
40+
)
41+
3742
// LoadBalancerController watches Kubernetes API and
3843
// reconfigures NGINX via NginxController when needed
3944
type LoadBalancerController struct {
@@ -70,18 +75,28 @@ func NewLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Dura
7075
ingHandlers := framework.ResourceEventHandlerFuncs{
7176
AddFunc: func(obj interface{}) {
7277
addIng := obj.(*extensions.Ingress)
78+
if !isNginxIngress(addIng) {
79+
glog.Infof("Ignoring Ingress %v based on Annotation %v", addIng.Name, ingressClassKey)
80+
return
81+
}
7382
glog.V(3).Infof("Adding Ingress: %v", addIng.Name)
7483
lbc.ingQueue.enqueue(obj)
7584
},
7685
DeleteFunc: func(obj interface{}) {
7786
remIng := obj.(*extensions.Ingress)
87+
if !isNginxIngress(remIng) {
88+
return
89+
}
7890
glog.V(3).Infof("Removing Ingress: %v", remIng.Name)
7991
lbc.ingQueue.enqueue(obj)
8092
},
8193
UpdateFunc: func(old, cur interface{}) {
94+
curIng := cur.(*extensions.Ingress)
95+
if !isNginxIngress(curIng) {
96+
return
97+
}
8298
if !reflect.DeepEqual(old, cur) {
83-
glog.V(3).Infof("Ingress %v changed, syncing",
84-
cur.(*extensions.Ingress).Name)
99+
glog.V(3).Infof("Ingress %v changed, syncing", curIng.Name)
85100
lbc.ingQueue.enqueue(cur)
86101
}
87102
},
@@ -440,3 +455,11 @@ func parseNginxConfigMaps(nginxConfigMaps string) (string, string, error) {
440455
}
441456
return res[0], res[1], nil
442457
}
458+
459+
func isNginxIngress(ing *extensions.Ingress) bool {
460+
if class, exists := ing.Annotations[ingressClassKey]; exists {
461+
return class == nginxIngressClass || class == ""
462+
}
463+
464+
return true
465+
}

nginx-plus-controller/controller/controller.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import (
3434
"k8s.io/kubernetes/pkg/watch"
3535
)
3636

37+
const (
38+
ingressClassKey = "kubernetes.io/ingress.class"
39+
nginxIngressClass = "nginx"
40+
)
41+
3742
// LoadBalancerController watches Kubernetes API and
3843
// reconfigures NGINX via NginxController when needed
3944
type LoadBalancerController struct {
@@ -70,18 +75,28 @@ func NewLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Dura
7075
ingHandlers := framework.ResourceEventHandlerFuncs{
7176
AddFunc: func(obj interface{}) {
7277
addIng := obj.(*extensions.Ingress)
78+
if !isNginxIngress(addIng) {
79+
glog.Infof("Ignoring Ingress %v based on Annotation %v", addIng.Name, ingressClassKey)
80+
return
81+
}
7382
glog.V(3).Infof("Adding Ingress: %v", addIng.Name)
7483
lbc.ingQueue.enqueue(obj)
7584
},
7685
DeleteFunc: func(obj interface{}) {
7786
remIng := obj.(*extensions.Ingress)
87+
if !isNginxIngress(remIng) {
88+
return
89+
}
7890
glog.V(3).Infof("Removing Ingress: %v", remIng.Name)
7991
lbc.ingQueue.enqueue(obj)
8092
},
8193
UpdateFunc: func(old, cur interface{}) {
94+
curIng := cur.(*extensions.Ingress)
95+
if !isNginxIngress(curIng) {
96+
return
97+
}
8298
if !reflect.DeepEqual(old, cur) {
83-
glog.V(3).Infof("Ingress %v changed, syncing",
84-
cur.(*extensions.Ingress).Name)
99+
glog.V(3).Infof("Ingress %v changed, syncing", curIng.Name)
85100
lbc.ingQueue.enqueue(cur)
86101
}
87102
},
@@ -440,3 +455,11 @@ func parseNginxConfigMaps(nginxConfigMaps string) (string, string, error) {
440455
}
441456
return res[0], res[1], nil
442457
}
458+
459+
func isNginxIngress(ing *extensions.Ingress) bool {
460+
if class, exists := ing.Annotations[ingressClassKey]; exists {
461+
return class == nginxIngressClass || class == ""
462+
}
463+
464+
return true
465+
}

0 commit comments

Comments
 (0)