From 89bbb8d4eef1b057ff17a444b31c8a71eb14db39 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Fri, 8 Jul 2016 17:01:40 -0400 Subject: [PATCH] Add annotation to skip ingress rule --- controllers/nginx/README.md | 5 +++++ controllers/nginx/controller.go | 16 ++++++++++++++-- controllers/nginx/utils.go | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/controllers/nginx/README.md b/controllers/nginx/README.md index 3e3f29fd1..9e9bf6f0e 100644 --- a/controllers/nginx/README.md +++ b/controllers/nginx/README.md @@ -16,6 +16,7 @@ This is a nginx Ingress controller that uses [ConfigMap](https://github.com/kube * [Proxy Protocol](#proxy-protocol) * [NGINX customization](configuration.md) * [NGINX status page](#nginx-status-page) +* [Disabling NGINX ingress controller](#disabling-nginx-ingress-controller) * [Debug & Troubleshooting](#troubleshooting) * [Limitations](#limitations) * [NGINX Notes](#nginx-notes) @@ -215,6 +216,10 @@ Please check the example `example/rc-default.yaml` To extract the information in JSON format the module provides a custom URL: `/nginx_status/format/json` +### Disabling NGINX ingress controller + +Setting the annotation `kubernetes.io/ingress.class` to any value other than "nginx" or the empty string, will force the NGINX Ingress controller to ignore your Ingress. Do this if you wish to use one of the other Ingress controllers at the same time as the NGINX controller. + ### Debug & Troubleshooting diff --git a/controllers/nginx/controller.go b/controllers/nginx/controller.go index bfe7e06a0..337bc4748 100644 --- a/controllers/nginx/controller.go +++ b/controllers/nginx/controller.go @@ -150,16 +150,28 @@ func newLoadBalancerController(kubeClient *client.Client, resyncPeriod time.Dura ingEventHandler := framework.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { addIng := obj.(*extensions.Ingress) + if !isNGINXIngress(addIng) { + glog.Infof("Ignoring add for ingress %v based on annotation %v", addIng.Name, ingressClassKey) + return + } lbc.recorder.Eventf(addIng, api.EventTypeNormal, "CREATE", fmt.Sprintf("%s/%s", addIng.Namespace, addIng.Name)) lbc.ingQueue.enqueue(obj) lbc.syncQueue.enqueue(obj) }, DeleteFunc: func(obj interface{}) { - upIng := obj.(*extensions.Ingress) - lbc.recorder.Eventf(upIng, api.EventTypeNormal, "DELETE", fmt.Sprintf("%s/%s", upIng.Namespace, upIng.Name)) + delIng := obj.(*extensions.Ingress) + if !isNGINXIngress(delIng) { + glog.Infof("Ignoring add for ingress %v based on annotation %v", delIng.Name, ingressClassKey) + return + } + lbc.recorder.Eventf(delIng, api.EventTypeNormal, "DELETE", fmt.Sprintf("%s/%s", delIng.Namespace, delIng.Name)) lbc.syncQueue.enqueue(obj) }, UpdateFunc: func(old, cur interface{}) { + curIng := cur.(*extensions.Ingress) + if !isNGINXIngress(curIng) { + return + } if !reflect.DeepEqual(old, cur) { upIng := cur.(*extensions.Ingress) lbc.recorder.Eventf(upIng, api.EventTypeNormal, "UPDATE", fmt.Sprintf("%s/%s", upIng.Namespace, upIng.Name)) diff --git a/controllers/nginx/utils.go b/controllers/nginx/utils.go index b4c60a819..f317b15ba 100644 --- a/controllers/nginx/utils.go +++ b/controllers/nginx/utils.go @@ -26,6 +26,7 @@ import ( "k8s.io/kubernetes/pkg/api" apierrs "k8s.io/kubernetes/pkg/api/errors" + "k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/util/wait" @@ -246,3 +247,29 @@ func waitForPodCondition(kubeClient *unversioned.Client, ns, podName string, con return false, nil }) } + +// ingAnnotations represents Ingress annotations. +type ingAnnotations map[string]string + +const ( + // ingressClassKey picks a specific "class" for the Ingress. The controller + // only processes Ingresses with this annotation either unset, or set + // to either nginxIngressClass or the empty string. + ingressClassKey = "kubernetes.io/ingress.class" + nginxIngressClass = "nginx" +) + +func (ing ingAnnotations) ingressClass() string { + val, ok := ing[ingressClassKey] + if !ok { + return "" + } + return val +} + +// isNGINXIngress returns true if the given Ingress either doesn't specify the +// ingress.class annotation, or it's set to "nginx". +func isNGINXIngress(ing *extensions.Ingress) bool { + class := ingAnnotations(ing.ObjectMeta.Annotations).ingressClass() + return class == "" || class == nginxIngressClass +}