From fe1b913f21903e0ce95bfd00d1da42bcb764154c Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Fri, 28 Jul 2017 18:57:33 -0400 Subject: [PATCH 1/2] Add function to allow custom values in Ingress status --- controllers/nginx/pkg/cmd/controller/nginx.go | 6 ++++++ core/pkg/ingress/controller/controller.go | 1 + core/pkg/ingress/status/status.go | 20 +++++++++++++++---- core/pkg/ingress/status/status_test.go | 6 ++++++ core/pkg/ingress/types.go | 5 +++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/controllers/nginx/pkg/cmd/controller/nginx.go b/controllers/nginx/pkg/cmd/controller/nginx.go index b551f9ddd..486300245 100644 --- a/controllers/nginx/pkg/cmd/controller/nginx.go +++ b/controllers/nginx/pkg/cmd/controller/nginx.go @@ -35,6 +35,7 @@ import ( proxyproto "github.com/armon/go-proxyproto" api_v1 "k8s.io/api/core/v1" + extensions "k8s.io/api/extensions/v1beta1" "k8s.io/ingress/controllers/nginx/pkg/config" ngx_template "k8s.io/ingress/controllers/nginx/pkg/template" @@ -373,6 +374,11 @@ func (n *NGINXController) SetListers(lister ingress.StoreLister) { n.storeLister = lister } +// UpdateIngressStatus custom Ingress status update +func (n *NGINXController) UpdateIngressStatus(*extensions.Ingress) []api_v1.LoadBalancerIngress { + return nil +} + // OnUpdate is called by syncQueue in https://github.com/aledbf/ingress-controller/blob/master/pkg/ingress/controller/controller.go#L82 // periodically to keep the configuration in sync. // diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 54b4966d5..79cf1d5bd 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -299,6 +299,7 @@ func newIngressController(config *Configuration) *GenericController { IngressClass: config.IngressClass, DefaultIngressClass: config.DefaultIngressClass, UpdateStatusOnShutdown: config.UpdateStatusOnShutdown, + CustomIngressStatus: ic.cfg.Backend.UpdateIngressStatus, }) } else { glog.Warning("Update of ingress status is disabled (flag --update-status=false was specified)") diff --git a/core/pkg/ingress/status/status.go b/core/pkg/ingress/status/status.go index 63fdea90c..9150ba5e2 100644 --- a/core/pkg/ingress/status/status.go +++ b/core/pkg/ingress/status/status.go @@ -66,6 +66,9 @@ type Config struct { DefaultIngressClass string IngressClass string + + // CustomIngressStatus allows to set custom values in Ingress status + CustomIngressStatus func(*extensions.Ingress) []v1.LoadBalancerIngress } // statusSync keeps the status IP in each Ingress rule updated executing a periodic check @@ -315,7 +318,10 @@ func sliceToStatus(endpoints []string) []v1.LoadBalancerIngress { return lbi } -func (s *statusSync) updateStatus(newIPs []v1.LoadBalancerIngress) { +// updateStatus changes the status information of Ingress rules +// If the backend function CustomIngressStatus returns a value different +// of nil then it uses the returned value or the newIngressPoint values +func (s *statusSync) updateStatus(newIngressPoint []v1.LoadBalancerIngress) { ings := s.IngressLister.List() var wg sync.WaitGroup wg.Add(len(ings)) @@ -336,15 +342,21 @@ func (s *statusSync) updateStatus(newIPs []v1.LoadBalancerIngress) { return } + addrs := newIngressPoint + ca := s.CustomIngressStatus(currIng) + if ca != nil { + addrs = ca + } + curIPs := currIng.Status.LoadBalancer.Ingress sort.Sort(loadBalancerIngressByIP(curIPs)) - if ingressSliceEqual(newIPs, curIPs) { + if ingressSliceEqual(addrs, curIPs) { glog.V(3).Infof("skipping update of Ingress %v/%v (no change)", currIng.Namespace, currIng.Name) return } - glog.Infof("updating Ingress %v/%v status to %v", currIng.Namespace, currIng.Name, newIPs) - currIng.Status.LoadBalancer.Ingress = newIPs + glog.Infof("updating Ingress %v/%v status to %v", currIng.Namespace, currIng.Name, addrs) + currIng.Status.LoadBalancer.Ingress = addrs _, err = ingClient.UpdateStatus(currIng) if err != nil { glog.Warningf("error updating ingress rule: %v", err) diff --git a/core/pkg/ingress/status/status_test.go b/core/pkg/ingress/status/status_test.go index bcf3ff7ea..17991d6be 100644 --- a/core/pkg/ingress/status/status_test.go +++ b/core/pkg/ingress/status/status_test.go @@ -251,6 +251,9 @@ func buildStatusSync() statusSync { Client: buildSimpleClientSet(), PublishService: api_v1.NamespaceDefault + "/" + "foo", IngressLister: buildIngressListener(), + CustomIngressStatus: func(*extensions.Ingress) []api_v1.LoadBalancerIngress { + return nil + }, }, } } @@ -266,6 +269,9 @@ func TestStatusActions(t *testing.T) { DefaultIngressClass: "nginx", IngressClass: "", UpdateStatusOnShutdown: true, + CustomIngressStatus: func(*extensions.Ingress) []api_v1.LoadBalancerIngress { + return nil + }, } // create object fkSync := NewStatusSyncer(c) diff --git a/core/pkg/ingress/types.go b/core/pkg/ingress/types.go index 5564079e5..25143ed7f 100644 --- a/core/pkg/ingress/types.go +++ b/core/pkg/ingress/types.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/pflag" api "k8s.io/api/core/v1" + extensions "k8s.io/api/extensions/v1beta1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apiserver/pkg/server/healthz" @@ -92,6 +93,10 @@ type Controller interface { OverrideFlags(*pflag.FlagSet) // DefaultIngressClass just return the default ingress class DefaultIngressClass() string + // UpdateIngressStatus custom callback used to update the status in an Ingress rule + // This allows custom implementations + // If the function returns nil the standard functions will be executed. + UpdateIngressStatus(*extensions.Ingress) []api.LoadBalancerIngress } // StoreLister returns the configured stores for ingresses, services, From 1e825bc07795c944047a4a6f6a86cf92778a224b Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Fri, 28 Jul 2017 19:58:17 -0400 Subject: [PATCH 2/2] Update dummy example --- examples/custom-controller/server.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/custom-controller/server.go b/examples/custom-controller/server.go index 40798ef7d..605332e6c 100644 --- a/examples/custom-controller/server.go +++ b/examples/custom-controller/server.go @@ -25,6 +25,7 @@ import ( "github.com/spf13/pflag" api "k8s.io/api/core/v1" + extensions "k8s.io/api/extensions/v1beta1" nginxconfig "k8s.io/ingress/controllers/nginx/pkg/config" "k8s.io/ingress/core/pkg/ingress" @@ -105,3 +106,7 @@ func (n DummyController) SetListers(lister ingress.StoreLister) { func (n DummyController) DefaultIngressClass() string { return "dummy" } + +func (n DummyController) UpdateIngressStatus(*extensions.Ingress) []api.LoadBalancerIngress { + return nil +}