From 04b548e6cc049d07d5039cb0776a79a2a55d451d Mon Sep 17 00:00:00 2001 From: Rohit Ramkumar Date: Thu, 10 May 2018 16:42:47 -0700 Subject: [PATCH] Condense health checkers into one health checker for all backends. --- cmd/glbc/main.go | 4 +- pkg/backends/backends.go | 3 +- pkg/backends/backends_test.go | 11 ++--- pkg/controller/cluster_manager.go | 31 ++++++-------- pkg/controller/controller.go | 27 ++++++------ pkg/controller/controller_test.go | 4 +- pkg/controller/fakes.go | 26 +++++++----- pkg/flags/flags.go | 40 ++++++++++-------- pkg/healthchecks/healthchecks.go | 60 ++++++++++++++------------- pkg/healthchecks/healthchecks_test.go | 42 ++++++++++++------- pkg/healthchecks/interfaces.go | 4 +- 11 files changed, 134 insertions(+), 118 deletions(-) diff --git a/cmd/glbc/main.go b/cmd/glbc/main.go index bdf8c323a4..c2dfff463e 100644 --- a/cmd/glbc/main.go +++ b/cmd/glbc/main.go @@ -90,7 +90,7 @@ func main() { cloud := app.NewGCEClient() defaultBackendServicePort := app.DefaultBackendServicePort(kubeClient) - clusterManager, err := controller.NewClusterManager(cloud, namer, flags.F.HealthCheckPath) + clusterManager, err := controller.NewClusterManager(cloud, namer, *defaultBackendServicePort, flags.F.HealthCheckPath, flags.F.DefaultSvcHealthCheckPath) if err != nil { glog.Fatalf("Error creating cluster manager: %v", err) } @@ -98,7 +98,7 @@ func main() { enableNEG := cloud.AlphaFeatureGate.Enabled(gce.AlphaFeatureNetworkEndpointGroup) stopCh := make(chan struct{}) ctx := context.NewControllerContext(kubeClient, flags.F.WatchNamespace, flags.F.ResyncPeriod, enableNEG) - lbc, err := controller.NewLoadBalancerController(kubeClient, stopCh, ctx, clusterManager, enableNEG, *defaultBackendServicePort) + lbc, err := controller.NewLoadBalancerController(kubeClient, stopCh, ctx, clusterManager, enableNEG) if err != nil { glog.Fatalf("Error creating load balancer controller: %v", err) } diff --git a/pkg/backends/backends.go b/pkg/backends/backends.go index 3080169ed2..c2451cc890 100644 --- a/pkg/backends/backends.go +++ b/pkg/backends/backends.go @@ -240,8 +240,7 @@ func (b *Backends) Get(name string, isAlpha bool) (*BackendService, error) { } func (b *Backends) ensureHealthCheck(sp utils.ServicePort) (string, error) { - name := sp.BackendName(b.namer) - hc := b.healthChecker.New(name, sp.NodePort, sp.Protocol, sp.NEGEnabled) + hc := b.healthChecker.New(sp) existingLegacyHC, err := b.healthChecker.GetLegacy(sp.NodePort) if err != nil && !utils.IsNotFoundError(err) { return "", err diff --git a/pkg/backends/backends_test.go b/pkg/backends/backends_test.go index 06b58df70a..4251bd921b 100644 --- a/pkg/backends/backends_test.go +++ b/pkg/backends/backends_test.go @@ -42,8 +42,9 @@ import ( const defaultZone = "zone-a" var ( - defaultNamer = utils.NewNamer("uid1", "fw1") - existingProbe = &api_v1.Probe{ + defaultNamer = utils.NewNamer("uid1", "fw1") + defaultBackendSvc = types.NamespacedName{Namespace: "system", Name: "default"} + existingProbe = &api_v1.Probe{ Handler: api_v1.Handler{ HTTPGet: &api_v1.HTTPGetAction{ Scheme: api_v1.URISchemeHTTPS, @@ -63,7 +64,7 @@ func newTestJig(f BackendServices, fakeIGs instances.InstanceGroups, syncWithClo nodePool := instances.NewNodePool(fakeIGs, defaultNamer) nodePool.Init(&instances.FakeZoneLister{Zones: []string{defaultZone}}) healthCheckProvider := healthchecks.NewFakeHealthCheckProvider() - healthChecks := healthchecks.NewHealthChecker(healthCheckProvider, "/", defaultNamer) + healthChecks := healthchecks.NewHealthChecker(healthCheckProvider, "/", "/healthz", defaultNamer, defaultBackendSvc) bp := NewBackendPool(f, negGetter, healthChecks, nodePool, defaultNamer, syncWithCloud) probes := map[utils.ServicePort]*api_v1.Probe{{NodePort: 443, Protocol: annotations.ProtocolHTTPS}: existingProbe} bp.Init(NewFakeProbeProvider(probes)) @@ -602,7 +603,7 @@ func TestBackendPoolDeleteLegacyHealthChecks(t *testing.T) { nodePool := instances.NewNodePool(fakeIGs, defaultNamer) nodePool.Init(&instances.FakeZoneLister{Zones: []string{defaultZone}}) hcp := healthchecks.NewFakeHealthCheckProvider() - healthChecks := healthchecks.NewHealthChecker(hcp, "/", defaultNamer) + healthChecks := healthchecks.NewHealthChecker(hcp, "/", "/healthz", defaultNamer, defaultBackendSvc) bp := NewBackendPool(f, negGetter, healthChecks, nodePool, defaultNamer, false) probes := map[utils.ServicePort]*api_v1.Probe{} bp.Init(NewFakeProbeProvider(probes)) @@ -783,7 +784,7 @@ func TestLinkBackendServiceToNEG(t *testing.T) { nodePool := instances.NewNodePool(fakeIGs, defaultNamer) nodePool.Init(&instances.FakeZoneLister{Zones: []string{defaultZone}}) hcp := healthchecks.NewFakeHealthCheckProvider() - healthChecks := healthchecks.NewHealthChecker(hcp, "/", defaultNamer) + healthChecks := healthchecks.NewHealthChecker(hcp, "/", "/healthz", defaultNamer, defaultBackendSvc) bp := NewBackendPool(f, fakeNEG, healthChecks, nodePool, defaultNamer, false) svcPort := utils.ServicePort{ diff --git a/pkg/controller/cluster_manager.go b/pkg/controller/cluster_manager.go index bc6d4b80a3..11f39e3c62 100644 --- a/pkg/controller/cluster_manager.go +++ b/pkg/controller/cluster_manager.go @@ -33,11 +33,6 @@ import ( "k8s.io/ingress-gce/pkg/utils" ) -const ( - defaultPort = 80 - defaultHealthCheckPath = "/" -) - // ClusterManager manages cluster resource pools. type ClusterManager struct { ClusterNamer *utils.Namer @@ -47,12 +42,10 @@ type ClusterManager struct { firewallPool firewalls.SingleFirewallPool // TODO: Refactor so we simply init a health check pool. - // Currently health checks are tied to backends because each backend needs - // the link of the associated health, but both the backend pool and - // loadbalancer pool manage backends, because the lifetime of the default - // backend is tied to the last/first loadbalancer not the life of the - // nodeport service or Ingress. - healthCheckers []healthchecks.HealthChecker + healthChecker healthchecks.HealthChecker + + // defaultBackendSvcPort is the ServicePort for the system default backend. + defaultBackendSvcPort utils.ServicePort } // Init initializes the cluster manager. @@ -179,25 +172,25 @@ func (c *ClusterManager) GC(lbNames []string, nodePorts []utils.ServicePort) err // - namer: is the namer used to tag cluster wide shared resources. // - defaultBackendNodePort: is the node port of glbc's default backend. This is // the kubernetes Service that serves the 404 page if no urls match. -// - defaultHealthCheckPath: is the default path used for L7 health checks, eg: "/healthz". +// - healthCheckPath: is the default path used for L7 health checks, eg: "/healthz". +// - defaultBackendHealthCheckPath: is the default path used for the default backend health checks. func NewClusterManager( cloud *gce.GCECloud, namer *utils.Namer, - defaultHealthCheckPath string) (*ClusterManager, error) { + defaultBackendSvcPort utils.ServicePort, + healthCheckPath string, + defaultBackendHealthCheckPath string) (*ClusterManager, error) { // Names are fundamental to the cluster, the uid allocator makes sure names don't collide. - cluster := ClusterManager{ClusterNamer: namer} + cluster := ClusterManager{ClusterNamer: namer, defaultBackendSvcPort: defaultBackendSvcPort} // NodePool stores GCE vms that are in this Kubernetes cluster. cluster.instancePool = instances.NewNodePool(cloud, namer) // BackendPool creates GCE BackendServices and associated health checks. - healthChecker := healthchecks.NewHealthChecker(cloud, defaultHealthCheckPath, cluster.ClusterNamer) - // Loadbalancer pool manages the default backend and its health check. - defaultBackendHealthChecker := healthchecks.NewHealthChecker(cloud, "/healthz", cluster.ClusterNamer) + cluster.healthChecker = healthchecks.NewHealthChecker(cloud, healthCheckPath, defaultBackendHealthCheckPath, cluster.ClusterNamer, defaultBackendSvcPort.SvcName) + cluster.backendPool = backends.NewBackendPool(cloud, cloud, cluster.healthChecker, cluster.instancePool, cluster.ClusterNamer, true) - cluster.healthCheckers = []healthchecks.HealthChecker{healthChecker, defaultBackendHealthChecker} - cluster.backendPool = backends.NewBackendPool(cloud, cloud, healthChecker, cluster.instancePool, cluster.ClusterNamer, true) // L7 pool creates targetHTTPProxy, ForwardingRules, UrlMaps, StaticIPs. cluster.l7Pool = loadbalancers.NewLoadBalancerPool(cloud, cluster.ClusterNamer) cluster.firewallPool = firewalls.NewFirewallPool(cloud, cluster.ClusterNamer, gce.LoadBalancerSrcRanges(), flags.F.NodePortRanges.Values()) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 573f034d7f..47fef63e57 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -84,8 +84,6 @@ type LoadBalancerController struct { hasSynced func() bool // negEnabled indicates whether NEG feature is enabled. negEnabled bool - // defaultBackendSvcPort is the ServicePort for the system default backend. - defaultBackendSvcPort utils.ServicePort } // NewLoadBalancerController creates a controller for gce loadbalancers. @@ -93,23 +91,22 @@ type LoadBalancerController struct { // - clusterManager: A ClusterManager capable of creating all cloud resources // required for L7 loadbalancing. // - resyncPeriod: Watchers relist from the Kubernetes API server this often. -func NewLoadBalancerController(kubeClient kubernetes.Interface, stopCh chan struct{}, ctx *context.ControllerContext, clusterManager *ClusterManager, negEnabled bool, defaultBackendSvcPort utils.ServicePort) (*LoadBalancerController, error) { +func NewLoadBalancerController(kubeClient kubernetes.Interface, stopCh chan struct{}, ctx *context.ControllerContext, clusterManager *ClusterManager, negEnabled bool) (*LoadBalancerController, error) { broadcaster := record.NewBroadcaster() broadcaster.StartLogging(glog.Infof) broadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{ Interface: kubeClient.Core().Events(""), }) lbc := LoadBalancerController{ - client: kubeClient, - ctx: ctx, - ingLister: StoreToIngressLister{Store: ctx.IngressInformer.GetStore()}, - nodeLister: ctx.NodeInformer.GetIndexer(), - nodes: NewNodeController(ctx, clusterManager), - CloudClusterManager: clusterManager, - stopCh: stopCh, - hasSynced: ctx.HasSynced, - negEnabled: negEnabled, - defaultBackendSvcPort: defaultBackendSvcPort, + client: kubeClient, + ctx: ctx, + ingLister: StoreToIngressLister{Store: ctx.IngressInformer.GetStore()}, + nodeLister: ctx.NodeInformer.GetIndexer(), + nodes: NewNodeController(ctx, clusterManager), + CloudClusterManager: clusterManager, + stopCh: stopCh, + hasSynced: ctx.HasSynced, + negEnabled: negEnabled, } lbc.ingQueue = utils.NewPeriodicTaskQueue("ingresses", lbc.sync) @@ -286,7 +283,7 @@ func (lbc *LoadBalancerController) sync(key string) (retErr error) { } func (lbc *LoadBalancerController) ensureIngress(key string, ing *extensions.Ingress, nodeNames []string, gceSvcPorts []utils.ServicePort) error { - urlMap := lbc.Translator.TranslateIngress(ing, lbc.defaultBackendSvcPort) + urlMap := lbc.Translator.TranslateIngress(ing, lbc.CloudClusterManager.defaultBackendSvcPort) ingSvcPorts := urlMap.AllServicePorts() igs, err := lbc.CloudClusterManager.EnsureInstanceGroupsAndPorts(nodeNames, ingSvcPorts) if err != nil { @@ -448,7 +445,7 @@ func updateAnnotations(client kubernetes.Interface, name, namespace string, anno func (lbc *LoadBalancerController) ToSvcPorts(ings *extensions.IngressList) []utils.ServicePort { var knownPorts []utils.ServicePort for _, ing := range ings.Items { - urlMap := lbc.Translator.TranslateIngress(&ing, lbc.defaultBackendSvcPort) + urlMap := lbc.Translator.TranslateIngress(&ing, lbc.CloudClusterManager.defaultBackendSvcPort) knownPorts = append(knownPorts, urlMap.AllServicePorts()...) } return knownPorts diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index f247ab5383..23b8c0941f 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -55,7 +55,7 @@ func newLoadBalancerController(t *testing.T, cm *fakeClusterManager) *LoadBalanc kubeClient := fake.NewSimpleClientset() stopCh := make(chan struct{}) ctx := context.NewControllerContext(kubeClient, api_v1.NamespaceAll, 1*time.Second, true) - lb, err := NewLoadBalancerController(kubeClient, stopCh, ctx, cm.ClusterManager, true, testDefaultBeNodePort) + lb, err := NewLoadBalancerController(kubeClient, stopCh, ctx, cm.ClusterManager, true) if err != nil { t.Fatalf("%v", err) } @@ -155,7 +155,7 @@ func gceURLMapFromPrimitive(primitiveMap utils.PrimitivePathMap, pm *nodePortMan } urlMap.PutPathRulesForHost(hostname, pathRules) } - urlMap.DefaultBackend = testDefaultBeNodePort + urlMap.DefaultBackend = testDefaultBeSvcPort return urlMap } diff --git a/pkg/controller/fakes.go b/pkg/controller/fakes.go index ebc5ca132c..a8103660fb 100644 --- a/pkg/controller/fakes.go +++ b/pkg/controller/fakes.go @@ -18,6 +18,7 @@ package controller import ( compute "google.golang.org/api/compute/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/sets" @@ -32,10 +33,14 @@ import ( ) var ( - testDefaultBeNodePort = utils.ServicePort{NodePort: 30000, Protocol: annotations.ProtocolHTTP} - testBackendPort = intstr.IntOrString{Type: intstr.Int, IntVal: 80} - testSrcRanges = []string{"1.1.1.1/20"} - testNodePortRanges = []string{"30000-32767"} + testDefaultBeSvcPort = utils.ServicePort{ + NodePort: 30000, + Protocol: annotations.ProtocolHTTP, + SvcName: types.NamespacedName{Namespace: "system", Name: "default"}, + } + testBackendPort = intstr.IntOrString{Type: intstr.Int, IntVal: 80} + testSrcRanges = []string{"1.1.1.1/20"} + testNodePortRanges = []string{"30000-32767"} ) // ClusterManager fake @@ -59,7 +64,7 @@ func NewFakeClusterManager(clusterName, firewallName string) *fakeClusterManager nodePool := instances.NewNodePool(fakeIGs, namer) nodePool.Init(&instances.FakeZoneLister{Zones: []string{"zone-a"}}) - healthChecker := healthchecks.NewHealthChecker(fakeHCP, "/", namer) + healthChecker := healthchecks.NewHealthChecker(fakeHCP, "/", "/healthz", namer, testDefaultBeSvcPort.SvcName) backendPool := backends.NewBackendPool( fakeBackends, @@ -68,11 +73,12 @@ func NewFakeClusterManager(clusterName, firewallName string) *fakeClusterManager l7Pool := loadbalancers.NewLoadBalancerPool(fakeLbs, namer) frPool := firewalls.NewFirewallPool(firewalls.NewFakeFirewallsProvider(false, false), namer, testSrcRanges, testNodePortRanges) cm := &ClusterManager{ - ClusterNamer: namer, - instancePool: nodePool, - backendPool: backendPool, - l7Pool: l7Pool, - firewallPool: frPool, + ClusterNamer: namer, + instancePool: nodePool, + backendPool: backendPool, + l7Pool: l7Pool, + firewallPool: frPool, + defaultBackendSvcPort: testDefaultBeSvcPort, } return &fakeClusterManager{cm, fakeLbs, fakeBackends, fakeIGs, namer} } diff --git a/pkg/flags/flags.go b/pkg/flags/flags.go index 88701235ac..686e406bcc 100644 --- a/pkg/flags/flags.go +++ b/pkg/flags/flags.go @@ -38,24 +38,25 @@ const ( var ( // F are global flags for the controller. F = struct { - APIServerHost string - ClusterName string - ConfigFilePath string - DefaultSvc string - DeleteAllOnQuit bool - GCERateLimit RateLimitSpecs - HealthCheckPath string - HealthzPort int - Features *Features - InCluster bool - IngressClass string - KubeConfigFile string - ResyncPeriod time.Duration - Verbose bool - Version bool - WatchNamespace string - NodePortRanges PortRanges - EnableBackendConfig bool + APIServerHost string + ClusterName string + ConfigFilePath string + DefaultSvcHealthCheckPath string + DefaultSvc string + DeleteAllOnQuit bool + GCERateLimit RateLimitSpecs + HealthCheckPath string + HealthzPort int + Features *Features + InCluster bool + IngressClass string + KubeConfigFile string + ResyncPeriod time.Duration + Verbose bool + Version bool + WatchNamespace string + NodePortRanges PortRanges + EnableBackendConfig bool }{} ) @@ -97,6 +98,9 @@ resources.`) flag.StringVar(&F.ConfigFilePath, "config-file-path", "", `Path to a file containing the gce config. If left unspecified this controller only works with default zones.`) + flag.StringVar(&F.DefaultSvcHealthCheckPath, "default-backend-health-check-path", "/healthz", + `Path used to health-check a default backend service. The default backend service +must serve a 200 page on this path.`) flag.StringVar(&F.DefaultSvc, "default-backend-service", "kube-system/default-http-backend", `Service used to serve a 404 page for the default backend. Takes the form namespace/name. The controller uses the first node port of this Service for diff --git a/pkg/healthchecks/healthchecks.go b/pkg/healthchecks/healthchecks.go index 3f479413cc..cd24adf2ff 100644 --- a/pkg/healthchecks/healthchecks.go +++ b/pkg/healthchecks/healthchecks.go @@ -22,6 +22,7 @@ import ( computealpha "google.golang.org/api/compute/v0.alpha" compute "google.golang.org/api/compute/v1" + "k8s.io/apimachinery/pkg/types" "github.com/golang/glog" @@ -64,39 +65,43 @@ const ( // HealthChecks manages health checks. type HealthChecks struct { - cloud HealthCheckProvider - defaultPath string - namer *utils.Namer + cloud HealthCheckProvider + // path is the default health check path for backends. + path string + // defaultBackend is the default health check path for the default backend. + defaultBackendPath string + namer *utils.Namer + // This is a workaround which allows us to not have to maintain + // a separate health checker for the default backend. + defaultBackendSvc types.NamespacedName } // NewHealthChecker creates a new health checker. // cloud: the cloud object implementing SingleHealthCheck. // defaultHealthCheckPath: is the HTTP path to use for health checks. -func NewHealthChecker(cloud HealthCheckProvider, defaultHealthCheckPath string, namer *utils.Namer) HealthChecker { - return &HealthChecks{cloud, defaultHealthCheckPath, namer} +func NewHealthChecker(cloud HealthCheckProvider, healthCheckPath string, defaultBackendHealthCheckPath string, namer *utils.Namer, defaultBackendSvc types.NamespacedName) HealthChecker { + return &HealthChecks{cloud, healthCheckPath, defaultBackendHealthCheckPath, namer, defaultBackendSvc} } // New returns a *HealthCheck with default settings and specified port/protocol -func (h *HealthChecks) New(name string, port int64, protocol annotations.AppProtocol, enableNEG bool) *HealthCheck { +func (h *HealthChecks) New(sp utils.ServicePort) *HealthCheck { var hc *HealthCheck - if enableNEG { - hc = DefaultNEGHealthCheck(protocol) + if sp.NEGEnabled { + hc = DefaultNEGHealthCheck(sp.Protocol) } else { - hc = DefaultHealthCheck(port, protocol) + hc = DefaultHealthCheck(sp.NodePort, sp.Protocol) } - - hc.Name = name + // port is the key for retriving existing health-check + // TODO: rename backend-service and health-check to not use port as key + hc.Name = sp.BackendName(h.namer) + hc.Port = sp.NodePort + hc.RequestPath = h.pathFromSvcPort(sp) return hc } // Sync retrieves a health check based on port, checks type and settings 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 *HealthCheck) (string, error) { - // Verify default path - if hc.RequestPath == "" { - hc.RequestPath = h.defaultPath - } - // Use alpha API when PORT_SPECIFICATION field is specified or when Type // is HTTP2 existingHC, err := h.Get(hc.Name, hc.isHttp2() || hc.ForNEG) @@ -223,12 +228,7 @@ func (h *HealthChecks) DeleteLegacy(port int64) error { // DefaultHealthCheck simply returns the default health check. func DefaultHealthCheck(port int64, protocol annotations.AppProtocol) *HealthCheck { - httpSettings := computealpha.HTTPHealthCheck{ - Port: port, - // Empty string is used as a signal to the caller to use the appropriate - // default. - RequestPath: "", - } + httpSettings := computealpha.HTTPHealthCheck{Port: port} hcSettings := computealpha.HealthCheck{ // How often to health check. @@ -252,12 +252,7 @@ func DefaultHealthCheck(port int64, protocol annotations.AppProtocol) *HealthChe // DefaultHealthCheck simply returns the default health check. func DefaultNEGHealthCheck(protocol annotations.AppProtocol) *HealthCheck { - httpSettings := computealpha.HTTPHealthCheck{ - PortSpecification: UseServingPortSpecification, - // Empty string is used as a signal to the caller to use the appropriate - // default. - RequestPath: "", - } + httpSettings := computealpha.HTTPHealthCheck{PortSpecification: UseServingPortSpecification} hcSettings := computealpha.HealthCheck{ // How often to health check. @@ -389,6 +384,15 @@ func toAlphaHealthCheck(hc *compute.HealthCheck) (*computealpha.HealthCheck, err return ret, err } +// pathFromSvcPort returns the default path for a health check based on whether +// the passed in ServicePort is associated with the system default backend. +func (h *HealthChecks) pathFromSvcPort(sp utils.ServicePort) string { + if h.defaultBackendSvc == sp.SvcName { + return h.defaultBackendPath + } + return h.path +} + type jsonConvertable interface { MarshalJSON() ([]byte, error) } diff --git a/pkg/healthchecks/healthchecks_test.go b/pkg/healthchecks/healthchecks_test.go index 57d62293dc..9a92366ca8 100644 --- a/pkg/healthchecks/healthchecks_test.go +++ b/pkg/healthchecks/healthchecks_test.go @@ -22,17 +22,23 @@ import ( compute "google.golang.org/api/compute/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/ingress-gce/pkg/annotations" "k8s.io/ingress-gce/pkg/utils" ) -var namer = utils.NewNamer("uid1", "fw1") +var ( + namer = utils.NewNamer("uid1", "fw1") + + defaultBackendSvc = types.NamespacedName{Namespace: "system", Name: "default"} +) func TestHealthCheckAdd(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) - hc := healthChecks.New(namer.IGBackend(80), 80, annotations.ProtocolHTTP, false) + sp := utils.ServicePort{NodePort: 80, Protocol: annotations.ProtocolHTTP, NEGEnabled: false} + hc := healthChecks.New(sp) _, err := healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -43,7 +49,8 @@ func TestHealthCheckAdd(t *testing.T) { t.Fatalf("expected the health check to exist, err: %v", err) } - hc = healthChecks.New(namer.IGBackend(443), 443, annotations.ProtocolHTTPS, false) + sp = utils.ServicePort{NodePort: 443, Protocol: annotations.ProtocolHTTPS, NEGEnabled: false} + hc = healthChecks.New(sp) _, err = healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -54,7 +61,8 @@ func TestHealthCheckAdd(t *testing.T) { t.Fatalf("expected the health check to exist, err: %v", err) } - hc = healthChecks.New(namer.IGBackend(3000), 3000, annotations.ProtocolHTTP2, false) + sp = utils.ServicePort{NodePort: 3000, Protocol: annotations.ProtocolHTTP2, NEGEnabled: false} + hc = healthChecks.New(sp) _, err = healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -68,7 +76,7 @@ func TestHealthCheckAdd(t *testing.T) { func TestHealthCheckAddExisting(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) // HTTP // Manually insert a health check @@ -81,8 +89,9 @@ func TestHealthCheckAddExisting(t *testing.T) { } hcp.CreateHealthCheck(v1hc) + sp := utils.ServicePort{NodePort: 3000, Protocol: annotations.ProtocolHTTP, NEGEnabled: false} // Should not fail adding the same type of health check - hc := healthChecks.New(namer.IGBackend(3000), 3000, annotations.ProtocolHTTP, false) + hc := healthChecks.New(sp) _, err = healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -104,7 +113,8 @@ func TestHealthCheckAddExisting(t *testing.T) { } hcp.CreateHealthCheck(v1hc) - hc = healthChecks.New(namer.IGBackend(4000), 4000, annotations.ProtocolHTTPS, false) + sp = utils.ServicePort{NodePort: 4000, Protocol: annotations.ProtocolHTTPS, NEGEnabled: false} + hc = healthChecks.New(sp) _, err = healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -126,7 +136,8 @@ func TestHealthCheckAddExisting(t *testing.T) { } hcp.CreateHealthCheck(v1hc) - hc = healthChecks.New(namer.IGBackend(5000), 5000, annotations.ProtocolHTTPS, false) + sp = utils.ServicePort{NodePort: 5000, Protocol: annotations.ProtocolHTTPS, NEGEnabled: false} + hc = healthChecks.New(sp) _, err = healthChecks.Sync(hc) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -140,7 +151,7 @@ func TestHealthCheckAddExisting(t *testing.T) { func TestHealthCheckDelete(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) // Create HTTP HC for 1234 hc := DefaultHealthCheck(1234, annotations.ProtocolHTTP) @@ -176,7 +187,7 @@ func TestHealthCheckDelete(t *testing.T) { func TestHTTP2HealthCheckDelete(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) // Create HTTP2 HC for 1234 hc := DefaultHealthCheck(1234, annotations.ProtocolHTTP2) @@ -200,7 +211,7 @@ func TestHTTP2HealthCheckDelete(t *testing.T) { func TestHealthCheckUpdate(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) // HTTP // Manually insert a health check @@ -301,7 +312,7 @@ func TestHealthCheckUpdate(t *testing.T) { func TestHealthCheckDeleteLegacy(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) err := hcp.CreateHttpHealthCheck(&compute.HttpHealthCheck{ Name: namer.IGBackend(80), @@ -319,8 +330,9 @@ func TestHealthCheckDeleteLegacy(t *testing.T) { func TestAlphaHealthCheck(t *testing.T) { hcp := NewFakeHealthCheckProvider() - healthChecks := NewHealthChecker(hcp, "/", namer) - hc := healthChecks.New(namer.IGBackend(8000), 8000, annotations.ProtocolHTTP, true) + healthChecks := NewHealthChecker(hcp, "/", "/healthz", namer, defaultBackendSvc) + sp := utils.ServicePort{NodePort: 8000, Protocol: annotations.ProtocolHTTPS, NEGEnabled: true} + hc := healthChecks.New(sp) _, err := healthChecks.Sync(hc) if err != nil { t.Fatalf("got %v, want nil", err) diff --git a/pkg/healthchecks/interfaces.go b/pkg/healthchecks/interfaces.go index 12ccd614d1..18dce76e9d 100644 --- a/pkg/healthchecks/interfaces.go +++ b/pkg/healthchecks/interfaces.go @@ -20,7 +20,7 @@ import ( computealpha "google.golang.org/api/compute/v0.alpha" compute "google.golang.org/api/compute/v1" - "k8s.io/ingress-gce/pkg/annotations" + "k8s.io/ingress-gce/pkg/utils" ) // HealthCheckProvider is an interface to manage a single GCE health check. @@ -41,7 +41,7 @@ type HealthCheckProvider interface { // HealthChecker is an interface to manage cloud HTTPHealthChecks. type HealthChecker interface { - New(name string, port int64, protocol annotations.AppProtocol, enableNEG bool) *HealthCheck + New(sp utils.ServicePort) *HealthCheck Sync(hc *HealthCheck) (string, error) Delete(name string) error Get(name string, alpha bool) (*HealthCheck, error)