Skip to content

Commit 3b38473

Browse files
authored
Merge pull request #211 from nginxinc/missing-endpoints
Fix a problem with endpoints updates for Plus
2 parents cfddc11 + 65795e8 commit 3b38473

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

nginx-controller/controller/controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,14 @@ func NewLoadBalancerController(kubeClient kubernetes.Interface, resyncPeriod tim
315315

316316
// Run starts the loadbalancer controller
317317
func (lbc *LoadBalancerController) Run() {
318-
go lbc.ingController.Run(lbc.stopCh)
319318
go lbc.svcController.Run(lbc.stopCh)
320319
go lbc.endpController.Run(lbc.stopCh)
321320
go lbc.secrController.Run(lbc.stopCh)
322-
go lbc.syncQueue.run(time.Second, lbc.stopCh)
323321
if lbc.watchNginxConfigMaps {
324322
go lbc.cfgmController.Run(lbc.stopCh)
325323
}
324+
go lbc.ingController.Run(lbc.stopCh)
325+
go lbc.syncQueue.run(time.Second, lbc.stopCh)
326326
<-lbc.stopCh
327327
}
328328

@@ -350,6 +350,9 @@ func (lbc *LoadBalancerController) syncEndp(task Task) {
350350
if !lbc.isNginxIngress(&ing) {
351351
continue
352352
}
353+
if !lbc.cnf.HasIngress(&ing) {
354+
continue
355+
}
353356
ingEx, err := lbc.createIngress(&ing)
354357
if err != nil {
355358
glog.Errorf("Error updating endpoints for %v/%v: %v, skipping", ing.Namespace, ing.Name, err)
@@ -600,6 +603,9 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
600603
if !lbc.isNginxIngress(&ings.Items[i]) {
601604
continue
602605
}
606+
if !lbc.cnf.HasIngress(&ings.Items[i]) {
607+
continue
608+
}
603609
ingEx, err := lbc.createIngress(&ings.Items[i])
604610
if err != nil {
605611
continue

nginx-controller/nginx/configurator.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ const JWTKeyAnnotation = "nginx.com/jwt-key"
2727

2828
// Configurator transforms an Ingress resource into NGINX Configuration
2929
type Configurator struct {
30-
nginx *NginxController
31-
config *Config
32-
nginxAPI *plus.NginxAPIController
30+
nginx *NginxController
31+
config *Config
32+
nginxAPI *plus.NginxAPIController
33+
ingresses map[string]*IngressEx
3334
}
3435

3536
// NewConfigurator creates a new Configurator
3637
func NewConfigurator(nginx *NginxController, config *Config, nginxAPI *plus.NginxAPIController) *Configurator {
3738
cnf := Configurator{
38-
nginx: nginx,
39-
config: config,
40-
nginxAPI: nginxAPI,
39+
nginx: nginx,
40+
config: config,
41+
nginxAPI: nginxAPI,
42+
ingresses: make(map[string]*IngressEx),
4143
}
4244

4345
return &cnf
@@ -63,6 +65,7 @@ func (cnf *Configurator) addOrUpdateIngress(ingEx *IngressEx) {
6365
nginxCfg := cnf.generateNginxCfg(ingEx, pems, jwtKeyFileName)
6466
name := objectMetaToFileName(&ingEx.Ingress.ObjectMeta)
6567
cnf.nginx.AddOrUpdateIngress(name, nginxCfg)
68+
cnf.ingresses[name] = ingEx
6669
}
6770

6871
func (cnf *Configurator) updateSecrets(ingEx *IngressEx) (map[string]string, string) {
@@ -611,7 +614,9 @@ func GenerateCertAndKeyFileContent(secret *api_v1.Secret) []byte {
611614
// DeleteSecret deletes the file associated with the secret and the configuration files for the Ingress resources. NGINX is reloaded only when len(ings) > 0
612615
func (cnf *Configurator) DeleteSecret(key string, ings []extensions.Ingress) error {
613616
for _, ing := range ings {
614-
cnf.nginx.DeleteIngress(objectMetaToFileName(&ing.ObjectMeta))
617+
name := objectMetaToFileName(&ing.ObjectMeta)
618+
cnf.nginx.DeleteIngress(name)
619+
delete(cnf.ingresses, name)
615620
}
616621

617622
cnf.nginx.DeleteSecretFile(keyToFileName(key))
@@ -627,7 +632,10 @@ func (cnf *Configurator) DeleteSecret(key string, ings []extensions.Ingress) err
627632

628633
// DeleteIngress deletes NGINX configuration for the Ingress resource
629634
func (cnf *Configurator) DeleteIngress(key string) error {
630-
cnf.nginx.DeleteIngress(keyToFileName(key))
635+
name := keyToFileName(key)
636+
cnf.nginx.DeleteIngress(name)
637+
delete(cnf.ingresses, name)
638+
631639
if err := cnf.nginx.Reload(); err != nil {
632640
return fmt.Errorf("Error when removing ingress %v: %v", key, err)
633641
}
@@ -639,23 +647,27 @@ func (cnf *Configurator) UpdateEndpoints(ingEx *IngressEx) error {
639647
cnf.addOrUpdateIngress(ingEx)
640648

641649
if cnf.isPlus() {
642-
cnf.updatePlusEndpoints(ingEx)
643-
} else {
644-
if err := cnf.nginx.Reload(); err != nil {
645-
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
650+
err := cnf.updatePlusEndpoints(ingEx)
651+
if err == nil {
652+
return nil
646653
}
654+
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
655+
}
656+
if err := cnf.nginx.Reload(); err != nil {
657+
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
647658
}
659+
648660
return nil
649661
}
650662

651-
func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) {
663+
func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) error {
652664
if ingEx.Ingress.Spec.Backend != nil {
653665
name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
654666
endps, exists := ingEx.Endpoints[ingEx.Ingress.Spec.Backend.ServiceName+ingEx.Ingress.Spec.Backend.ServicePort.String()]
655667
if exists {
656668
err := cnf.nginxAPI.UpdateServers(name, endps)
657669
if err != nil {
658-
glog.Warningf("Couldn't update the endponts for %v: %v", name, err)
670+
return fmt.Errorf("Couldn't update the endpoints for %v: %v", name, err)
659671
}
660672
}
661673
}
@@ -669,11 +681,13 @@ func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) {
669681
if exists {
670682
err := cnf.nginxAPI.UpdateServers(name, endps)
671683
if err != nil {
672-
glog.Warningf("Couldn't update the endponts for %v: %v", name, err)
684+
return fmt.Errorf("Couldn't update the endpoints for %v: %v", name, err)
673685
}
674686
}
675687
}
676688
}
689+
690+
return nil
677691
}
678692

679693
// UpdateConfig updates NGINX Configuration parameters
@@ -720,3 +734,10 @@ func keyToFileName(key string) string {
720734
func objectMetaToFileName(meta *meta_v1.ObjectMeta) string {
721735
return meta.Namespace + "-" + meta.Name
722736
}
737+
738+
// HasIngress checks if the Ingress resource is present in NGINX configuration
739+
func (cnf *Configurator) HasIngress(ing *extensions.Ingress) bool {
740+
name := objectMetaToFileName(&ing.ObjectMeta)
741+
_, exists := cnf.ingresses[name]
742+
return exists
743+
}

nginx-controller/nginx/plus/nginx_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (client *NginxClient) GetHTTPServers(upstream string) ([]string, error) {
236236
func (client *NginxClient) getIDOfHTTPServer(upstream string, name string) (int, error) {
237237
peers, err := client.getUpstreamPeers(upstream)
238238
if err != nil {
239-
return -1, fmt.Errorf("Error getting id of server %v of upstream %v:", name, upstream)
239+
return -1, fmt.Errorf("Error getting id of server %v of upstream %v: %v", name, upstream, err)
240240
}
241241

242242
for _, p := range peers.Peers {

0 commit comments

Comments
 (0)