mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 11:00:25 +00:00
Merge pull request #975 from nathanleclaire/fix_daemon_avail_check
Fix daemon availability check
This commit is contained in:
commit
fa62544f14
@ -3,20 +3,29 @@ package main
|
|||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errDockerPingNotOK = errors.New("Docker /_ping did not return OK")
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
healthcheckTimeout = 5 * time.Second
|
||||||
dockerSock = "/var/run/docker.sock"
|
dockerSock = "/var/run/docker.sock"
|
||||||
lgtm = "LGTM"
|
dockerPingOK = "LGTM"
|
||||||
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
|
httpMagicPort = ":44554" // chosen arbitrarily due to IANA availability -- might change
|
||||||
bucket = "editionsdiagnostics"
|
bucket = "editionsdiagnostics"
|
||||||
sessionIDField = "session"
|
sessionIDField = "session"
|
||||||
@ -36,14 +45,67 @@ func init() {
|
|||||||
// for cloud editions.
|
// for cloud editions.
|
||||||
type HTTPDiagnosticListener struct{}
|
type HTTPDiagnosticListener struct{}
|
||||||
|
|
||||||
|
// UnixSocketRoundTripper provides a way to make HTTP request to Docker socket
|
||||||
|
// directly.
|
||||||
|
type UnixSocketRoundTripper struct{}
|
||||||
|
|
||||||
|
// RoundTrip dials the Docker UNIX socket to make a HTTP request.
|
||||||
|
func (u UnixSocketRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
dial, err := net.Dial("unix", dockerSock)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn := httputil.NewClientConn(dial, nil)
|
||||||
|
defer conn.Close()
|
||||||
|
return conn.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
|
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
|
||||||
func (h HTTPDiagnosticListener) Listen() {
|
func (h HTTPDiagnosticListener) Listen() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, err := os.Stat(dockerSock); os.IsNotExist(err) {
|
client := &http.Client{
|
||||||
http.Error(w, "Docker socket not found -- daemon is down", http.StatusServiceUnavailable)
|
Transport: &UnixSocketRoundTripper{},
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, "/_ping", nil)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Error creating HTTP request to talk to Docker", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := w.Write([]byte(lgtm)); err != nil {
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
|
errCh := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
errCh <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
errCh <- errDockerPingNotOK
|
||||||
|
return
|
||||||
|
}
|
||||||
|
errCh <- nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-errCh:
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Docker daemon ping error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
http.Error(w, "Docker daemon ping timed out", http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write([]byte(dockerPingOK)); err != nil {
|
||||||
log.Println("Error writing HTTP success response:", err)
|
log.Println("Error writing HTTP success response:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user