|
|
@ -29,11 +29,12 @@ import ( |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/docker/go-connections/nat" |
|
|
|
"github.com/docker/go-connections/nat" |
|
|
|
|
|
|
|
"github.com/go-test/deep" |
|
|
|
"github.com/rancher/k3d/v4/pkg/runtimes" |
|
|
|
"github.com/rancher/k3d/v4/pkg/runtimes" |
|
|
|
"github.com/rancher/k3d/v4/pkg/types" |
|
|
|
"github.com/rancher/k3d/v4/pkg/types" |
|
|
|
k3d "github.com/rancher/k3d/v4/pkg/types" |
|
|
|
k3d "github.com/rancher/k3d/v4/pkg/types" |
|
|
|
log "github.com/sirupsen/logrus" |
|
|
|
log "github.com/sirupsen/logrus" |
|
|
|
"sigs.k8s.io/yaml" |
|
|
|
"gopkg.in/yaml.v2" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// UpdateLoadbalancerConfig updates the loadbalancer config with an updated list of servers belonging to that cluster
|
|
|
|
// UpdateLoadbalancerConfig updates the loadbalancer config with an updated list of servers belonging to that cluster
|
|
|
@ -47,25 +48,34 @@ func UpdateLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, clu |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// find the LoadBalancer for the target cluster
|
|
|
|
currentConfig, err := GetLoadbalancerConfig(ctx, runtime, cluster) |
|
|
|
serverNodesList := []string{} |
|
|
|
if err != nil { |
|
|
|
var loadbalancer *k3d.Node |
|
|
|
return fmt.Errorf("error getting current config from loadbalancer: %w", err) |
|
|
|
for _, node := range cluster.Nodes { |
|
|
|
|
|
|
|
if node.Role == k3d.LoadBalancerRole { // get the loadbalancer we want to update
|
|
|
|
|
|
|
|
loadbalancer = node |
|
|
|
|
|
|
|
} else if node.Role == k3d.ServerRole { // create a list of server nodes
|
|
|
|
|
|
|
|
serverNodesList = append(serverNodesList, node.Name) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Tracef("Current loadbalancer config:\n%+v", currentConfig) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newLBConfig, err := LoadbalancerGenerateConfig(cluster) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error generating new loadbalancer config: %w", err) |
|
|
|
} |
|
|
|
} |
|
|
|
serverNodes := strings.Join(serverNodesList, ",") |
|
|
|
log.Tracef("New loadbalancer config:\n%+v", currentConfig) |
|
|
|
if loadbalancer == nil { |
|
|
|
|
|
|
|
return fmt.Errorf("Failed to find loadbalancer for cluster '%s'", cluster.Name) |
|
|
|
if diff := deep.Equal(currentConfig, newLBConfig); diff != nil { |
|
|
|
|
|
|
|
log.Debugf("Updating the loadbalancer with this diff: %+v", diff) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("Servers as passed to serverlb: '%s'", serverNodes) |
|
|
|
newLbConfigYaml, err := yaml.Marshal(&newLBConfig) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error marshalling the new loadbalancer config: %w", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
log.Debugf("Writing lb config:\n%s", string(newLbConfigYaml)) |
|
|
|
|
|
|
|
if err := runtime.WriteToNode(ctx, newLbConfigYaml, k3d.DefaultLoadbalancerConfigPath, 0744, cluster.ServerLoadBalancer); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error writing new loadbalancer config to container: %w", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
command := fmt.Sprintf("SERVERS=%s %s", serverNodes, "confd -onetime -backend env && nginx -s reload") |
|
|
|
command := "confd -onetime -backend file -file /etc/confd/values.yaml -log-level debug && nginx -s reload" |
|
|
|
if err := runtime.ExecInNode(ctx, loadbalancer, []string{"sh", "-c", command}); err != nil { |
|
|
|
if err := runtime.ExecInNode(ctx, cluster.ServerLoadBalancer, []string{"sh", "-c", command}); err != nil { |
|
|
|
if strings.Contains(err.Error(), "host not found in upstream") { |
|
|
|
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()) |
|
|
|
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 nil |
|
|
@ -76,7 +86,9 @@ func UpdateLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, clu |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (*types.LoadbalancerConfig, error) { |
|
|
|
func GetLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (types.LoadbalancerConfig, error) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cfg k3d.LoadbalancerConfig |
|
|
|
|
|
|
|
|
|
|
|
if cluster.ServerLoadBalancer == nil { |
|
|
|
if cluster.ServerLoadBalancer == nil { |
|
|
|
for _, node := range cluster.Nodes { |
|
|
|
for _, node := range cluster.Nodes { |
|
|
@ -84,7 +96,7 @@ func GetLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, cluste |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
cluster.ServerLoadBalancer, err = NodeGet(ctx, runtime, node) |
|
|
|
cluster.ServerLoadBalancer, err = NodeGet(ctx, runtime, node) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return cfg, err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -92,23 +104,22 @@ func GetLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, cluste |
|
|
|
|
|
|
|
|
|
|
|
reader, err := runtime.ReadFromNode(ctx, types.DefaultLoadbalancerConfigPath, cluster.ServerLoadBalancer) |
|
|
|
reader, err := runtime.ReadFromNode(ctx, types.DefaultLoadbalancerConfigPath, cluster.ServerLoadBalancer) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return &k3d.LoadbalancerConfig{}, err |
|
|
|
return cfg, err |
|
|
|
} |
|
|
|
} |
|
|
|
defer reader.Close() |
|
|
|
defer reader.Close() |
|
|
|
|
|
|
|
|
|
|
|
file, err := ioutil.ReadAll(reader) |
|
|
|
file, err := ioutil.ReadAll(reader) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return cfg, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
file = bytes.Trim(file[512:], "\x00") // trim control characters, etc.
|
|
|
|
file = bytes.Trim(file[512:], "\x00") // trim control characters, etc.
|
|
|
|
|
|
|
|
|
|
|
|
currentConfig := &types.LoadbalancerConfig{} |
|
|
|
if err := yaml.Unmarshal(file, &cfg); err != nil { |
|
|
|
if err := yaml.Unmarshal(file, currentConfig); err != nil { |
|
|
|
return cfg, err |
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return currentConfig, nil |
|
|
|
return cfg, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func LoadbalancerGenerateConfig(cluster *k3d.Cluster) (k3d.LoadbalancerConfig, error) { |
|
|
|
func LoadbalancerGenerateConfig(cluster *k3d.Cluster) (k3d.LoadbalancerConfig, error) { |
|
|
|