[FIX] clusterCreate: process simpleConfig before validation step to avoid early exit in hostnetwork mode (#860)

pull/826/head
Thorsten Klein 3 years ago committed by GitHub
parent 0f5d11282b
commit 925dec492b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      cmd/cluster/clusterCreate.go
  2. 15
      pkg/config/process.go

@ -140,17 +140,15 @@ func NewCmdClusterCreate() *cobra.Command {
simpleCfg.Name = args[0]
}
clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg)
if err != nil {
l.Log().Fatalln(err)
if err := config.ProcessSimpleConfig(&simpleCfg); err != nil {
l.Log().Fatalf("error processing/sanitizing simple config: %v", err)
}
l.Log().Debugf("===== Merged Cluster Config =====\n%+v\n===== ===== =====\n", clusterConfig)
clusterConfig, err = config.ProcessClusterConfig(*clusterConfig)
clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg)
if err != nil {
l.Log().Fatalln(err)
}
l.Log().Debugf("===== Processed Cluster Config =====\n%+v\n===== ===== =====\n", clusterConfig)
l.Log().Debugf("===== Merged Cluster Config =====\n%+v\n===== ===== =====\n", clusterConfig)
if err := config.ValidateClusterConfig(cmd.Context(), runtimes.SelectedRuntime, *clusterConfig); err != nil {
l.Log().Fatalln("Failed Cluster Configuration Validation: ", err)

@ -25,14 +25,27 @@ package config
import (
conf "github.com/rancher/k3d/v5/pkg/config/v1alpha3"
l "github.com/rancher/k3d/v5/pkg/logger"
k3d "github.com/rancher/k3d/v5/pkg/types"
)
// ProcessSimpleConfig applies processing to the simple config, sanitizing it and doing some modifications
func ProcessSimpleConfig(simpleConfig *conf.SimpleConfig) error {
if simpleConfig.Network == "host" {
l.Log().Infoln("[SimpleConfig] Hostnetwork selected - disabling injection of docker host into the cluster, server load balancer and setting the api port to the k3s default")
simpleConfig.Options.K3dOptions.DisableLoadbalancer = true
l.Log().Debugf("Host network was chosen, changing provided/random api port to k3s:%s", k3d.DefaultAPIPort)
simpleConfig.ExposeAPI.HostPort = k3d.DefaultAPIPort
}
return nil
}
// ProcessClusterConfig applies processing to the config sanitizing it and doing
// some final modifications
func ProcessClusterConfig(clusterConfig conf.ClusterConfig) (*conf.ClusterConfig, error) {
cluster := clusterConfig.Cluster
if cluster.Network.Name == "host" {
l.Log().Infoln("Hostnetwork selected - disabling injection of docker host into the cluster, server load balancer and setting the api port to the k3s default")
l.Log().Infoln("[ClusterConfig] Hostnetwork selected - disabling injection of docker host into the cluster, server load balancer and setting the api port to the k3s default")
// if network is set to host, exposed api port must be the one imposed by k3s
k3sPort := cluster.KubeAPI.Port.Port()
l.Log().Debugf("Host network was chosen, changing provided/random api port to k3s:%s", k3sPort)

Loading…
Cancel
Save