From f73e18232883b218dfca53982561b2e0af8f9edf Mon Sep 17 00:00:00 2001 From: Patryk Strusiewicz-Surmacki Date: Wed, 17 Apr 2024 18:00:05 +0200 Subject: [PATCH] Added healthcheck changes Signed-off-by: Patryk Strusiewicz-Surmacki --- pkg/healthcheck/healthcheck.go | 9 +++++++++ pkg/healthcheck/healthcheck_test.go | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/healthcheck/healthcheck.go b/pkg/healthcheck/healthcheck.go index d09e29a4..09cf76cb 100644 --- a/pkg/healthcheck/healthcheck.go +++ b/pkg/healthcheck/healthcheck.go @@ -163,6 +163,15 @@ func (hc *HealthChecker) CheckReachability() error { return nil } +// CheckAPIServer checks if Kubernetes API Server is reachable. +func (hc *HealthChecker) CheckAPIServer(ctx context.Context) error { + pods := &corev1.PodList{} + if err := hc.client.List(ctx, pods); err != nil { + return fmt.Errorf("error listing pods: %w", err) + } + return nil +} + func (hc *HealthChecker) checkReachabilityItem(r netReachabilityItem) error { target := r.Host + ":" + strconv.Itoa(r.Port) conn, err := hc.toolkit.tcpDialer.Dial("tcp", target) diff --git a/pkg/healthcheck/healthcheck_test.go b/pkg/healthcheck/healthcheck_test.go index 011df4f2..3e7003b2 100644 --- a/pkg/healthcheck/healthcheck_test.go +++ b/pkg/healthcheck/healthcheck_test.go @@ -262,6 +262,16 @@ var _ = Describe("HealthCheck", func() { Expect(err).To(HaveOccurred()) }) }) + Context("CheckAPIServer() should", func() { + It("return no error", func() { + c := fake.NewClientBuilder().Build() + hc, err := NewHealthChecker(c, NewHealthCheckToolkit(nil, nil, nil), &NetHealthcheckConfig{}) + Expect(err).ToNot(HaveOccurred()) + Expect(hc).ToNot(BeNil()) + err = hc.CheckAPIServer(context.TODO()) + Expect(err).ToNot(HaveOccurred()) + }) + }) }) func fakeErrorGetByName(_ string) (netlink.Link, error) {