|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cloudnativelabs/kube-router/app/options" |
| 10 | + "github.com/golang/glog" |
| 11 | + "golang.org/x/net/context" |
| 12 | +) |
| 13 | + |
| 14 | +//ControllerHeartbeat is the structure to hold the heartbeats sent by controlers |
| 15 | +type ControllerHeartbeat struct { |
| 16 | + Component string |
| 17 | + Lastheartbeat time.Time |
| 18 | +} |
| 19 | + |
| 20 | +//HealthController reports the health of the controller loops as a http endpoint |
| 21 | +type HealthController struct { |
| 22 | + HealthPort uint16 |
| 23 | + HTTPenabled bool |
| 24 | + Status HealthStats |
| 25 | + Config *options.KubeRouterConfig |
| 26 | +} |
| 27 | + |
| 28 | +//HealthStats is holds the latest heartbeats |
| 29 | +type HealthStats struct { |
| 30 | + sync.Mutex |
| 31 | + Healthy bool |
| 32 | + MetricsControllerAlive time.Time |
| 33 | + NetworkPolicyControllerAlive time.Time |
| 34 | + NetworkRoutingControllerAlive time.Time |
| 35 | + NetworkServicesControllerAlive time.Time |
| 36 | +} |
| 37 | + |
| 38 | +//sendHeartBeat sends a heartbeat on the passed channel |
| 39 | +func sendHeartBeat(channel chan<- *ControllerHeartbeat, controller string) { |
| 40 | + heartbeat := ControllerHeartbeat{ |
| 41 | + Component: controller, |
| 42 | + Lastheartbeat: time.Now(), |
| 43 | + } |
| 44 | + channel <- &heartbeat |
| 45 | +} |
| 46 | + |
| 47 | +//Handler writes HTTP responses to the health path |
| 48 | +func (hc *HealthController) Handler(w http.ResponseWriter, req *http.Request) { |
| 49 | + if hc.Status.Healthy { |
| 50 | + w.WriteHeader(http.StatusOK) |
| 51 | + w.Write([]byte("OK\n")) |
| 52 | + } else { |
| 53 | + w.WriteHeader(http.StatusInternalServerError) |
| 54 | + /* |
| 55 | + statusText := fmt.Sprintf("Service controller last alive %s\n ago"+ |
| 56 | + "Routing controller last alive: %s\n ago"+ |
| 57 | + "Policy controller last alive: %s\n ago"+ |
| 58 | + "Metrics controller last alive: %s\n ago", |
| 59 | + time.Since(hc.Status.NetworkServicesControllerAlive), |
| 60 | + time.Since(hc.Status.NetworkRoutingControllerAlive), |
| 61 | + time.Since(hc.Status.NetworkPolicyControllerAlive), |
| 62 | + time.Since(hc.Status.MetricsControllerAlive)) |
| 63 | + w.Write([]byte(statusText)) |
| 64 | + */ |
| 65 | + w.Write([]byte("Unhealthy")) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +//HandleHeartbeat handles recevied heartbeats onthe health channel |
| 70 | +func (hc *HealthController) HandleHeartbeat(beat *ControllerHeartbeat) { |
| 71 | + glog.V(3).Infof("Received heartbeat from %s", beat.Component) |
| 72 | + |
| 73 | + hc.Status.Lock() |
| 74 | + defer hc.Status.Unlock() |
| 75 | + |
| 76 | + switch component := beat.Component; component { |
| 77 | + case "NSC": |
| 78 | + hc.Status.NetworkServicesControllerAlive = time.Now() |
| 79 | + case "NRC": |
| 80 | + hc.Status.NetworkRoutingControllerAlive = time.Now() |
| 81 | + case "NPC": |
| 82 | + hc.Status.NetworkPolicyControllerAlive = time.Now() |
| 83 | + case "MC": |
| 84 | + hc.Status.MetricsControllerAlive = time.Now() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +//CheckHealth evaluates the time since last heartbeat to decide if the controller is running or not |
| 89 | +func (hc *HealthController) CheckHealth() bool { |
| 90 | + health := true |
| 91 | + if hc.Config.RunFirewall { |
| 92 | + if time.Since(hc.Status.NetworkPolicyControllerAlive) > hc.Config.IPTablesSyncPeriod+5*time.Second { |
| 93 | + glog.Error("Network Policy Controller heartbeat missed") |
| 94 | + health = false |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if hc.Config.RunRouter { |
| 99 | + if time.Since(hc.Status.NetworkRoutingControllerAlive) > hc.Config.RoutesSyncPeriod+5*time.Second { |
| 100 | + glog.Error("Network Routing Controller heartbeat missed") |
| 101 | + health = false |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if hc.Config.RunServiceProxy { |
| 106 | + if time.Since(hc.Status.NetworkServicesControllerAlive) > hc.Config.IpvsSyncPeriod+5*time.Second { |
| 107 | + glog.Error("NetworkService Controller heartbeat missed") |
| 108 | + health = false |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if hc.Config.MetricsEnabled { |
| 113 | + if time.Since(hc.Status.MetricsControllerAlive) > 5*time.Second { |
| 114 | + glog.Error("Metrics Controller heartbeat missed") |
| 115 | + health = false |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return health |
| 120 | +} |
| 121 | + |
| 122 | +//Run starts the HealthController |
| 123 | +func (hc *HealthController) Run(healthChan <-chan *ControllerHeartbeat, stopCh <-chan struct{}, wg *sync.WaitGroup) error { |
| 124 | + Started := time.Now() |
| 125 | + t := time.NewTicker(500 * time.Millisecond) |
| 126 | + defer wg.Done() |
| 127 | + glog.Info("Starting health controller") |
| 128 | + |
| 129 | + srv := &http.Server{Addr: ":" + strconv.Itoa(int(hc.HealthPort)), Handler: http.DefaultServeMux} |
| 130 | + |
| 131 | + http.HandleFunc("/healthz", hc.Handler) |
| 132 | + |
| 133 | + if (hc.Config.HealthPort > 0) && (hc.Config.HealthPort <= 65535) { |
| 134 | + hc.HTTPenabled = true |
| 135 | + go func() { |
| 136 | + if err := srv.ListenAndServe(); err != nil { |
| 137 | + // cannot panic, because this probably is an intentional close |
| 138 | + glog.Errorf("Health controller error: %s", err) |
| 139 | + } |
| 140 | + }() |
| 141 | + } else if hc.Config.MetricsPort > 65535 { |
| 142 | + glog.Errorf("Metrics port must be over 0 and under 65535, given port: %d", hc.Config.MetricsPort) |
| 143 | + } else { |
| 144 | + hc.HTTPenabled = false |
| 145 | + } |
| 146 | + |
| 147 | + for { |
| 148 | + //Give the controllers a few seconds to start before checking health |
| 149 | + if time.Since(Started) > 5*time.Second { |
| 150 | + hc.Status.Healthy = hc.CheckHealth() |
| 151 | + } |
| 152 | + select { |
| 153 | + case <-stopCh: |
| 154 | + glog.Infof("Shutting down health controller") |
| 155 | + if hc.HTTPenabled { |
| 156 | + if err := srv.Shutdown(context.Background()); err != nil { |
| 157 | + glog.Errorf("could not shutdown: %v", err) |
| 158 | + } |
| 159 | + } |
| 160 | + return nil |
| 161 | + case heartbeat := <-healthChan: |
| 162 | + hc.HandleHeartbeat(heartbeat) |
| 163 | + case <-t.C: |
| 164 | + glog.V(4).Info("Health controller tick") |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +//NewHealthController creates a new healh controller and returns a reference to it |
| 171 | +func NewHealthController(config *options.KubeRouterConfig) (*HealthController, error) { |
| 172 | + hc := HealthController{ |
| 173 | + Config: config, |
| 174 | + HealthPort: config.HealthPort, |
| 175 | + Status: HealthStats{ |
| 176 | + Healthy: false, |
| 177 | + }, |
| 178 | + } |
| 179 | + return &hc, nil |
| 180 | +} |
0 commit comments