2016-02-22 00:13:08 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package healthchecks
|
|
|
|
|
|
|
|
import (
|
2017-04-01 14:38:58 +00:00
|
|
|
"net/http"
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-04-01 14:38:58 +00:00
|
|
|
compute "google.golang.org/api/compute/v1"
|
|
|
|
|
2016-11-10 23:31:49 +00:00
|
|
|
"k8s.io/ingress/controllers/gce/utils"
|
2016-02-22 00:13:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HealthChecks manages health checks.
|
|
|
|
type HealthChecks struct {
|
|
|
|
cloud SingleHealthCheck
|
|
|
|
defaultPath string
|
2016-03-15 00:33:12 +00:00
|
|
|
namer *utils.Namer
|
2016-05-29 05:02:39 +00:00
|
|
|
healthCheckGetter
|
|
|
|
}
|
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
// NewHealthChecker creates a new health checker.
|
|
|
|
// cloud: the cloud object implementing SingleHealthCheck.
|
|
|
|
// defaultHealthCheckPath: is the HTTP path to use for health checks.
|
2016-03-15 00:33:12 +00:00
|
|
|
func NewHealthChecker(cloud SingleHealthCheck, defaultHealthCheckPath string, namer *utils.Namer) HealthChecker {
|
2016-05-29 05:02:39 +00:00
|
|
|
return &HealthChecks{cloud, defaultHealthCheckPath, namer, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the health checker.
|
|
|
|
func (h *HealthChecks) Init(r healthCheckGetter) {
|
|
|
|
h.healthCheckGetter = r
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a healthcheck if one for the same port doesn't already exist.
|
2016-05-29 05:02:39 +00:00
|
|
|
func (h *HealthChecks) Add(port int64) error {
|
|
|
|
wantHC, err := h.healthCheckGetter.HealthCheck(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-05-29 05:02:39 +00:00
|
|
|
if wantHC.RequestPath == "" {
|
|
|
|
wantHC.RequestPath = h.defaultPath
|
|
|
|
}
|
|
|
|
name := h.namer.BeName(port)
|
|
|
|
wantHC.Name = name
|
|
|
|
hc, _ := h.Get(port)
|
2016-02-22 00:13:08 +00:00
|
|
|
if hc == nil {
|
2016-05-29 05:02:39 +00:00
|
|
|
// TODO: check if the readiness probe has changed and update the
|
|
|
|
// health check.
|
2016-02-22 00:13:08 +00:00
|
|
|
glog.Infof("Creating health check %v", name)
|
2016-05-29 05:02:39 +00:00
|
|
|
if err := h.cloud.CreateHttpHealthCheck(wantHC); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if wantHC.RequestPath != hc.RequestPath {
|
2016-09-23 21:22:22 +00:00
|
|
|
// TODO: reconcile health checks, and compare headers interval etc.
|
|
|
|
// Currently Ingress doesn't expose all the health check params
|
|
|
|
// natively, so some users prefer to hand modify the check.
|
|
|
|
glog.Infof("Unexpected request path on health check %v, has %v want %v, NOT reconciling",
|
|
|
|
name, hc.RequestPath, wantHC.RequestPath)
|
2016-02-22 00:13:08 +00:00
|
|
|
} else {
|
2016-09-23 21:22:22 +00:00
|
|
|
glog.Infof("Health check %v already exists and has the expected path %v", hc.Name, hc.RequestPath)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the health check by port.
|
|
|
|
func (h *HealthChecks) Delete(port int64) error {
|
|
|
|
name := h.namer.BeName(port)
|
|
|
|
glog.Infof("Deleting health check %v", name)
|
|
|
|
if err := h.cloud.DeleteHttpHealthCheck(h.namer.BeName(port)); err != nil {
|
|
|
|
if !utils.IsHTTPErrorCode(err, http.StatusNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the given health check.
|
|
|
|
func (h *HealthChecks) Get(port int64) (*compute.HttpHealthCheck, error) {
|
|
|
|
return h.cloud.GetHttpHealthCheck(h.namer.BeName(port))
|
|
|
|
}
|