diff --git a/pkg/backends/syncer.go b/pkg/backends/syncer.go index 98210a3aee..56ddb13d5e 100644 --- a/pkg/backends/syncer.go +++ b/pkg/backends/syncer.go @@ -18,7 +18,6 @@ import ( "strings" "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" - v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/ingress-gce/pkg/backends/features" "k8s.io/ingress-gce/pkg/composite" @@ -247,7 +246,7 @@ func (s *backendSyncer) ensureHealthCheck(sp utils.ServicePort, hasLegacyHC bool } if probe != nil { klog.V(4).Infof("Applying httpGet settings of readinessProbe to health check on port %+v", sp) - applyProbeSettingsToHC(probe, hc) + healthchecks.ApplyProbeSettingsToHC(probe, hc) } } @@ -282,31 +281,3 @@ func ensureHealthCheckLink(be *composite.BackendService, hcLink string) (needsUp be.HealthChecks = []string{hcLink} return true } - -func applyProbeSettingsToHC(p *v1.Probe, hc *healthchecks.HealthCheck) { - healthPath := p.Handler.HTTPGet.Path - // GCE requires a leading "/" for health check urls. - if !strings.HasPrefix(healthPath, "/") { - healthPath = "/" + healthPath - } - // Extract host from HTTP headers - host := p.Handler.HTTPGet.Host - for _, header := range p.Handler.HTTPGet.HTTPHeaders { - if header.Name == "Host" { - host = header.Value - break - } - } - - hc.RequestPath = healthPath - hc.Host = host - hc.Description = "Kubernetes L7 health check generated with readiness probe settings." - hc.TimeoutSec = int64(p.TimeoutSeconds) - if hc.ForNEG { - // For NEG mode, we can support more aggressive healthcheck interval. - hc.CheckIntervalSec = int64(p.PeriodSeconds) - } else { - // For IG mode, short healthcheck interval may health check flooding problem. - hc.CheckIntervalSec = int64(p.PeriodSeconds) + int64(healthchecks.DefaultHealthCheckInterval.Seconds()) - } -} diff --git a/pkg/backends/syncer_test.go b/pkg/backends/syncer_test.go index 479297e98f..5771c513cf 100644 --- a/pkg/backends/syncer_test.go +++ b/pkg/backends/syncer_test.go @@ -564,32 +564,6 @@ func TestShutdown(t *testing.T) { } } -func TestApplyProbeSettingsToHC(t *testing.T) { - p := "healthz" - hc := healthchecks.DefaultHealthCheck(8080, annotations.ProtocolHTTPS) - probe := &api_v1.Probe{ - Handler: api_v1.Handler{ - HTTPGet: &api_v1.HTTPGetAction{ - Scheme: api_v1.URISchemeHTTP, - Path: p, - Port: intstr.IntOrString{ - Type: intstr.Int, - IntVal: 80, - }, - }, - }, - } - - applyProbeSettingsToHC(probe, hc) - - if hc.Protocol() != annotations.ProtocolHTTPS || hc.Port != 8080 { - t.Errorf("Basic HC settings changed") - } - if hc.RequestPath != "/"+p { - t.Errorf("Failed to apply probe's requestpath") - } -} - func TestEnsureBackendServiceProtocol(t *testing.T) { fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) syncer := newTestSyncer(fakeGCE) diff --git a/pkg/healthchecks/healthchecks.go b/pkg/healthchecks/healthchecks.go index 7d01c34f63..a814e29771 100644 --- a/pkg/healthchecks/healthchecks.go +++ b/pkg/healthchecks/healthchecks.go @@ -20,12 +20,14 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" computealpha "google.golang.org/api/compute/v0.alpha" computebeta "google.golang.org/api/compute/v0.beta" "google.golang.org/api/compute/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/ingress-gce/pkg/annotations" "k8s.io/ingress-gce/pkg/composite" @@ -639,3 +641,32 @@ func copyViaJSON(dest interface{}, src jsonConvertable) error { } return json.Unmarshal(bytes, dest) } + +// ApplyProbeSettingsToHC TODO +func ApplyProbeSettingsToHC(p *v1.Probe, hc *HealthCheck) { + healthPath := p.Handler.HTTPGet.Path + // GCE requires a leading "/" for health check urls. + if !strings.HasPrefix(healthPath, "/") { + healthPath = "/" + healthPath + } + // Extract host from HTTP headers + host := p.Handler.HTTPGet.Host + for _, header := range p.Handler.HTTPGet.HTTPHeaders { + if header.Name == "Host" { + host = header.Value + break + } + } + + hc.RequestPath = healthPath + hc.Host = host + hc.Description = "Kubernetes L7 health check generated with readiness probe settings." + hc.TimeoutSec = int64(p.TimeoutSeconds) + if hc.ForNEG { + // For NEG mode, we can support more aggressive healthcheck interval. + hc.CheckIntervalSec = int64(p.PeriodSeconds) + } else { + // For IG mode, short healthcheck interval may health check flooding problem. + hc.CheckIntervalSec = int64(p.PeriodSeconds) + int64(DefaultHealthCheckInterval.Seconds()) + } +} diff --git a/pkg/healthchecks/healthchecks_test.go b/pkg/healthchecks/healthchecks_test.go index 98ecc291a8..36f91b712c 100644 --- a/pkg/healthchecks/healthchecks_test.go +++ b/pkg/healthchecks/healthchecks_test.go @@ -24,7 +24,9 @@ import ( "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/mock" computealpha "google.golang.org/api/compute/v0.alpha" + api_v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/ingress-gce/pkg/annotations" "k8s.io/ingress-gce/pkg/utils" namer_util "k8s.io/ingress-gce/pkg/utils/namer" @@ -398,5 +400,30 @@ func TestVersion(t *testing.T) { } }) } +} +func TestApplyProbeSettingsToHC(t *testing.T) { + p := "healthz" + hc := DefaultHealthCheck(8080, annotations.ProtocolHTTPS) + probe := &api_v1.Probe{ + Handler: api_v1.Handler{ + HTTPGet: &api_v1.HTTPGetAction{ + Scheme: api_v1.URISchemeHTTP, + Path: p, + Port: intstr.IntOrString{ + Type: intstr.Int, + IntVal: 80, + }, + }, + }, + } + + ApplyProbeSettingsToHC(probe, hc) + + if hc.Protocol() != annotations.ProtocolHTTPS || hc.Port != 8080 { + t.Errorf("Basic HC settings changed") + } + if hc.RequestPath != "/"+p { + t.Errorf("Failed to apply probe's requestpath") + } }