2015-02-26 02:15:07 +00:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
2023-10-27 19:44:25 +00:00
|
|
|
"context"
|
2015-02-26 02:15:07 +00:00
|
|
|
"errors"
|
2016-11-17 14:25:04 +00:00
|
|
|
"fmt"
|
2015-08-20 00:57:18 +00:00
|
|
|
"net"
|
2015-02-26 02:15:07 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-11-17 14:25:04 +00:00
|
|
|
"path/filepath"
|
2015-08-20 00:57:18 +00:00
|
|
|
"time"
|
2015-08-19 00:19:46 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/health"
|
2015-02-26 02:15:07 +00:00
|
|
|
)
|
|
|
|
|
2015-08-20 00:57:18 +00:00
|
|
|
// FileChecker checks the existence of a file and returns an error
|
|
|
|
// if the file exists.
|
2015-02-26 02:15:07 +00:00
|
|
|
func FileChecker(f string) health.Checker {
|
2023-10-27 19:44:25 +00:00
|
|
|
return health.CheckFunc(func(context.Context) error {
|
2016-11-17 14:25:04 +00:00
|
|
|
absoluteFilePath, err := filepath.Abs(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get absolute path for %q: %v", f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(absoluteFilePath)
|
|
|
|
if err == nil {
|
2015-02-26 02:15:07 +00:00
|
|
|
return errors.New("file exists")
|
2016-11-17 14:25:04 +00:00
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return nil
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
2016-11-17 14:25:04 +00:00
|
|
|
|
|
|
|
return err
|
2015-02-26 02:15:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-20 00:57:18 +00:00
|
|
|
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
|
|
|
// returned matches statusCode.
|
2015-08-20 01:23:58 +00:00
|
|
|
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
|
2023-10-27 19:44:25 +00:00
|
|
|
return health.CheckFunc(func(ctx context.Context) error {
|
2015-08-20 00:57:18 +00:00
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
2023-10-27 19:44:25 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, r, nil)
|
2015-08-20 01:23:58 +00:00
|
|
|
if err != nil {
|
2023-10-27 19:44:25 +00:00
|
|
|
return fmt.Errorf("%v: error creating request: %w", r, err)
|
2015-08-20 01:23:58 +00:00
|
|
|
}
|
|
|
|
for headerName, headerValues := range headers {
|
|
|
|
for _, headerValue := range headerValues {
|
|
|
|
req.Header.Add(headerName, headerValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response, err := client.Do(req)
|
2015-02-26 02:15:07 +00:00
|
|
|
if err != nil {
|
2023-10-27 19:44:25 +00:00
|
|
|
return fmt.Errorf("%v: error while checking: %w", r, err)
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
2023-08-19 08:16:02 +00:00
|
|
|
defer response.Body.Close()
|
2015-08-20 00:57:18 +00:00
|
|
|
if response.StatusCode != statusCode {
|
2023-10-27 19:44:25 +00:00
|
|
|
return fmt.Errorf("%v: downstream service returned unexpected status: %d", r, response.StatusCode)
|
2015-02-26 02:15:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-08-20 00:57:18 +00:00
|
|
|
|
|
|
|
// TCPChecker attempts to open a TCP connection.
|
|
|
|
func TCPChecker(addr string, timeout time.Duration) health.Checker {
|
2023-10-27 19:44:25 +00:00
|
|
|
return health.CheckFunc(func(ctx context.Context) error {
|
|
|
|
d := net.Dialer{Timeout: timeout}
|
|
|
|
conn, err := d.DialContext(ctx, "tcp", addr)
|
2015-08-20 00:57:18 +00:00
|
|
|
if err != nil {
|
2023-10-27 19:44:25 +00:00
|
|
|
return fmt.Errorf("%v: connection failed: %w", addr, err)
|
2015-08-20 00:57:18 +00:00
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|