Skip to content

Commit

Permalink
Merge pull request #65 from aledbf/fix-status
Browse files Browse the repository at this point in the history
Support hostnames in Ingress status
  • Loading branch information
aledbf authored Dec 16, 2016
2 parents e194288 + 3873042 commit e58524f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions core/pkg/ingress/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package status

import (
"net"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -92,19 +93,19 @@ func (s statusSync) Shutdown() {

glog.Infof("updating status of Ingress rules (remove)")

ips, err := s.getRunningIPs()
addrs, err := s.runningAddresess()
if err != nil {
glog.Errorf("error obtaining running IPs: %v", ips)
glog.Errorf("error obtaining running IPs: %v", addrs)
return
}

if len(ips) > 1 {
if len(addrs) > 1 {
// leave the job to the next leader
glog.Infof("leaving status update for next leader (%v)", len(ips))
glog.Infof("leaving status update for next leader (%v)", len(addrs))
return
}

glog.Infof("removing my ip (%v)", ips)
glog.Infof("removing address from ingress status (%v)", addrs)
s.updateStatus([]api.LoadBalancerIngress{})
}

Expand All @@ -131,11 +132,11 @@ func (s *statusSync) sync(key interface{}) error {
return nil
}

ips, err := s.getRunningIPs()
addrs, err := s.runningAddresess()
if err != nil {
return err
}
s.updateStatus(sliceToStatus(ips))
s.updateStatus(sliceToStatus(addrs))

return nil
}
Expand Down Expand Up @@ -180,20 +181,26 @@ func NewStatusSyncer(config Config) Sync {
return st
}

func (s *statusSync) getRunningIPs() ([]string, error) {
// runningAddresess returns a list of IP addresses and/or FQDN where the
// ingress controller is currently running
func (s *statusSync) runningAddresess() ([]string, error) {
if s.PublishService != "" {
ns, name, _ := k8s.ParseNameNS(s.PublishService)
svc, err := s.Client.Core().Services(ns).Get(name)
if err != nil {
return nil, err
}

ips := []string{}
addrs := []string{}
for _, ip := range svc.Status.LoadBalancer.Ingress {
ips = append(ips, ip.IP)
if ip.IP == "" {
addrs = append(addrs, ip.Hostname)
} else {
addrs = append(addrs, ip.IP)
}
}

return ips, nil
return addrs, nil
}

// get information about all the pods running the ingress controller
Expand All @@ -204,19 +211,25 @@ func (s *statusSync) getRunningIPs() ([]string, error) {
return nil, err
}

ips := []string{}
addrs := []string{}
for _, pod := range pods.Items {
if !strings.StringInSlice(pod.Status.HostIP, ips) {
ips = append(ips, pod.Status.HostIP)
name := k8s.GetNodeIP(s.Client, pod.Spec.NodeName)
if !strings.StringInSlice(name, addrs) {
addrs = append(addrs, name)
}
}
return ips, nil
return addrs, nil
}

func sliceToStatus(ips []string) []api.LoadBalancerIngress {
// sliceToStatus converts a slice of IP and/or hostnames to LoadBalancerIngress
func sliceToStatus(endpoints []string) []api.LoadBalancerIngress {
lbi := []api.LoadBalancerIngress{}
for _, ip := range ips {
lbi = append(lbi, api.LoadBalancerIngress{IP: ip})
for _, ep := range endpoints {
if net.ParseIP(ep) == nil {
lbi = append(lbi, api.LoadBalancerIngress{Hostname: ep})
} else {
lbi = append(lbi, api.LoadBalancerIngress{IP: ep})
}
}

sort.Sort(loadBalancerIngressByIP(lbi))
Expand Down

0 comments on commit e58524f

Please sign in to comment.