Skip to content

Commit

Permalink
Add UT for route agent health checker
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin Suryanarayanan <asuryana@redhat.com>
  • Loading branch information
aswinsuryan committed Sep 10, 2024
1 parent be1d40a commit 5bdc520
Show file tree
Hide file tree
Showing 4 changed files with 484 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 healthchecker_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/log/kzerolog"
submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
"k8s.io/client-go/kubernetes/scheme"
)

func init() {
kzerolog.AddFlags(nil)
}

var _ = BeforeSuite(func() {
kzerolog.InitK8sLogging()
Expect(submarinerv1.AddToScheme(scheme.Scheme)).To(Succeed())
})

func TestHealthChecker(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "RouteAgent Health Checker Suite")
}
51 changes: 29 additions & 22 deletions pkg/routeagent_driver/handlers/healthchecker/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var RouteAgentUpdateInterval = 60 * time.Second

type Config struct {
PingInterval uint
MaxPacketLossCount uint
HealthCheckerEnabled bool
NewPinger func(pinger.Config) pinger.Interface
PingInterval uint
MaxPacketLossCount uint
HealthCheckerEnabled bool
RouteAgentUpdateInterval time.Duration
NewPinger func(pinger.Config) pinger.Interface
}

type controller struct {
Expand Down Expand Up @@ -84,7 +83,10 @@ func (h *controller) Stop() error {

h.pingers = map[string]pinger.Interface{}

close(h.stopCh)
if h.stopCh != nil {
close(h.stopCh)
h.stopCh = nil
}

err := h.client.Delete(context.TODO(),
h.localNodeName, metav1.DeleteOptions{})
Expand All @@ -96,36 +98,43 @@ func (h *controller) Stop() error {
}

func (h *controller) RemoteEndpointCreated(endpoint *submarinerv1.Endpoint) error {
h.Lock()
defer h.Unlock()

if !h.config.HealthCheckerEnabled || h.State().IsOnGateway() {
return nil
}

return h.processEndpointCreatedOrUpdated(endpoint)
h.processEndpointCreatedOrUpdated(endpoint)

return nil
}

func (h *controller) RemoteEndpointUpdated(endpoint *submarinerv1.Endpoint) error {
h.Lock()
defer h.Unlock()

if !h.config.HealthCheckerEnabled || h.State().IsOnGateway() {
return nil
}

return h.processEndpointCreatedOrUpdated(endpoint)
h.processEndpointCreatedOrUpdated(endpoint)

return nil
}

func (h *controller) processEndpointCreatedOrUpdated(endpoint *submarinerv1.Endpoint) error {
func (h *controller) processEndpointCreatedOrUpdated(endpoint *submarinerv1.Endpoint) {
logger.Infof("Processing Endpoint: %#v", endpoint)

if endpoint.Spec.HealthCheckIP == "" || endpoint.Spec.CableName == "" {
logger.Infof("HealthCheckIP (%q) and/or CableName (%q) for Endpoint %q empty - will not monitor endpoint health",
endpoint.Spec.HealthCheckIP, endpoint.Spec.CableName, endpoint.Name)
return nil
return
}

h.Lock()
defer h.Unlock()

if pingerObject, found := h.pingers[endpoint.Spec.CableName]; found {
if pingerObject.GetIP() == endpoint.Spec.HealthCheckIP {
return nil
return
}

logger.Infof("HealthChecker is already running for %q - stopping", endpoint.Name)
Expand Down Expand Up @@ -156,8 +165,6 @@ func (h *controller) processEndpointCreatedOrUpdated(endpoint *submarinerv1.Endp

logger.Infof("HealthChecker started pinger for CableName: %q with HealthCheckIP %q",
endpoint.Spec.CableName, endpoint.Spec.HealthCheckIP)

return nil
}

func (h *controller) RemoteEndpointRemoved(endpoint *submarinerv1.Endpoint) error {
Expand All @@ -179,7 +186,7 @@ func (h *controller) Init() error {
defer h.Unlock()

h.syncRouteAgentStatus()
}, RouteAgentUpdateInterval, h.stopCh)
}, h.config.RouteAgentUpdateInterval, h.stopCh)
}()

return nil
Expand All @@ -194,10 +201,7 @@ func (h *controller) TransitionToNonGateway() error {
remoteEndpoints := h.State().GetRemoteEndpoints()

for i := range remoteEndpoints {
err := h.processEndpointCreatedOrUpdated(&remoteEndpoints[i])
if err != nil {
return errors.Wrapf(err, "error processing remote endpoint %s", remoteEndpoints[i].Name)
}
h.processEndpointCreatedOrUpdated(&remoteEndpoints[i])
}
}

Expand Down Expand Up @@ -271,6 +275,9 @@ func (h *controller) syncRouteAgentStatus() {
connectionStatus = submarinerv1.Connecting
statusMessage = ""
}
} else {
connectionStatus = submarinerv1.ConnectionNone
statusMessage = "Health checker IP is not configured"
}

remoteEndpoint = submarinerv1.RemoteEndpoint{
Expand Down
Loading

0 comments on commit 5bdc520

Please sign in to comment.