UpdateLoadbalancerConfig: deduplicate and warn if node is down

- getCluster: now deduplicates the node list (i.e. overwrites existing
nodes in the list with "fresh" nodes and appends new ones)
- UpdateLoadbalancerConfig: print warning if "host not found in
upstream" returned by nginx after updating the config, as it's not a
failure per se, but shouldn't be ignored
This commit is contained in:
iwilltry42 2020-06-03 12:52:10 +02:00
parent f0d3389a1f
commit b28f93d207
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
2 changed files with 21 additions and 2 deletions

View File

@ -29,6 +29,7 @@ import (
"strings"
"time"
"github.com/imdario/mergo"
k3drt "github.com/rancher/k3d/pkg/runtimes"
"github.com/rancher/k3d/pkg/types"
k3d "github.com/rancher/k3d/pkg/types"
@ -460,7 +461,22 @@ func GetCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster
// append nodes
for _, node := range nodes {
cluster.Nodes = append(cluster.Nodes, node)
// check if there's already a node in the struct
overwroteExisting := false
for _, existingNode := range cluster.Nodes {
// overwrite existing node
if existingNode.Name == node.Name {
mergo.MergeWithOverwrite(existingNode, node)
overwroteExisting = true
}
}
// no existing node overwritten: append new node
if !overwroteExisting {
cluster.Nodes = append(cluster.Nodes, node)
}
}
if err := populateClusterFieldsFromLabels(cluster); err != nil {

View File

@ -61,7 +61,10 @@ func UpdateLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, clu
command := fmt.Sprintf("SERVERS=%s %s", masterNodes, "confd -onetime -backend env && nginx -s reload")
if err := runtime.ExecInNode(ctx, loadbalancer, []string{"sh", "-c", command}); err != nil {
log.Errorln("Failed to update loadbalancer configuration")
if strings.Contains(err.Error(), "host not found in upstream") {
log.Warnf("Loadbalancer configuration updated, but one or more k3d nodes seem to be down, check the logs:\n%s", err.Error())
return nil
}
return err
}