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:
parent
7ba71ad66c
commit
630788f1e7
@ -343,6 +343,10 @@ func NewCmdClusterCreate() *cobra.Command {
|
||||
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 */
|
||||
|
||||
// done
|
||||
|
@ -495,6 +495,7 @@ ClusterCreatOpts:
|
||||
// *** ServerLoadBalancer ***
|
||||
if !clusterCreateOpts.DisableLoadBalancer {
|
||||
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})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare loadbalancer: %w", err)
|
||||
|
@ -27,14 +27,17 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/go-test/deep"
|
||||
"github.com/imdario/mergo"
|
||||
l "github.com/rancher/k3d/v4/pkg/logger"
|
||||
"github.com/rancher/k3d/v4/pkg/runtimes"
|
||||
"github.com/rancher/k3d/v4/pkg/types"
|
||||
k3d "github.com/rancher/k3d/v4/pkg/types"
|
||||
"github.com/spf13/viper"
|
||||
"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
|
||||
lbNode := &k3d.Node{
|
||||
Name: fmt.Sprintf("%s-%s-serverlb", k3d.DefaultObjectNamePrefix, cluster.Name),
|
||||
|
@ -111,8 +111,12 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
|
||||
|
||||
if !simpleConfig.Options.K3dOptions.DisableLoadbalancer {
|
||||
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
|
||||
newCluster.ServerLoadBalancer.Node, err = client.LoadbalancerPrepare(ctx, runtime, &newCluster, nil)
|
||||
newCluster.ServerLoadBalancer.Node, err = client.LoadbalancerPrepare(ctx, runtime, &newCluster, lbCreateOpts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing the loadbalancer: %w", err)
|
||||
}
|
||||
|
@ -102,12 +102,17 @@ type SimpleConfigOptionsRuntime struct {
|
||||
}
|
||||
|
||||
type SimpleConfigOptionsK3d struct {
|
||||
Wait bool `mapstructure:"wait" yaml:"wait"`
|
||||
Timeout time.Duration `mapstructure:"timeout" yaml:"timeout"`
|
||||
DisableLoadbalancer bool `mapstructure:"disableLoadbalancer" yaml:"disableLoadbalancer"`
|
||||
DisableImageVolume bool `mapstructure:"disableImageVolume" yaml:"disableImageVolume"`
|
||||
NoRollback bool `mapstructure:"disableRollback" yaml:"disableRollback"`
|
||||
NodeHookActions []k3d.NodeHookAction `mapstructure:"nodeHookActions" yaml:"nodeHookActions,omitempty"`
|
||||
Wait bool `mapstructure:"wait" yaml:"wait"`
|
||||
Timeout time.Duration `mapstructure:"timeout" yaml:"timeout"`
|
||||
DisableLoadbalancer bool `mapstructure:"disableLoadbalancer" yaml:"disableLoadbalancer"`
|
||||
DisableImageVolume bool `mapstructure:"disableImageVolume" yaml:"disableImageVolume"`
|
||||
NoRollback bool `mapstructure:"disableRollback" yaml:"disableRollback"`
|
||||
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 {
|
||||
|
@ -73,7 +73,8 @@ type LoadbalancerConfig struct {
|
||||
}
|
||||
|
||||
type LoadBalancerSettings struct {
|
||||
WorkerProcesses int `yaml:"workerProcesses"`
|
||||
WorkerProcesses int `yaml:"workerProcesses"`
|
||||
DefaultProxyTimeout int `yaml:"defaultProxyTimeout,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
@ -82,7 +83,8 @@ const (
|
||||
)
|
||||
|
||||
type LoadbalancerCreateOpts struct {
|
||||
Labels map[string]string
|
||||
Labels map[string]string
|
||||
ConfigOverrides []string
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -33,7 +33,7 @@ stream {
|
||||
server {
|
||||
listen {{ $port }} {{- if (eq $protocol "udp") }} udp{{- end -}};
|
||||
proxy_pass {{ $upstream }};
|
||||
proxy_timeout 600;
|
||||
proxy_timeout {{ getv "/settings/defaultProxyTimeout" "600" }};
|
||||
proxy_connect_timeout 2s;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user