From 4b553cd245913395f2d8669083f62877ee9b1277 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 29 Jan 2019 10:31:09 -0800 Subject: [PATCH] pkg/types/defaults/installconfig: Set defaults for null replicas We don't currently support configuring zero workers [1], largely because some key operators still do not tolerate masters. Still, some users are attempting to work around our checks by leaving 'replicas' unset (which ends up as nil in Go) [2]. This commit adjusts our install-config defaulting to fill in the default replica counts when the user provides machine-pool entries but leaves replicas unset. [1]: https://github.com/openshift/installer/pull/958 [2]: https://bugzilla.redhat.com/show_bug.cgi?id=1670005#c1 --- pkg/types/defaults/installconfig.go | 34 ++++++++++++++++++------ pkg/types/defaults/installconfig_test.go | 6 ++++- pkg/types/machinepools.go | 3 +-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/pkg/types/defaults/installconfig.go b/pkg/types/defaults/installconfig.go index a249eed11e8..e36864903b3 100644 --- a/pkg/types/defaults/installconfig.go +++ b/pkg/types/defaults/installconfig.go @@ -9,6 +9,7 @@ import ( libvirtdefaults "github.com/openshift/installer/pkg/types/libvirt/defaults" nonedefaults "github.com/openshift/installer/pkg/types/none/defaults" openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults" + "k8s.io/utils/pointer" ) var ( @@ -43,23 +44,40 @@ func SetInstallConfigDefaults(c *types.InstallConfig) { }, } } + numberOfMasters := int64(3) + numberOfWorkers := int64(3) + if c.Platform.Libvirt != nil { + numberOfMasters = 1 + numberOfWorkers = 1 + } if len(c.Machines) == 0 { - numberOfMasters := int64(3) - numberOfWorkers := int64(3) - if c.Platform.Libvirt != nil { - numberOfMasters = 1 - numberOfWorkers = 1 - } c.Machines = []types.MachinePool{ { Name: "master", - Replicas: func(x int64) *int64 { return &x }(numberOfMasters), + Replicas: &numberOfMasters, }, { Name: "worker", - Replicas: func(x int64) *int64 { return &x }(numberOfWorkers), + Replicas: &numberOfWorkers, }, } + } else { + for i := range c.Machines { + switch c.Machines[i].Name { + case "master": + if c.Machines[i].Replicas == nil { + c.Machines[i].Replicas = &numberOfMasters + } + case "worker": + if c.Machines[i].Replicas == nil { + c.Machines[i].Replicas = &numberOfWorkers + } + default: + if c.Machines[i].Replicas == nil { + c.Machines[i].Replicas = pointer.Int64Ptr(0) + } + } + } } switch { case c.Platform.AWS != nil: diff --git a/pkg/types/defaults/installconfig_test.go b/pkg/types/defaults/installconfig_test.go index 2a4216cdd84..bfb1caeeb07 100644 --- a/pkg/types/defaults/installconfig_test.go +++ b/pkg/types/defaults/installconfig_test.go @@ -16,6 +16,7 @@ import ( nonedefaults "github.com/openshift/installer/pkg/types/none/defaults" "github.com/openshift/installer/pkg/types/openstack" openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults" + "k8s.io/utils/pointer" ) func defaultInstallConfig() *types.InstallConfig { @@ -192,7 +193,10 @@ func TestSetInstallConfigDefaults(t *testing.T) { }, expected: func() *types.InstallConfig { c := defaultInstallConfig() - c.Machines = []types.MachinePool{{Name: "test-machine"}} + c.Machines = []types.MachinePool{{ + Name: "test-machine", + Replicas: pointer.Int64Ptr(0), + }} return c }(), }, diff --git a/pkg/types/machinepools.go b/pkg/types/machinepools.go index 89d5a985464..595fa0d691f 100644 --- a/pkg/types/machinepools.go +++ b/pkg/types/machinepools.go @@ -12,8 +12,7 @@ type MachinePool struct { Name string `json:"name"` // Replicas is the count of machines for this machine pool. - // Default is 1. - Replicas *int64 `json:"replicas"` + Replicas *int64 `json:"replicas,omitempty"` // Platform is configuration for machine pool specific to the platfrom. Platform MachinePoolPlatform `json:"platform"`