Merge pull request #10405 from dragonchaser/bugfix-healthchecks
avoid 0.0.0.0 & replace by outbound ip
This commit is contained in:
@@ -4,14 +4,19 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewGRPCCheck checks the reachability of a grpc server.
|
// NewGRPCCheck checks the reachability of a grpc server.
|
||||||
func NewGRPCCheck(address string) func(context.Context) error {
|
func NewGRPCCheck(address string) func(context.Context) error {
|
||||||
return func(_ context.Context) error {
|
return func(_ context.Context) error {
|
||||||
|
address, err := handlers.FailSaveAddress(address)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not connect to grpc server: %v", err)
|
return fmt.Errorf("could not connect to grpc server: %v", err)
|
||||||
|
|||||||
@@ -4,12 +4,24 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTPCheck checks the reachability of a http server.
|
// NewHTTPCheck checks the reachability of a http server.
|
||||||
func NewHTTPCheck(url string) func(context.Context) error {
|
func NewHTTPCheck(url string) func(context.Context) error {
|
||||||
return func(_ context.Context) error {
|
return func(_ context.Context) error {
|
||||||
|
url, err := handlers.FailSaveAddress(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
||||||
|
url = "http://" + url
|
||||||
|
}
|
||||||
|
|
||||||
c := http.Client{
|
c := http.Client{
|
||||||
Timeout: 3 * time.Second,
|
Timeout: 3 * time.Second,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTCPCheck returns a check that connects to a given tcp endpoint.
|
// NewTCPCheck returns a check that connects to a given tcp endpoint.
|
||||||
func NewTCPCheck(address string) func(context.Context) error {
|
func NewTCPCheck(address string) func(context.Context) error {
|
||||||
return func(_ context.Context) error {
|
return func(_ context.Context) error {
|
||||||
|
address, err := handlers.FailSaveAddress(address)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
|
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"maps"
|
"maps"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
|
|
||||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
// check is a function that performs a check.
|
// check is a function that performs a check.
|
||||||
@@ -113,3 +114,35 @@ func (h *CheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
h.conf.logger.Panic().Err(err).Msg("failed to write response")
|
h.conf.logger.Panic().Err(err).Msg("failed to write response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FailSaveAddress replaces wildcard addresses with the outbound IP.
|
||||||
|
func FailSaveAddress(address string) (string, error) {
|
||||||
|
if strings.Contains(address, "0.0.0.0") || strings.Contains(address, "::") {
|
||||||
|
outboundIp, err := getOutBoundIP()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
address = strings.Replace(address, "0.0.0.0", outboundIp, 1)
|
||||||
|
address = strings.Replace(address, "::", "["+outboundIp+"]", 1)
|
||||||
|
address = strings.Replace(address, "[::]", "["+outboundIp+"]", 1)
|
||||||
|
}
|
||||||
|
return address, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getOutBoundIP returns the outbound IP address.
|
||||||
|
func getOutBoundIP() (string, error) {
|
||||||
|
interfacesAddresses, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, address := range interfacesAddresses {
|
||||||
|
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
||||||
|
if ipNet.IP.To4() != nil {
|
||||||
|
return ipNet.IP.String(), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("no IP found")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user