Skip to content

Commit

Permalink
refactor: simplify check functions
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Nussbaumer <[email protected]>
  • Loading branch information
clementnuss committed Jul 20, 2024
1 parent be906bb commit b37c8d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/servicecheck/neighbours.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 9 additions & 12 deletions internal/servicecheck/servicecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
12 changes: 7 additions & 5 deletions internal/servicecheck/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"strings"
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/servicecheck/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b37c8d0

Please sign in to comment.