diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index 4905f692..27225ed9 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -507,15 +507,40 @@ ClusterCreatOpts: */ // *** ServerLoadBalancer *** if !clusterCreateOpts.DisableLoadBalancer { - node, nodeCreateOpts, err := LoadbalancerPrepare(ctx, runtime, cluster, &k3d.LoadbalancerCreateOpts{Labels: clusterCreateOpts.GlobalLabels}) + lbNode, err := LoadbalancerPrepare(ctx, runtime, cluster, &k3d.LoadbalancerCreateOpts{Labels: clusterCreateOpts.GlobalLabels}) if err != nil { return err } - if err := NodeCreate(ctx, runtime, node, *nodeCreateOpts); err != nil { - log.Errorln("Failed to create loadbalancer") + cluster.Nodes = append(cluster.Nodes, lbNode) // append lbNode to list of cluster nodes, so it will be considered during rollback + + lbConfig, err := LoadbalancerGenerateConfig(cluster) + if err != nil { + return fmt.Errorf("error generating loadbalancer config: %v", err) + } + + // prepare to write config to lb container + configyaml, err := yaml.Marshal(lbConfig) + if err != nil { return err } - log.Debugf("Created loadbalancer '%s'", node.Name) + + writeLbConfigAction := k3d.NodeHook{ + Stage: k3d.LifecycleStagePreStart, + Action: actions.WriteFileAction{ + Runtime: runtime, + Dest: k3d.DefaultLoadbalancerConfigPath, + Mode: 0744, + Content: configyaml, + }, + } + + lbNode.HookActions = append(lbNode.HookActions, writeLbConfigAction) + + log.Infof("Creating LoadBalancer '%s'", lbNode.Name) + if err := NodeCreate(ctx, runtime, lbNode, k3d.NodeCreateOpts{}); err != nil { + return fmt.Errorf("error creating loadbalancer: %v", err) + } + log.Debugf("Created loadbalancer '%s'", lbNode.Name) return err } @@ -865,7 +890,9 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust log.Infoln("Starting helpers...") failedHelpers := 0 for _, helperNode := range aux { - nodeStartOpts := k3d.NodeStartOpts{} + nodeStartOpts := k3d.NodeStartOpts{ + NodeHooks: helperNode.HookActions, + } if helperNode.Role == k3d.LoadBalancerRole { nodeStartOpts.Wait = true } diff --git a/pkg/client/loadbalancer.go b/pkg/client/loadbalancer.go index db47c494..2a1debcc 100644 --- a/pkg/client/loadbalancer.go +++ b/pkg/client/loadbalancer.go @@ -29,7 +29,6 @@ import ( "strings" "github.com/docker/go-connections/nat" - "github.com/rancher/k3d/v4/pkg/actions" "github.com/rancher/k3d/v4/pkg/runtimes" "github.com/rancher/k3d/v4/pkg/types" k3d "github.com/rancher/k3d/v4/pkg/types" @@ -112,8 +111,7 @@ func GetLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, cluste return currentConfig, nil } -func LoadbalancerPrepare(ctx context.Context, runtime runtimes.Runtime, cluster *types.Cluster, opts *k3d.LoadbalancerCreateOpts) (*k3d.Node, *k3d.NodeCreateOpts, error) { - +func LoadbalancerGenerateConfig(cluster *k3d.Cluster) (k3d.LoadbalancerConfig, error) { lbConfig := k3d.LoadbalancerConfig{ Ports: map[string][]string{}, Settings: k3d.LoadBalancerSettings{}, @@ -136,6 +134,14 @@ func LoadbalancerPrepare(ctx context.Context, runtime runtimes.Runtime, cluster lbConfig.Ports[fmt.Sprintf("%s.%s", exposedPort.Port(), exposedPort.Proto())] = servers } + // some additional nginx settings + lbConfig.Settings.WorkerProcesses = k3d.DefaultLoadbalancerWorkerProcesses + len(cluster.ServerLoadBalancer.Ports)*len(servers) + + return lbConfig, nil +} + +func LoadbalancerPrepare(ctx context.Context, runtime runtimes.Runtime, cluster *types.Cluster, opts *k3d.LoadbalancerCreateOpts) (*k3d.Node, error) { + if cluster.ServerLoadBalancer.Ports == nil { cluster.ServerLoadBalancer.Ports = nat.PortMap{} } @@ -151,28 +157,7 @@ func LoadbalancerPrepare(ctx context.Context, runtime runtimes.Runtime, cluster Networks: []string{cluster.Network.Name}, Restart: true, } - cluster.Nodes = append(cluster.Nodes, lbNode) // append lbNode to list of cluster nodes, so it will be considered during rollback - log.Infof("Creating LoadBalancer '%s'", lbNode.Name) - // some additional nginx settings - lbConfig.Settings.WorkerProcesses = k3d.DefaultLoadbalancerWorkerProcesses + len(cluster.ServerLoadBalancer.Ports)*len(servers) - - // prepare to write config to lb container - configyaml, err := yaml.Marshal(lbConfig) - if err != nil { - return nil, nil, err - } - - writeLbConfigAction := k3d.NodeHook{ - Stage: k3d.LifecycleStagePreStart, - Action: actions.WriteFileAction{ - Runtime: runtime, - Dest: k3d.DefaultLoadbalancerConfigPath, - Mode: 0744, - Content: configyaml, - }, - } - - return lbNode, &k3d.NodeCreateOpts{NodeHooks: []k3d.NodeHook{writeLbConfigAction}}, nil + return lbNode, nil } diff --git a/pkg/types/types.go b/pkg/types/types.go index d47e1cd7..296d1a84 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -334,6 +334,7 @@ type Node struct { Memory string // filled automatically State NodeState // filled automatically IP NodeIP // filled automatically + HookActions []NodeHook `yaml:"hooks" json:"hooks,omitempty"` } // ServerOpts describes some additional server role specific opts @@ -437,7 +438,7 @@ type LoadBalancerSettings struct { } const ( - DefaultLoadbalancerConfigPath = "/etc/confd/portmap.yaml" + DefaultLoadbalancerConfigPath = "/etc/confd/values.yaml" DefaultLoadbalancerWorkerProcesses = 1024 )