clusterCreate: add --lb-config-override flag

- allow overriding k3d-proxy settings (workerProcesses,
defaultProxyTimeout)
- add new field to loadbalancer config and SimpleConfig structs
This commit is contained in:
iwilltry42 2021-09-07 08:12:53 +02:00 committed by Thorsten Klein
parent 7ba71ad66c
commit 630788f1e7
7 changed files with 45 additions and 10 deletions

View File

@ -343,6 +343,10 @@ func NewCmdClusterCreate() *cobra.Command {
l.Log().Fatalln("Failed to mark flag 'config' as filename flag") l.Log().Fatalln("Failed to mark flag 'config' as filename flag")
} }
/* Loadbalancer / Proxy */
cmd.Flags().StringSlice("lb-config-override", nil, "Use dotted YAML path syntax to override nginx loadbalancer settings")
_ = cfgViper.BindPFlag("options.k3d.loadbalancer.configoverrides", cmd.Flags().Lookup("lb-config-override"))
/* Subcommands */ /* Subcommands */
// done // done

View File

@ -495,6 +495,7 @@ ClusterCreatOpts:
// *** ServerLoadBalancer *** // *** ServerLoadBalancer ***
if !clusterCreateOpts.DisableLoadBalancer { if !clusterCreateOpts.DisableLoadBalancer {
if cluster.ServerLoadBalancer == nil { if cluster.ServerLoadBalancer == nil {
l.Log().Infof("No loadbalancer specified, creating a default one...")
lbNode, err := LoadbalancerPrepare(ctx, runtime, cluster, &k3d.LoadbalancerCreateOpts{Labels: clusterCreateOpts.GlobalLabels}) lbNode, err := LoadbalancerPrepare(ctx, runtime, cluster, &k3d.LoadbalancerCreateOpts{Labels: clusterCreateOpts.GlobalLabels})
if err != nil { if err != nil {
return fmt.Errorf("failed to prepare loadbalancer: %w", err) return fmt.Errorf("failed to prepare loadbalancer: %w", err)

View File

@ -27,14 +27,17 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"time" "time"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/go-test/deep" "github.com/go-test/deep"
"github.com/imdario/mergo"
l "github.com/rancher/k3d/v4/pkg/logger" l "github.com/rancher/k3d/v4/pkg/logger"
"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"
"github.com/spf13/viper"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -192,6 +195,22 @@ func LoadbalancerPrepare(ctx context.Context, runtime runtimes.Runtime, cluster
} }
} }
if opts != nil && opts.ConfigOverrides != nil && len(opts.ConfigOverrides) > 0 {
tmpViper := viper.New()
for _, override := range opts.ConfigOverrides {
kv := strings.SplitN(override, "=", 2)
l.Log().Tracef("Overriding LB config with %s...", kv)
tmpViper.Set(kv[0], kv[1])
}
lbConfigOverride := &k3d.LoadbalancerConfig{}
if err := tmpViper.Unmarshal(lbConfigOverride); err != nil {
return nil, fmt.Errorf("failed to unmarshal loadbalancer config override into loadbalancer config: %w", err)
}
if err := mergo.MergeWithOverwrite(cluster.ServerLoadBalancer.Config, lbConfigOverride); err != nil {
return nil, fmt.Errorf("failed to override loadbalancer config: %w", err)
}
}
// Create LB as a modified node with loadbalancerRole // Create LB as a modified node with loadbalancerRole
lbNode := &k3d.Node{ lbNode := &k3d.Node{
Name: fmt.Sprintf("%s-%s-serverlb", k3d.DefaultObjectNamePrefix, cluster.Name), Name: fmt.Sprintf("%s-%s-serverlb", k3d.DefaultObjectNamePrefix, cluster.Name),

View File

@ -111,8 +111,12 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
if !simpleConfig.Options.K3dOptions.DisableLoadbalancer { if !simpleConfig.Options.K3dOptions.DisableLoadbalancer {
newCluster.ServerLoadBalancer = k3d.NewLoadbalancer() newCluster.ServerLoadBalancer = k3d.NewLoadbalancer()
lbCreateOpts := &k3d.LoadbalancerCreateOpts{}
if simpleConfig.Options.K3dOptions.Loadbalancer.ConfigOverrides != nil && len(simpleConfig.Options.K3dOptions.Loadbalancer.ConfigOverrides) > 0 {
lbCreateOpts.ConfigOverrides = simpleConfig.Options.K3dOptions.Loadbalancer.ConfigOverrides
}
var err error var err error
newCluster.ServerLoadBalancer.Node, err = client.LoadbalancerPrepare(ctx, runtime, &newCluster, nil) newCluster.ServerLoadBalancer.Node, err = client.LoadbalancerPrepare(ctx, runtime, &newCluster, lbCreateOpts)
if err != nil { if err != nil {
return nil, fmt.Errorf("error preparing the loadbalancer: %w", err) return nil, fmt.Errorf("error preparing the loadbalancer: %w", err)
} }

View File

@ -108,6 +108,11 @@ type SimpleConfigOptionsK3d struct {
DisableImageVolume bool `mapstructure:"disableImageVolume" yaml:"disableImageVolume"` DisableImageVolume bool `mapstructure:"disableImageVolume" yaml:"disableImageVolume"`
NoRollback bool `mapstructure:"disableRollback" yaml:"disableRollback"` NoRollback bool `mapstructure:"disableRollback" yaml:"disableRollback"`
NodeHookActions []k3d.NodeHookAction `mapstructure:"nodeHookActions" yaml:"nodeHookActions,omitempty"` NodeHookActions []k3d.NodeHookAction `mapstructure:"nodeHookActions" yaml:"nodeHookActions,omitempty"`
Loadbalancer SimpleConfigOptionsK3dLoadbalancer `mapstructure:"loadbalancer" yaml:"loadbalancer,omitempty"`
}
type SimpleConfigOptionsK3dLoadbalancer struct {
ConfigOverrides []string `mapstructure:"configOverrides" yaml:"configOverrides,omitempty"`
} }
type SimpleConfigOptionsK3s struct { type SimpleConfigOptionsK3s struct {

View File

@ -74,6 +74,7 @@ type LoadbalancerConfig struct {
type LoadBalancerSettings struct { type LoadBalancerSettings struct {
WorkerProcesses int `yaml:"workerProcesses"` WorkerProcesses int `yaml:"workerProcesses"`
DefaultProxyTimeout int `yaml:"defaultProxyTimeout,omitempty"`
} }
const ( const (
@ -83,6 +84,7 @@ const (
type LoadbalancerCreateOpts struct { type LoadbalancerCreateOpts struct {
Labels map[string]string Labels map[string]string
ConfigOverrides []string
} }
/* /*

View File

@ -33,7 +33,7 @@ stream {
server { server {
listen {{ $port }} {{- if (eq $protocol "udp") }} udp{{- end -}}; listen {{ $port }} {{- if (eq $protocol "udp") }} udp{{- end -}};
proxy_pass {{ $upstream }}; proxy_pass {{ $upstream }};
proxy_timeout 600; proxy_timeout {{ getv "/settings/defaultProxyTimeout" "600" }};
proxy_connect_timeout 2s; proxy_connect_timeout 2s;
} }