From 3afe5d4522b2c8573a2546052f106f71e9298491 Mon Sep 17 00:00:00 2001 From: Jirka Kremser <535866+jkremser@users.noreply.github.com> Date: Tue, 31 Oct 2023 15:44:29 +0100 Subject: [PATCH] args for vsphere template cluster (#1164) Signed-off-by: Jirka Kremser --- cmd/template/cluster/flag.go | 14 ++++++++------ cmd/template/cluster/provider/capv.go | 14 ++++++++------ cmd/template/cluster/provider/common.go | 5 +++-- .../cluster/provider/templates/capv/types.go | 1 + cmd/template/cluster/runner_test.go | 3 ++- .../testdata/run_template_cluster_capv.golden | 1 + 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/template/cluster/flag.go b/cmd/template/cluster/flag.go index a7625c791..d6e88b412 100644 --- a/cmd/template/cluster/flag.go +++ b/cmd/template/cluster/flag.go @@ -94,6 +94,7 @@ const ( flagVSphereControlPlaneIP = "vsphere-control-plane-ip" flagVSphereServiceLoadBalancerCIDR = "vsphere-service-load-balancer-cidr" flagVSphereNetworkName = "vsphere-network-name" + flagVSphereSvcLbIpPool = "vsphere-service-lb-pool" flagVSphereControlPlaneDiskGiB = "vsphere-control-plane-disk-gib" flagVSphereControlPlaneIpPool = "vsphere-control-plane-ip-pool" flagVSphereControlPlaneMemoryMiB = "vsphere-control-plane-memory-mib" @@ -239,10 +240,11 @@ func (f *flag) Init(cmd *cobra.Command) { cmd.Flags().IntVar(&f.OpenStack.WorkerReplicas, flagOpenStackWorkerReplicas, 0, "Default worker node pool replicas (OpenStack only).") // VSphere only - cmd.Flags().StringVar(&f.VSphere.ControlPlane.IP, flagVSphereControlPlaneIP, "", "Control plane IP, leave empty for auto allocation.") + cmd.Flags().StringVar(&f.VSphere.ControlPlane.Ip, flagVSphereControlPlaneIP, "", "Control plane IP, leave empty for auto allocation.") cmd.Flags().StringVar(&f.VSphere.ServiceLoadBalancerCIDR, flagVSphereServiceLoadBalancerCIDR, "", "CIDR for Service LB for new cluster") cmd.Flags().StringVar(&f.VSphere.NetworkName, flagVSphereNetworkName, "", "Network name in vcenter that should be used for the new VMs") - cmd.Flags().StringVar(&f.VSphere.ControlPlane.IPPoolName, flagVSphereControlPlaneIpPool, "wc-cp-ips", "Name of `GlobalInClusterIpPool` CR from which the IP for CP is taken") + cmd.Flags().StringVar(&f.VSphere.SvcLbIpPoolName, flagVSphereSvcLbIpPool, "svc-lb-ips", "Name of `GlobalInClusterIpPool` CR from which the IP for Service LB (kubevip) is taken") + cmd.Flags().StringVar(&f.VSphere.ControlPlane.IpPoolName, flagVSphereControlPlaneIpPool, "wc-cp-ips", "Name of `GlobalInClusterIpPool` CR from which the IP for CP is taken") cmd.Flags().IntVar(&f.VSphere.ControlPlane.DiskGiB, flagVSphereControlPlaneDiskGiB, 50, "Disk size in GiB for control individual plane nodes") cmd.Flags().IntVar(&f.VSphere.ControlPlane.MemoryMiB, flagVSphereControlPlaneMemoryMiB, 8096, "Memory size in MiB for individual control plane nodes") cmd.Flags().IntVar(&f.VSphere.ControlPlane.NumCPUs, flagVSphereControlPlaneNumCPUs, 4, "Number of CPUs for individual control plane nodes") @@ -430,16 +432,16 @@ func (f *flag) Validate(cmd *cobra.Command) error { return microerror.Maskf(invalidFlagError, "--%s supports one availability zone only", flagControlPlaneAZ) } case key.ProviderVSphere: - if f.VSphere.ServiceLoadBalancerCIDR == "" { - return microerror.Maskf(invalidFlagError, "CIDR range from which the public IPs for Services of type LoadBalancer are taken (required) (--%s)", flagVSphereServiceLoadBalancerCIDR) + if f.VSphere.NetworkName == "" { + return microerror.Maskf(invalidFlagError, "Provide the network name in vcenter (required) (--%s)", flagVSphereNetworkName) } - if !validateCIDR(f.VSphere.ServiceLoadBalancerCIDR) { + if f.VSphere.ServiceLoadBalancerCIDR != "" && !validateCIDR(f.VSphere.ServiceLoadBalancerCIDR) { return microerror.Maskf(invalidFlagError, "--%s must be a valid CIDR", flagVSphereServiceLoadBalancerCIDR) } if !cmd.Flags().Changed(flagKubernetesVersion) { f.KubernetesVersion = defaultVSphereKubernetesVersion } - // todo: add validation for flagVSphereImageTemplate + placeholders := strings.Count(f.VSphere.ImageTemplate, "%s") if placeholders > 1 { return microerror.Maskf(invalidFlagError, "--%s must contain at most one occurrence of '%%s' where k8s version will be injected", flagVSphereImageTemplate) diff --git a/cmd/template/cluster/provider/capv.go b/cmd/template/cluster/provider/capv.go index df4d82130..0b745c672 100644 --- a/cmd/template/cluster/provider/capv.go +++ b/cmd/template/cluster/provider/capv.go @@ -114,7 +114,7 @@ func templateClusterVSphere(ctx context.Context, k8sClient k8sclient.Interface, func BuildCapvClusterConfig(config ClusterConfig) capv.ClusterConfig { const className = "default" - return capv.ClusterConfig{ + cfg := capv.ClusterConfig{ BaseDomain: "test.gigantic.io", ClusterDescription: config.Description, Organization: config.Organization, @@ -126,14 +126,12 @@ func BuildCapvClusterConfig(config ClusterConfig) capv.ClusterConfig { Network: &capv.Network{ AllowAllEgress: true, ControlPlaneEndpoint: &capv.ControlPlaneEndpoint{ - Host: config.VSphere.ControlPlane.IP, - IpPoolName: config.VSphere.ControlPlane.IPPoolName, + Host: config.VSphere.ControlPlane.Ip, + IpPoolName: config.VSphere.ControlPlane.IpPoolName, Port: 6443, }, LoadBalancers: &capv.LoadBalancers{ - CidrBlocks: []string{ - config.VSphere.ServiceLoadBalancerCIDR, - }, + IpPoolName: config.VSphere.SvcLbIpPoolName, }, }, }, @@ -165,6 +163,10 @@ func BuildCapvClusterConfig(config ClusterConfig) capv.ClusterConfig { }, }, } + if config.VSphere.ServiceLoadBalancerCIDR != "" { + cfg.Connectivity.Network.LoadBalancers.CidrBlocks = []string{config.VSphere.ServiceLoadBalancerCIDR} + } + return cfg } func getMachineTemplate(machineTemplate *VSphereMachineTemplate, clusterConfig *ClusterConfig) *capv.MachineTemplate { diff --git a/cmd/template/cluster/provider/common.go b/cmd/template/cluster/provider/common.go index 94ab4219c..29687c038 100644 --- a/cmd/template/cluster/provider/common.go +++ b/cmd/template/cluster/provider/common.go @@ -70,6 +70,7 @@ type VSphereConfig struct { Worker VSphereMachineTemplate ResourcePool string ServiceLoadBalancerCIDR string + SvcLbIpPoolName string } type VSphereMachineTemplate struct { @@ -80,8 +81,8 @@ type VSphereMachineTemplate struct { } type VSphereControlPlane struct { - IP string - IPPoolName string + Ip string + IpPoolName string VSphereMachineTemplate } diff --git a/cmd/template/cluster/provider/templates/capv/types.go b/cmd/template/cluster/provider/templates/capv/types.go index f26b74dac..919a80fd7 100644 --- a/cmd/template/cluster/provider/templates/capv/types.go +++ b/cmd/template/cluster/provider/templates/capv/types.go @@ -40,6 +40,7 @@ type ControlPlaneEndpoint struct { type LoadBalancers struct { CidrBlocks []string `json:"cidrBlocks,omitempty"` + IpPoolName string `json:"ipPoolName,omitempty"` } type ControlPlane struct { diff --git a/cmd/template/cluster/runner_test.go b/cmd/template/cluster/runner_test.go index 62b9b711a..b9bba9254 100644 --- a/cmd/template/cluster/runner_test.go +++ b/cmd/template/cluster/runner_test.go @@ -219,6 +219,7 @@ func Test_run(t *testing.T) { ServiceLoadBalancerCIDR: "1.2.3.4/32", ResourcePool: "foopool", NetworkName: "foonet", + SvcLbIpPoolName: "svc-foo-pool", CredentialsSecretName: "foosecret", ImageTemplate: "foobar", ControlPlane: provider.VSphereControlPlane{ @@ -228,7 +229,7 @@ func Test_run(t *testing.T) { NumCPUs: 6, Replicas: 5, }, - IPPoolName: "foo-pool", + IpPoolName: "foo-pool", }, Worker: provider.VSphereMachineTemplate{ DiskGiB: 43, diff --git a/cmd/template/cluster/testdata/run_template_cluster_capv.golden b/cmd/template/cluster/testdata/run_template_cluster_capv.golden index d2387d7b8..70680b28e 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capv.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capv.golden @@ -16,6 +16,7 @@ data: loadBalancers: cidrBlocks: - 1.2.3.4/32 + ipPoolName: svc-foo-pool controlPlane: image: repository: registry.k8s.io