Skip to content

Commit 059a51a

Browse files
authored
Merge pull request #5016 from muraee/fix-rosa-update-config
🐛 ROSA: fix ROSAMachinePool.Spec.UpdateConfig causing unnecessary update calls
2 parents be020fd + 599eb74 commit 059a51a

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

exp/api/v1beta2/rosamachinepool_webhook.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"github.com/google/go-cmp/cmp"
66
"github.com/pkg/errors"
77
apierrors "k8s.io/apimachinery/pkg/api/errors"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
runtime "k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/util/intstr"
911
"k8s.io/apimachinery/pkg/util/validation/field"
12+
"k8s.io/utils/ptr"
1013
ctrl "sigs.k8s.io/controller-runtime"
1114
"sigs.k8s.io/controller-runtime/pkg/webhook"
1215
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -127,4 +130,17 @@ func validateImmutable(old, updated interface{}, name string) field.ErrorList {
127130

128131
// Default implements admission.Defaulter.
129132
func (r *ROSAMachinePool) Default() {
133+
if r.Spec.NodeDrainGracePeriod == nil {
134+
r.Spec.NodeDrainGracePeriod = &metav1.Duration{}
135+
}
136+
137+
if r.Spec.UpdateConfig == nil {
138+
r.Spec.UpdateConfig = &RosaUpdateConfig{}
139+
}
140+
if r.Spec.UpdateConfig.RollingUpdate == nil {
141+
r.Spec.UpdateConfig.RollingUpdate = &RollingUpdate{
142+
MaxUnavailable: ptr.To(intstr.FromInt32(0)),
143+
MaxSurge: ptr.To(intstr.FromInt32(1)),
144+
}
145+
}
130146
}

exp/controllers/rosamachinepool_controller.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,12 @@ func (r *ROSAMachinePoolReconciler) reconcileMachinePoolVersion(machinePoolScope
357357
}
358358

359359
func (r *ROSAMachinePoolReconciler) updateNodePool(machinePoolScope *scope.RosaMachinePoolScope, ocmClient *ocm.Client, nodePool *cmv1.NodePool) (*cmv1.NodePool, error) {
360-
desiredSpec := *machinePoolScope.RosaMachinePool.Spec.DeepCopy()
361-
currentSpec := nodePoolToRosaMachinePoolSpec(nodePool)
360+
machinePool := machinePoolScope.RosaMachinePool.DeepCopy()
361+
// default all fields before comparing, so that nil/unset fields don't cause an unnecessary update call.
362+
machinePool.Default()
362363

363-
if desiredSpec.NodeDrainGracePeriod == nil {
364-
// currentSpec.NodeDrainGracePeriod is always non-nil.
365-
// if desiredSpec.NodeDrainGracePeriod is nil, set to 0 so we update the nodePool, otherewise the current value will be preserved.
366-
desiredSpec.NodeDrainGracePeriod = &metav1.Duration{}
367-
}
364+
desiredSpec := machinePool.Spec
365+
currentSpec := nodePoolToRosaMachinePoolSpec(nodePool)
368366

369367
ignoredFields := []string{
370368
"ProviderIDList", // providerIDList is set by the controller.
@@ -481,12 +479,12 @@ func nodePoolBuilder(rosaMachinePoolSpec expinfrav1.RosaMachinePoolSpec, machine
481479
if rosaMachinePoolSpec.UpdateConfig != nil {
482480
configMgmtBuilder := cmv1.NewNodePoolManagementUpgrade()
483481

484-
if rosaMachinePoolSpec.UpdateConfig.RollingUpdate != nil {
485-
if rosaMachinePoolSpec.UpdateConfig.RollingUpdate.MaxSurge != nil {
486-
configMgmtBuilder = configMgmtBuilder.MaxSurge(rosaMachinePoolSpec.UpdateConfig.RollingUpdate.MaxSurge.String())
482+
if rollingUpdate := rosaMachinePoolSpec.UpdateConfig.RollingUpdate; rollingUpdate != nil {
483+
if rollingUpdate.MaxSurge != nil {
484+
configMgmtBuilder = configMgmtBuilder.MaxSurge(rollingUpdate.MaxSurge.String())
487485
}
488-
if rosaMachinePoolSpec.UpdateConfig.RollingUpdate.MaxUnavailable != nil {
489-
configMgmtBuilder = configMgmtBuilder.MaxUnavailable(rosaMachinePoolSpec.UpdateConfig.RollingUpdate.MaxUnavailable.String())
486+
if rollingUpdate.MaxUnavailable != nil {
487+
configMgmtBuilder = configMgmtBuilder.MaxUnavailable(rollingUpdate.MaxUnavailable.String())
490488
}
491489
}
492490

@@ -542,7 +540,7 @@ func nodePoolToRosaMachinePoolSpec(nodePool *cmv1.NodePool) expinfrav1.RosaMachi
542540
spec.UpdateConfig.RollingUpdate.MaxSurge = ptr.To(intstr.Parse(nodePool.ManagementUpgrade().MaxSurge()))
543541
}
544542
if nodePool.ManagementUpgrade().MaxUnavailable() != "" {
545-
spec.UpdateConfig.RollingUpdate.MaxSurge = ptr.To(intstr.Parse(nodePool.ManagementUpgrade().MaxUnavailable()))
543+
spec.UpdateConfig.RollingUpdate.MaxUnavailable = ptr.To(intstr.Parse(nodePool.ManagementUpgrade().MaxUnavailable()))
546544
}
547545
}
548546

exp/controllers/rosamachinepool_controller_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/google/go-cmp/cmp/cmpopts"
78
. "github.com/onsi/gomega"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/apimachinery/pkg/util/intstr"
@@ -28,7 +29,8 @@ func TestNodePoolToRosaMachinePoolSpec(t *testing.T) {
2829
},
2930
UpdateConfig: &expinfrav1.RosaUpdateConfig{
3031
RollingUpdate: &expinfrav1.RollingUpdate{
31-
MaxSurge: &intstr.IntOrString{IntVal: 3},
32+
MaxSurge: ptr.To(intstr.FromInt32(3)),
33+
MaxUnavailable: ptr.To(intstr.FromInt32(5)),
3234
},
3335
},
3436
}
@@ -44,5 +46,5 @@ func TestNodePoolToRosaMachinePoolSpec(t *testing.T) {
4446

4547
expectedSpec := nodePoolToRosaMachinePoolSpec(nodePoolSpec)
4648

47-
g.Expect(expectedSpec).To(Equal(rosaMachinePoolSpec))
49+
g.Expect(rosaMachinePoolSpec).To(BeComparableTo(expectedSpec, cmpopts.EquateEmpty()))
4850
}

0 commit comments

Comments
 (0)