Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce reloads #362

Merged
merged 3 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,44 +277,55 @@ func (lbc *LoadBalancerController) syncEndpoint(task queue.Task) {
if endpExists {
ings := lbc.getIngressForEndpoints(obj)

for _, ing := range ings {
if !lbc.IsNginxIngress(&ing) {
var ingExes []*nginx.IngressEx
var mergableIngressesSlice []*nginx.MergeableIngresses

for i := range ings {
if !lbc.IsNginxIngress(&ings[i]) {
continue
}
if utils.IsMinion(&ing) {
master, err := lbc.FindMasterForMinion(&ing)
if utils.IsMinion(&ings[i]) {
master, err := lbc.FindMasterForMinion(&ings[i])
if err != nil {
glog.Errorf("Ignoring Ingress %v(Minion): %v", ing.Name, err)
glog.Errorf("Ignoring Ingress %v(Minion): %v", ings[i].Name, err)
continue
}
if !lbc.configurator.HasIngress(master) {
if !lbc.configurator.HasMinion(master, &ings[i]) {
continue
}
mergeableIngresses, err := lbc.createMergableIngresses(master)
if err != nil {
glog.Errorf("Ignoring Ingress %v(Minion): %v", ing.Name, err)
glog.Errorf("Ignoring Ingress %v(Minion): %v", ings[i].Name, err)
continue
}

glog.V(3).Infof("Updating Endpoints for %v/%v", ing.Namespace, ing.Name)
err = lbc.configurator.UpdateEndpointsMergeableIngress(mergeableIngresses)
if err != nil {
glog.Errorf("Error updating endpoints for %v/%v: %v", ing.Namespace, ing.Name, err)
}
mergableIngressesSlice = append(mergableIngressesSlice, mergeableIngresses)
continue
}
if !lbc.configurator.HasIngress(&ing) {
if !lbc.configurator.HasIngress(&ings[i]) {
continue
}
ingEx, err := lbc.createIngress(&ing)
ingEx, err := lbc.createIngress(&ings[i])
if err != nil {
glog.Errorf("Error updating endpoints for %v/%v: %v, skipping", ing.Namespace, ing.Name, err)
glog.Errorf("Error updating endpoints for %v/%v: %v, skipping", &ings[i].Namespace, &ings[i].Name, err)
continue
}
glog.V(3).Infof("Updating Endpoints for %v/%v", ing.Namespace, ing.Name)
err = lbc.configurator.UpdateEndpoints(ingEx)
ingExes = append(ingExes, ingEx)
}

if len(ingExes) > 0 {
glog.V(3).Infof("Updating Endpoints for %v", ingExes)
err = lbc.configurator.UpdateEndpoints(ingExes)
if err != nil {
glog.Errorf("Error updating endpoints for %v/%v: %v", ing.Namespace, ing.Name, err)
glog.Errorf("Error updating endpoints for %v: %v", ingExes, err)
}
}

if len(mergableIngressesSlice) > 0 {
glog.V(3).Infof("Updating Endpoints for %v", mergableIngressesSlice)
err = lbc.configurator.UpdateEndpointsMergeableIngress(mergableIngressesSlice)
if err != nil {
glog.Errorf("Error updating endpoints for %v: %v", mergableIngressesSlice, err)
}
}
}
Expand Down Expand Up @@ -1130,7 +1141,6 @@ func (lbc *LoadBalancerController) IsNginxIngress(ing *extensions.Ingress) bool
return class == lbc.ingressClass || class == ""
}
return !lbc.useIngressClassOnly

}

// isHealthCheckEnabled checks if health checks are enabled so we can only query pods if enabled.
Expand Down
69 changes: 41 additions & 28 deletions internal/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,51 +962,64 @@ func (cnf *Configurator) DeleteIngress(key string) error {
return nil
}

// UpdateEndpoints updates endpoints in NGINX configuration for the Ingress resource
func (cnf *Configurator) UpdateEndpoints(ingEx *IngressEx) error {
err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
// UpdateEndpoints updates endpoints in NGINX configuration for the Ingress resources
func (cnf *Configurator) UpdateEndpoints(ingExes []*IngressEx) error {
reloadPlus := false

if cnf.isPlus() {
err = cnf.updatePlusEndpoints(ingEx)
if err == nil {
return nil
for _, ingEx := range ingExes {
err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)

if cnf.isPlus() {
err := cnf.updatePlusEndpoints(ingEx)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
reloadPlus = true
}
}
}

if cnf.isPlus() && !reloadPlus {
glog.V(3).Info("No need to reload nginx")
return nil
}

if err := cnf.nginx.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
return fmt.Errorf("Error reloading NGINX when updating endpoints: %v", err)
}

return nil
}

// UpdateEndpointsMergeableIngress updates endpoints in NGINX configuration for a mergeable Ingress resource
func (cnf *Configurator) UpdateEndpointsMergeableIngress(mergeableIngs *MergeableIngresses) error {
err := cnf.addOrUpdateMergeableIngress(mergeableIngs)
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
}
func (cnf *Configurator) UpdateEndpointsMergeableIngress(mergableIngressesSlice []*MergeableIngresses) error {
reloadPlus := false
for i := range mergableIngressesSlice {
err := cnf.addOrUpdateMergeableIngress(mergableIngressesSlice[i])
if err != nil {
return fmt.Errorf("Error adding or updating mergeableIngress %v/%v: %v", mergableIngressesSlice[i].Master.Ingress.Namespace, mergableIngressesSlice[i].Master.Ingress.Name, err)
}

if cnf.isPlus() {
for _, ing := range mergeableIngs.Minions {
err = cnf.updatePlusEndpoints(ing)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
break
if cnf.isPlus() {
for _, ing := range mergableIngressesSlice[i].Minions {
err = cnf.updatePlusEndpoints(ing)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
reloadPlus = true
}
}
}
if err == nil {
return nil
}
}
if cnf.isPlus() && !reloadPlus {
glog.V(3).Info("No need to reload nginx")
return nil
}

if err := cnf.nginx.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v/%v: %v", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v: %v", mergableIngressesSlice, err)
}

return nil
}

Expand Down
16 changes: 10 additions & 6 deletions internal/nginx/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,15 @@ func TestUpdateEndpoints(t *testing.T) {
}

ingress := createCafeIngressEx()
err = cnf.UpdateEndpoints(&ingress)
ingresses := []*IngressEx{&ingress}
err = cnf.UpdateEndpoints(ingresses)
if err != nil {
t.Errorf("UpdateEndpoints returned\n%v, but expected \n%v", err, nil)
}

// test with OSS Configurator
cnf.nginxAPI = nil
err = cnf.UpdateEndpoints(&ingress)
err = cnf.UpdateEndpoints(ingresses)
if err != nil {
t.Errorf("UpdateEndpoints returned\n%v, but expected \n%v", err, nil)
}
Expand All @@ -738,14 +739,15 @@ func TestUpdateEndpointsMergeableIngress(t *testing.T) {
}

mergeableIngress := createMergeableCafeIngress()
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngress)
mergeableIngresses := []*MergeableIngresses{mergeableIngress}
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngresses)
if err != nil {
t.Errorf("UpdateEndpointsMergeableIngress returned \n%v, but expected \n%v", err, nil)
}

// test with OSS Configurator
cnf.nginxAPI = nil
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngress)
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngresses)
if err != nil {
t.Errorf("UpdateEndpointsMergeableIngress returned \n%v, but expected \n%v", err, nil)
}
Expand All @@ -758,7 +760,8 @@ func TestUpdateEndpointsFailsWithInvalidTemplate(t *testing.T) {
}

ingress := createCafeIngressEx()
err = cnf.UpdateEndpoints(&ingress)
ingresses := []*IngressEx{&ingress}
err = cnf.UpdateEndpoints(ingresses)
if err == nil {
t.Errorf("UpdateEndpoints returned\n%v, but expected \n%v", nil, "template execution error")
}
Expand All @@ -771,7 +774,8 @@ func TestUpdateEndpointsMergeableIngressFailsWithInvalidTemplate(t *testing.T) {
}

mergeableIngress := createMergeableCafeIngress()
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngress)
mergeableIngresses := []*MergeableIngresses{mergeableIngress}
err = cnf.UpdateEndpointsMergeableIngress(mergeableIngresses)
if err == nil {
t.Errorf("UpdateEndpointsMergeableIngress returned \n%v, but expected \n%v", nil, "template execution error")
}
Expand Down
10 changes: 10 additions & 0 deletions internal/nginx/ingress.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nginx

import (
"fmt"

api_v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
)
Expand Down Expand Up @@ -60,3 +62,11 @@ var minionInheritanceList = map[string]bool{
"nginx.org/max-fails": true,
"nginx.org/fail-timeout": true,
}

func (ingEx *IngressEx) String() string {
if ingEx.Ingress == nil {
return "IngressEx has no Ingress"
}

return fmt.Sprintf("%v/%v", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
}