@@ -27,17 +27,19 @@ const JWTKeyAnnotation = "nginx.com/jwt-key"
2727
2828// Configurator transforms an Ingress resource into NGINX Configuration
2929type 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
3637func 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
6871func (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
612615func (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
629634func (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 {
720734func 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+ }
0 commit comments