From b37c8d06d808ee8665cea59702af963fef5085b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Nussbaumer?= Date: Sat, 20 Jul 2024 07:03:12 +0200 Subject: [PATCH] refactor: simplify check functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Clément Nussbaumer --- internal/servicecheck/neighbours.go | 2 +- internal/servicecheck/servicecheck.go | 21 +++++++++------------ internal/servicecheck/transport.go | 12 +++++++----- internal/servicecheck/types.go | 2 +- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/internal/servicecheck/neighbours.go b/internal/servicecheck/neighbours.go index b8fedee..94a8f8b 100644 --- a/internal/servicecheck/neighbours.go +++ b/internal/servicecheck/neighbours.go @@ -95,7 +95,7 @@ func (c *Checker) checkNeighbours(nh []*Neighbour) { } for _, neighbour := range nh { - check := func(ctx context.Context) (string, error) { + check := func(ctx context.Context) string { if c.UseTLS { return c.doRequest(ctx, "https://"+neighbour.PodIP+":8443/alwayshappy", true) } diff --git a/internal/servicecheck/servicecheck.go b/internal/servicecheck/servicecheck.go index 548749c..36d9756 100644 --- a/internal/servicecheck/servicecheck.go +++ b/internal/servicecheck/servicecheck.go @@ -118,9 +118,9 @@ func (c *Checker) StopScheduled() { } // APIServerDirect checks the /version endpoint of the Kubernetes API Server through the direct link -func (c *Checker) APIServerDirect(ctx context.Context) (string, error) { +func (c *Checker) APIServerDirect(ctx context.Context) string { if c.SkipCheckAPIServerDirect { - return skippedStr, nil + return skippedStr } apiurl := fmt.Sprintf("https://%s:%s/version", c.KubernetesServiceHost, c.KubernetesServicePort) @@ -129,9 +129,9 @@ func (c *Checker) APIServerDirect(ctx context.Context) (string, error) { } // APIServerDNS checks the /version endpoint of the Kubernetes API Server through the Cluster DNS URL -func (c *Checker) APIServerDNS(ctx context.Context) (string, error) { +func (c *Checker) APIServerDNS(ctx context.Context) string { if c.SkipCheckAPIServerDNS { - return skippedStr, nil + return skippedStr } apiurl := fmt.Sprintf("https://kubernetes.default.svc.cluster.local:%s/version", c.KubernetesServicePort) @@ -140,18 +140,18 @@ func (c *Checker) APIServerDNS(ctx context.Context) (string, error) { } // MeIngress checks if the kubenurse is reachable at the /alwayshappy endpoint behind the ingress -func (c *Checker) MeIngress(ctx context.Context) (string, error) { +func (c *Checker) MeIngress(ctx context.Context) string { if c.SkipCheckMeIngress { - return skippedStr, nil + return skippedStr } return c.doRequest(ctx, c.KubenurseIngressURL+"/alwayshappy", false) //nolint:goconst // readability } // MeService checks if the kubenurse is reachable at the /alwayshappy endpoint through the kubernetes service -func (c *Checker) MeService(ctx context.Context) (string, error) { +func (c *Checker) MeService(ctx context.Context) string { if c.SkipCheckMeService { - return skippedStr, nil + return skippedStr } return c.doRequest(ctx, c.KubenurseServiceURL+"/alwayshappy", false) @@ -163,8 +163,5 @@ func (c *Checker) measure(check Check, requestType string) string { // metrics and errors based with the label ctx := context.WithValue(context.Background(), kubenurseTypeKey{}, requestType) - // Execute check - res, _ := check(ctx) // this error is ignored as it is already logged in httptrace - - return res + return check(ctx) } diff --git a/internal/servicecheck/transport.go b/internal/servicecheck/transport.go index 06c445b..996f3da 100644 --- a/internal/servicecheck/transport.go +++ b/internal/servicecheck/transport.go @@ -6,6 +6,7 @@ import ( "crypto/x509" "errors" "fmt" + "log/slog" "net/http" "os" "strings" @@ -18,11 +19,12 @@ const ( ) // doRequest does an http request only to get the http status code -func (c *Checker) doRequest(ctx context.Context, url string, addOriginHeader bool) (string, error) { +func (c *Checker) doRequest(ctx context.Context, url string, addOriginHeader bool) string { // Read Bearer Token file from ServiceAccount token, err := os.ReadFile(K8sTokenFile) if err != nil { - return errStr, fmt.Errorf("load kubernetes serviceaccount token from %s: %w", K8sTokenFile, err) + slog.Error("error in doRequest while reading k8sTokenFile", "err", err) + return errStr } req, _ := http.NewRequestWithContext(ctx, "GET", url, http.NoBody) @@ -39,17 +41,17 @@ func (c *Checker) doRequest(ctx context.Context, url string, addOriginHeader boo resp, err := c.httpClient.Do(req) if err != nil { - return err.Error(), err + return err.Error() } // Body is non-nil if err is nil, so close it _ = resp.Body.Close() if resp.StatusCode == http.StatusOK { - return okStr, nil + return okStr } - return resp.Status, errors.New(resp.Status) + return resp.Status } // generateTLSConfig returns a TLSConfig including K8s CA and the user-defined extraCA diff --git a/internal/servicecheck/types.go b/internal/servicecheck/types.go index bedcd4e..f47c9fa 100644 --- a/internal/servicecheck/types.go +++ b/internal/servicecheck/types.go @@ -60,4 +60,4 @@ type Checker struct { } // Check is the signature used by all checks that the checker can execute. -type Check func(ctx context.Context) (string, error) +type Check func(ctx context.Context) string