From c88790d91cafd4fd8211fc414620dccbd2f82c78 Mon Sep 17 00:00:00 2001 From: Dimitrios Karagiannis Date: Fri, 24 Nov 2017 15:37:59 +0000 Subject: [PATCH] ignore hostname deletes when used by other ingresses --- ingress.go | 16 +++++++++++++++- registrator.go | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ingress.go b/ingress.go index 4364eac..35182c6 100644 --- a/ingress.go +++ b/ingress.go @@ -20,6 +20,7 @@ type ingressWatcher struct { resyncPeriod time.Duration labelSelector string stopChannel chan struct{} + store cache.Store } func newIngressWatcher(client kubernetes.Interface, eventHandler eventHandlerFunc, labelSelector string, resyncPeriod time.Duration) *ingressWatcher { @@ -54,7 +55,8 @@ func (iw *ingressWatcher) Start() { iw.eventHandler(watch.Deleted, obj.(*v1beta1.Ingress), nil) }, } - _, controller := cache.NewInformer(lw, &v1beta1.Ingress{}, iw.resyncPeriod, eh) + store, controller := cache.NewInformer(lw, &v1beta1.Ingress{}, iw.resyncPeriod, eh) + iw.store = store log.Println("[INFO] starting ingress watcher") controller.Run(iw.stopChannel) log.Println("[INFO] ingress watcher stopped") @@ -65,6 +67,18 @@ func (iw *ingressWatcher) Stop() { close(iw.stopChannel) } +func (iw *ingressWatcher) HostnameOwners(hostname string) []string { + owners := []string{} + for _, i := range iw.store.List() { + for _, h := range getHostnamesFromIngress(i.(*v1beta1.Ingress)) { + if hostname == h { + owners = append(owners, i.(*v1beta1.Ingress).Name) + } + } + } + return owners +} + func getHostnamesFromIngress(ingress *v1beta1.Ingress) []string { hostnames := []string{} for _, rule := range ingress.Spec.Rules { diff --git a/registrator.go b/registrator.go index 5b026d5..752b51e 100644 --- a/registrator.go +++ b/registrator.go @@ -290,6 +290,11 @@ func (r *registrator) pruneBatch(action string, records []cnameRecord) []cnameRe t, err := resolveCname(fmt.Sprintf("%s.", strings.Trim(u.Hostname, ".")), r.ListNameservers()) switch action { case route53.ChangeActionDelete: + o := r.ingressWatcher.HostnameOwners(u.Hostname) + if len(o) > 0 { + log.Printf("[DEBUG] will not delete record %s because it's still claimed by: %s", u.Hostname, strings.Join(o, ",")) + break + } if err == nil { pruned = append(pruned, u) } else if err != errDNSEmptyAnswer {