Skip to content

Commit

Permalink
Merge pull request #10405 from dragonchaser/bugfix-healthchecks
Browse files Browse the repository at this point in the history
avoid 0.0.0.0 & replace by outbound ip
  • Loading branch information
dragonchaser authored Oct 24, 2024
2 parents 4dfba21 + fd26297 commit 77a554d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ocis-pkg/checks/checkgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions ocis-pkg/checks/checkhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
7 changes: 7 additions & 0 deletions ocis-pkg/checks/checktcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions ocis-pkg/handlers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
}

0 comments on commit 77a554d

Please sign in to comment.