Skip to content

Commit e9b6948

Browse files
lion7Unix4ever
andcommitted
fix: nil check replicas ptr before de-referencing
This change does a nil check on the int32 ptr specifying the number of replica's inside the TalosControlPlane spec. Signed-off-by: Gerard de Leeuw <[email protected]> Co-authored-by: Artem Chernyshev <[email protected]>
1 parent b10e2e7 commit e9b6948

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

api/v1alpha3/taloscontrolplane_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ type TalosControlPlaneSpec struct {
6262
RolloutStrategy *RolloutStrategy `json:"rolloutStrategy,omitempty"`
6363
}
6464

65+
// GetReplicas reads spec replicas in a safe way.
66+
// If replicas is nil it will return 0.
67+
func (s *TalosControlPlaneSpec) GetReplicas() int32 {
68+
if s.Replicas == nil {
69+
return 0
70+
}
71+
72+
return *s.Replicas
73+
}
74+
6575
// RolloutStrategy describes how to replace existing machines
6676
// with new ones.
6777
type RolloutStrategy struct {

controllers/scale.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ import (
2424

2525
func (r *TalosControlPlaneReconciler) scaleUpControlPlane(ctx context.Context, cluster *clusterv1.Cluster, tcp *controlplanev1.TalosControlPlane, controlPlane *ControlPlane) (ctrl.Result, error) {
2626
numMachines := len(controlPlane.Machines)
27-
desiredReplicas := 0
28-
29-
if tcp.Spec.Replicas != nil {
30-
desiredReplicas = int(*tcp.Spec.Replicas)
31-
}
27+
desiredReplicas := tcp.Spec.GetReplicas()
3228

3329
conditions.MarkFalse(tcp, controlplanev1.ResizedCondition, controlplanev1.ScalingUpReason, clusterv1.ConditionSeverityWarning,
3430
"Scaling up control plane to %d replicas (actual %d)",
@@ -48,11 +44,7 @@ func (r *TalosControlPlaneReconciler) scaleDownControlPlane(
4844
machinesRequireUpgrade collections.Machines) (ctrl.Result, error) {
4945

5046
numMachines := len(controlPlane.Machines)
51-
desiredReplicas := 0
52-
53-
if tcp.Spec.Replicas != nil {
54-
desiredReplicas = int(*tcp.Spec.Replicas)
55-
}
47+
desiredReplicas := tcp.Spec.GetReplicas()
5648

5749
conditions.MarkFalse(tcp, controlplanev1.ResizedCondition, controlplanev1.ScalingDownReason, clusterv1.ConditionSeverityWarning,
5850
"Scaling down control plane to %d replicas (actual %d)",

controllers/taloscontrolplane_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ func (r *TalosControlPlaneReconciler) reconcileMachines(ctx context.Context, clu
726726

727727
// If we've made it this far, we can assume that all ownedMachines are up to date
728728
numMachines := len(machines.Items)
729-
desiredReplicas := int(*tcp.Spec.Replicas)
729+
desiredReplicas := int(tcp.Spec.GetReplicas())
730730

731731
controlPlane, err := newControlPlane(ctx, r.Client, cluster, tcp, collections.FromMachineList(machines))
732732
if err != nil {

controllers/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *TalosControlPlaneReconciler) upgradeControlPlane(
3232
switch tcp.Spec.RolloutStrategy.Type {
3333
case controlplanev1.RollingUpdateStrategyType:
3434
// RolloutStrategy is currently defaulted and validated to be RollingUpdate
35-
maxNodes := *tcp.Spec.Replicas + int32(tcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntValue())
35+
maxNodes := tcp.Spec.GetReplicas() + int32(tcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntValue())
3636
if int32(controlPlane.Machines.Len()) < maxNodes {
3737
// scaleUp ensures that we don't continue scaling up while waiting for Machines to have NodeRefs
3838
return r.scaleUpControlPlane(ctx, cluster, tcp, controlPlane)

0 commit comments

Comments
 (0)