Skip to content

Commit

Permalink
Merge pull request #1042 from aledbf/custom-status
Browse files Browse the repository at this point in the history
Add function to allow custom values in Ingress status
  • Loading branch information
aledbf committed Jul 29, 2017
2 parents 697041a + 1e825bc commit 65e8cec
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions controllers/nginx/pkg/cmd/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
//
Expand Down
1 change: 1 addition & 0 deletions core/pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
20 changes: 16 additions & 4 deletions core/pkg/ingress/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions core/pkg/ingress/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
}
}
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions core/pkg/ingress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions examples/custom-controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 65e8cec

Please sign in to comment.