2015-02-26 03:15:07 +01:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-11-17 15:25:04 +01:00
|
|
|
"fmt"
|
2015-08-20 02:57:18 +02:00
|
|
|
"net"
|
2015-02-26 03:15:07 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-11-17 15:25:04 +01:00
|
|
|
"path/filepath"
|
2015-08-19 02:19:46 +02:00
|
|
|
"strconv"
|
2015-08-20 02:57:18 +02:00
|
|
|
"time"
|
2015-08-19 02:19:46 +02:00
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3/health"
|
2015-02-26 03:15:07 +01:00
|
|
|
)
|
|
|
|
|
2015-08-20 02:57:18 +02:00
|
|
|
// FileChecker checks the existence of a file and returns an error
|
|
|
|
// if the file exists.
|
2015-02-26 03:15:07 +01:00
|
|
|
func FileChecker(f string) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
2016-11-17 15:25:04 +01: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 03:15:07 +01:00
|
|
|
return errors.New("file exists")
|
2016-11-17 15:25:04 +01:00
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return nil
|
2015-02-26 03:15:07 +01:00
|
|
|
}
|
2016-11-17 15:25:04 +01:00
|
|
|
|
|
|
|
return err
|
2015-02-26 03:15:07 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-20 02:57:18 +02:00
|
|
|
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
|
|
|
// returned matches statusCode.
|
2015-08-20 03:23:58 +02:00
|
|
|
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
|
2015-02-26 03:15:07 +01:00
|
|
|
return health.CheckFunc(func() error {
|
2015-08-20 02:57:18 +02:00
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
2015-08-20 03:23:58 +02:00
|
|
|
req, err := http.NewRequest("HEAD", r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("error creating request: " + r)
|
|
|
|
}
|
|
|
|
for headerName, headerValues := range headers {
|
|
|
|
for _, headerValue := range headerValues {
|
|
|
|
req.Header.Add(headerName, headerValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response, err := client.Do(req)
|
2015-02-26 03:15:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("error while checking: " + r)
|
|
|
|
}
|
2015-08-20 02:57:18 +02:00
|
|
|
if response.StatusCode != statusCode {
|
2015-08-19 02:19:46 +02:00
|
|
|
return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
|
2015-02-26 03:15:07 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-08-20 02:57:18 +02:00
|
|
|
|
|
|
|
// TCPChecker attempts to open a TCP connection.
|
|
|
|
func TCPChecker(addr string, timeout time.Duration) health.Checker {
|
|
|
|
return health.CheckFunc(func() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("connection to " + addr + " failed")
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|