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) {