Skip to content

Commit

Permalink
Move health checks from utils to healthchecks pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Sardo committed Apr 7, 2017
1 parent bffd850 commit b1851de
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 92 deletions.
4 changes: 2 additions & 2 deletions controllers/gce/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (b *Backends) Status(name string) string {
return hs.HealthStatus[0].HealthState
}

func applyProbeSettingsToHC(p *api_v1.Probe, hc *utils.HealthCheck) {
func applyProbeSettingsToHC(p *api_v1.Probe, hc *healthchecks.HealthCheck) {
healthPath := p.Handler.HTTPGet.Path
// GCE requires a leading "/" for health check urls.
if !strings.HasPrefix(healthPath, "/") {
Expand All @@ -458,6 +458,6 @@ func applyProbeSettingsToHC(p *api_v1.Probe, hc *utils.HealthCheck) {
// We're just trying to detect if the node networking is
// borked, service level outages will get detected sooner
// by kube-proxy.
hc.CheckIntervalSec = int64(p.PeriodSeconds + utils.DefaultHealthCheckInterval)
hc.CheckIntervalSec = int64(p.PeriodSeconds + healthchecks.DefaultHealthCheckInterval)
hc.TimeoutSec = int64(p.TimeoutSeconds)
}
2 changes: 1 addition & 1 deletion controllers/gce/backends/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func TestBackendCreateBalancingMode(t *testing.T) {

func TestApplyProbeSettingsToHC(t *testing.T) {
p := "healthz"
hc := utils.DefaultHealthCheckTemplate(8080, true)
hc := healthchecks.DefaultHealthCheckTemplate(8080, true)
probe := &api_v1.Probe{
Handler: api_v1.Handler{
HTTPGet: &api_v1.HTTPGetAction{
Expand Down
87 changes: 81 additions & 6 deletions controllers/gce/healthchecks/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ package healthchecks
import (
"net/http"

compute "google.golang.org/api/compute/v1"

"github.com/golang/glog"

"k8s.io/ingress/controllers/gce/utils"
)

const (
// DefaultHealthCheckInterval defines how frequently a probe runs
DefaultHealthCheckInterval = 60
// DefaultHealthyThreshold defines the threshold of success probes that declare a backend "healthy"
DefaultHealthyThreshold = 1
// DefaultUnhealthyThreshold defines the threshold of failure probes that declare a backend "unhealthy"
DefaultUnhealthyThreshold = 10
// DefaultTimeoutSeconds defines the timeout of each probe
DefaultTimeoutSeconds = 60
)

// HealthChecks manages health checks.
type HealthChecks struct {
cloud HealthCheckProvider
Expand All @@ -38,16 +51,16 @@ func NewHealthChecker(cloud HealthCheckProvider, defaultHealthCheckPath string,
return &HealthChecks{cloud, defaultHealthCheckPath, namer}
}

func (h *HealthChecks) New(port int64, encrypted bool) *utils.HealthCheck {
hc := utils.DefaultHealthCheckTemplate(port, encrypted)
func (h *HealthChecks) New(port int64, encrypted bool) *HealthCheck {
hc := DefaultHealthCheckTemplate(port, encrypted)
hc.Name = h.namer.BeName(port)
return hc
}

// Sync retrieves a health check based on port/encryption, checks for properties
// that should be identical, and updates/creates if necessary.
// Sync is only called by the backends.Add func - it's not a pool like other resources.
func (h *HealthChecks) Sync(hc *utils.HealthCheck) error {
func (h *HealthChecks) Sync(hc *HealthCheck) error {
// Verify default path
if hc.RequestPath == "" {
hc.RequestPath = h.defaultPath
Expand Down Expand Up @@ -88,18 +101,80 @@ func (h *HealthChecks) Delete(port int64, encrypted bool) error {
return h.cloud.DeleteHttpHealthCheck(h.namer.BeName(port))
}

func (h *HealthChecks) Get(port int64, encrypted bool) (*utils.HealthCheck, error) {
func (h *HealthChecks) Get(port int64, encrypted bool) (*HealthCheck, error) {
name := h.namer.BeName(port)
if encrypted {
hc, err := h.cloud.GetHttpsHealthCheck(name)
if err != nil {
return nil, err
}
return utils.NewHealthCheckHttps(hc), nil
return NewHealthCheckHttps(hc), nil
}
hc, err := h.cloud.GetHttpHealthCheck(name)
if err != nil {
return nil, err
}
return utils.NewHealthCheckHttp(hc), nil
return NewHealthCheckHttp(hc), nil
}

// DefaultHealthCheckTemplate simply returns the default health check template.
func DefaultHealthCheckTemplate(port int64, encrypted bool) *HealthCheck {
return &HealthCheck{
HttpHealthCheck: compute.HttpHealthCheck{
Port: port,
// Empty string is used as a signal to the caller to use the appropriate
// default.
RequestPath: "",
Description: "Default kubernetes L7 Loadbalancing health check.",
// How often to health check.
CheckIntervalSec: DefaultHealthCheckInterval,
// How long to wait before claiming failure of a health check.
TimeoutSec: DefaultTimeoutSeconds,
// Number of healthchecks to pass for a vm to be deemed healthy.
HealthyThreshold: DefaultHealthyThreshold,
// Number of healthchecks to fail before the vm is deemed unhealthy.
UnhealthyThreshold: DefaultUnhealthyThreshold,
},
Encrypted: encrypted,
}
}

type HealthCheck struct {
compute.HttpHealthCheck
Encrypted bool
}

// NewHealthCheckHttp
func NewHealthCheckHttp(hc *compute.HttpHealthCheck) *HealthCheck {
if hc == nil {
return nil
}

return &HealthCheck{
HttpHealthCheck: *hc,
Encrypted: false,
}
}

// NewHealthCheckHttps
func NewHealthCheckHttps(hc *compute.HttpsHealthCheck) *HealthCheck {
if hc == nil {
return nil
}
h := *hc
return &HealthCheck{
HttpHealthCheck: compute.HttpHealthCheck(h),
Encrypted: true,
}
}

// ToHttpHealthCheck should only be called if Encrypted=false
func (hc *HealthCheck) ToHttpHealthCheck() *compute.HttpHealthCheck {
return &hc.HttpHealthCheck
}

// ToHttpsHealthCheck should only be called if Encrypted=true
func (hc *HealthCheck) ToHttpsHealthCheck() *compute.HttpsHealthCheck {
ehc := compute.HttpsHealthCheck(hc.HttpHealthCheck)
return &ehc
}
10 changes: 5 additions & 5 deletions controllers/gce/healthchecks/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestHealthCheckAddExisting(t *testing.T) {

// HTTP
// Manually insert a health check
httpHC := utils.DefaultHealthCheckTemplate(3000, false)
httpHC := DefaultHealthCheckTemplate(3000, false)
httpHC.Name = namer.BeName(3000)
httpHC.RequestPath = "/my-probes-health"
hcp.CreateHttpHealthCheck(httpHC.ToHttpHealthCheck())
Expand All @@ -77,7 +77,7 @@ func TestHealthCheckAddExisting(t *testing.T) {

// HTTPS
// Manually insert a health check
httpsHC := utils.DefaultHealthCheckTemplate(4000, true)
httpsHC := DefaultHealthCheckTemplate(4000, true)
httpsHC.Name = namer.BeName(4000)
httpsHC.RequestPath = "/my-probes-health"
hcp.CreateHttpsHealthCheck(httpsHC.ToHttpsHealthCheck())
Expand All @@ -100,7 +100,7 @@ func TestHealthCheckDelete(t *testing.T) {
healthChecks := NewHealthChecker(hcp, "/", namer)

// Create HTTP HC for 1234
hc := utils.DefaultHealthCheckTemplate(1234, false)
hc := DefaultHealthCheckTemplate(1234, false)
hc.Name = namer.BeName(1234)
hcp.CreateHttpHealthCheck(hc.ToHttpHealthCheck())

Expand Down Expand Up @@ -139,7 +139,7 @@ func TestHealthCheckGet(t *testing.T) {

// HTTP
// Manually insert a health check
httpHC := utils.DefaultHealthCheckTemplate(3000, false)
httpHC := DefaultHealthCheckTemplate(3000, false)
httpHC.Name = namer.BeName(3000)
httpHC.RequestPath = "/my-probes-health"
hcp.CreateHttpHealthCheck(httpHC.ToHttpHealthCheck())
Expand All @@ -152,7 +152,7 @@ func TestHealthCheckGet(t *testing.T) {

// HTTPS
// Manually insert a health check
httpsHC := utils.DefaultHealthCheckTemplate(4000, true)
httpsHC := DefaultHealthCheckTemplate(4000, true)
httpsHC.Name = namer.BeName(4000)
httpsHC.RequestPath = "/my-probes-health"
hcp.CreateHttpsHealthCheck(httpsHC.ToHttpsHealthCheck())
Expand Down
11 changes: 4 additions & 7 deletions controllers/gce/healthchecks/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ limitations under the License.

package healthchecks

import (
compute "google.golang.org/api/compute/v1"
"k8s.io/ingress/controllers/gce/utils"
)
import compute "google.golang.org/api/compute/v1"

// HealthCheckProvider is an interface to manage a single GCE health check.
type HealthCheckProvider interface {
Expand All @@ -36,8 +33,8 @@ type HealthCheckProvider interface {

// HealthChecker is an interface to manage cloud HTTPHealthChecks.
type HealthChecker interface {
New(port int64, encrypted bool) *utils.HealthCheck
Sync(hc *utils.HealthCheck) error
New(port int64, encrypted bool) *HealthCheck
Sync(hc *HealthCheck) error
Delete(port int64, encrypted bool) error
Get(port int64, encrypted bool) (*utils.HealthCheck, error)
Get(port int64, encrypted bool) (*HealthCheck, error)
}
71 changes: 0 additions & 71 deletions controllers/gce/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ const (
// K8sAnnotationPrefix is the prefix used in annotations used to record
// debug information in the Ingress annotations.
K8sAnnotationPrefix = "ingress.kubernetes.io"

// DefaultHealthCheckInterval defines how frequently a probe runs
DefaultHealthCheckInterval = 60
// DefaultHealthyThreshold defines the threshold of success probes that declare a backend "healthy"
DefaultHealthyThreshold = 1
// DefaultUnhealthyThreshold defines the threshold of failure probes that declare a backend "unhealthy"
DefaultUnhealthyThreshold = 10
// DefaultTimeoutSeconds defines the timeout of each probe
DefaultTimeoutSeconds = 60
)

// Namer handles centralized naming for the cluster.
Expand Down Expand Up @@ -336,65 +327,3 @@ func GetHTTPScheme(encrypted bool) string {
// FakeIngressRuleValueMap is a convenience type used by multiple submodules
// that share the same testing methods.
type FakeIngressRuleValueMap map[string]string

// DefaultHealthCheckTemplate simply returns the default health check template.
func DefaultHealthCheckTemplate(port int64, encrypted bool) *HealthCheck {
return &HealthCheck{
HttpHealthCheck: compute.HttpHealthCheck{
Port: port,
// Empty string is used as a signal to the caller to use the appropriate
// default.
RequestPath: "",
Description: "Default kubernetes L7 Loadbalancing health check.",
// How often to health check.
CheckIntervalSec: DefaultHealthCheckInterval,
// How long to wait before claiming failure of a health check.
TimeoutSec: DefaultTimeoutSeconds,
// Number of healthchecks to pass for a vm to be deemed healthy.
HealthyThreshold: DefaultHealthyThreshold,
// Number of healthchecks to fail before the vm is deemed unhealthy.
UnhealthyThreshold: DefaultUnhealthyThreshold,
},
Encrypted: encrypted,
}
}

type HealthCheck struct {
compute.HttpHealthCheck
Encrypted bool
}

// NewHealthCheckHttp
func NewHealthCheckHttp(hc *compute.HttpHealthCheck) *HealthCheck {
if hc == nil {
return nil
}

return &HealthCheck{
HttpHealthCheck: *hc,
Encrypted: false,
}
}

// NewHealthCheckHttps
func NewHealthCheckHttps(hc *compute.HttpsHealthCheck) *HealthCheck {
if hc == nil {
return nil
}
h := *hc
return &HealthCheck{
HttpHealthCheck: compute.HttpHealthCheck(h),
Encrypted: true,
}
}

// ToHttpHealthCheck should only be called if Encrypted=false
func (hc *HealthCheck) ToHttpHealthCheck() *compute.HttpHealthCheck {
return &hc.HttpHealthCheck
}

// ToHttpsHealthCheck should only be called if Encrypted=true
func (hc *HealthCheck) ToHttpsHealthCheck() *compute.HttpsHealthCheck {
ehc := compute.HttpsHealthCheck(hc.HttpHealthCheck)
return &ehc
}

0 comments on commit b1851de

Please sign in to comment.