k3s/pkg/agent/loadbalancer/utility.go
Brad Davidson cd4ddedbc9 Fix issue with loadbalancer failover to default server
The loadbalancer should only fail over to the default server if all other server have failed, and it should force fail-back to a preferred server as soon as one passes health checks.

The loadbalancer tests have been improved to ensure that this occurs.

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2024-11-13 19:41:45 -08:00

52 lines
1.1 KiB
Go

package loadbalancer
import (
"errors"
"net/url"
"sort"
"strings"
)
func parseURL(serverURL, newHost string) (string, string, error) {
parsedURL, err := url.Parse(serverURL)
if err != nil {
return "", "", err
}
if parsedURL.Host == "" {
return "", "", errors.New("Initial server URL host is not defined for load balancer")
}
address := parsedURL.Host
if parsedURL.Port() == "" {
if strings.ToLower(parsedURL.Scheme) == "http" {
address += ":80"
}
if strings.ToLower(parsedURL.Scheme) == "https" {
address += ":443"
}
}
parsedURL.Host = newHost
return address, parsedURL.String(), nil
}
// sortServers returns a sorted, unique list of strings, with any
// empty values removed. The returned bool is true if the list
// contains the search string.
func sortServers(input []string, search string) ([]string, bool) {
result := []string{}
found := false
skip := map[string]bool{"": true}
for _, entry := range input {
if skip[entry] {
continue
}
if search == entry {
found = true
}
skip[entry] = true
result = append(result, entry)
}
sort.Strings(result)
return result, found
}