Merge pull request #10405 from dragonchaser/bugfix-healthchecks

avoid 0.0.0.0 & replace by outbound ip
This commit is contained in:
Christian Richter
2024-10-24 10:28:17 +00:00
committed by GitHub
4 changed files with 61 additions and 4 deletions

View File

@@ -4,14 +4,19 @@ import (
"context"
"fmt"
"google.golang.org/grpc/credentials/insecure"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// NewGRPCCheck checks the reachability of a grpc server.
func NewGRPCCheck(address string) 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()))
if err != nil {
return fmt.Errorf("could not connect to grpc server: %v", err)

View File

@@ -4,12 +4,24 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
)
// NewHTTPCheck checks the reachability of a http server.
func NewHTTPCheck(url string) 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{
Timeout: 3 * time.Second,
}

View File

@@ -4,11 +4,18 @@ import (
"context"
"net"
"time"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
)
// NewTCPCheck returns a check that connects to a given tcp endpoint.
func NewTCPCheck(address string) 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)
if err != nil {
return err

View File

@@ -5,11 +5,12 @@ import (
"fmt"
"io"
"maps"
"net"
"net/http"
"golang.org/x/sync/errgroup"
"strings"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"golang.org/x/sync/errgroup"
)
// 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")
}
}
// 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")
}