Skip to content

Commit

Permalink
include machines in deleting status when calculate machineset replicas
Browse files Browse the repository at this point in the history
  • Loading branch information
jzhoucliqr committed Aug 3, 2020
1 parent 95fe9e2 commit adcf5fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
5 changes: 3 additions & 2 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (r *MachineSetReconciler) reconcile(ctx context.Context, cluster *clusterv1

var errs []error
for _, machine := range filteredMachines {
if conditions.IsFalse(machine, clusterv1.MachineOwnerRemediatedCondition) {
if machine.DeletionTimestamp.IsZero() && conditions.IsFalse(machine, clusterv1.MachineOwnerRemediatedCondition) {
logger.Info("Deleting unhealthy machine", "machine", machine.GetName())
patch := client.MergeFrom(machine.DeepCopy())
if err := r.Client.Delete(ctx, machine); err != nil {
Expand Down Expand Up @@ -443,7 +443,8 @@ func shouldExcludeMachine(machineSet *clusterv1.MachineSet, machine *clusterv1.M
logger.V(4).Info("Machine is not controlled by machineset", "machine", machine.Name)
return true
}
return !machine.ObjectMeta.DeletionTimestamp.IsZero()

return false
}

// adoptOrphan sets the MachineSet as a controller OwnerReference to the Machine.
Expand Down
2 changes: 1 addition & 1 deletion controllers/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func TestShouldExcludeMachine(t *testing.T) {
},
},
},
expected: true,
expected: false,
},
}

Expand Down
43 changes: 24 additions & 19 deletions controllers/mdutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,21 @@ func GetActualReplicaCountForMachineSets(machineSets []*clusterv1.MachineSet) in
return totalActualReplicas
}

// GetTotalReplicaCountForMachineSets returns sum of max(ms.Spec.Replicas, ms.Status.Replicas) across all the machine sets.
// This is used to guarantee that the total number of machines will not exceed md.Spec.Replicas + maxSurge
// Use max(spec.Replicas,status.Replicas) to cover the cases that:
// 1. Scale up, where spec.Replicas increased but no machine created yet, so spec.Replicas > status.Replicas
// 2. Scale down, where spec.Replicas decreased but machine not deleted yet, so spec.Replicas < status.Replicas
func GetTotalReplicaCountForMachineSets(machineSets []*clusterv1.MachineSet) int32 {
totalReplicas := int32(0)
for _, ms := range machineSets {
if ms != nil {
totalReplicas += max(*(ms.Spec.Replicas), ms.Status.Replicas)
}
}
return totalReplicas
}

// GetReadyReplicaCountForMachineSets returns the number of ready machines corresponding to the given machine sets.
func GetReadyReplicaCountForMachineSets(machineSets []*clusterv1.MachineSet) int32 {
totalReadyReplicas := int32(0)
Expand Down Expand Up @@ -521,7 +536,7 @@ func NewMSNewReplicas(deployment *clusterv1.MachineDeployment, allMSs []*cluster
return 0, err
}
// Find the total number of machines
currentMachineCount := GetReplicaCountForMachineSets(allMSs)
currentMachineCount := GetTotalReplicaCountForMachineSets(allMSs)
maxTotalMachines := *(deployment.Spec.Replicas) + int32(maxSurge)
if currentMachineCount >= maxTotalMachines {
// Cannot scale up.
Expand All @@ -533,24 +548,7 @@ func NewMSNewReplicas(deployment *clusterv1.MachineDeployment, allMSs []*cluster
scaleUpCount = integer.Int32Min(scaleUpCount, *(deployment.Spec.Replicas)-*(newMS.Spec.Replicas))
return *(newMS.Spec.Replicas) + scaleUpCount, nil
default:
// Check if we can scale up.
maxSurge, err := intstrutil.GetValueFromIntOrPercent(deployment.Spec.Strategy.RollingUpdate.MaxSurge, int(*(deployment.Spec.Replicas)), true)
if err != nil {
return 0, err
}
// Find the total number of machines
currentMachineCount := GetReplicaCountForMachineSets(allMSs)
maxTotalMachines := *(deployment.Spec.Replicas) + int32(maxSurge)
if currentMachineCount >= maxTotalMachines {
// Cannot scale up.
return *(newMS.Spec.Replicas), nil
}
// Scale up.
scaleUpCount := maxTotalMachines - currentMachineCount
// Do not exceed the number of desired replicas.
scaleUpCount = integer.Int32Min(scaleUpCount, *(deployment.Spec.Replicas)-*(newMS.Spec.Replicas))
return *(newMS.Spec.Replicas) + scaleUpCount, nil
// -- return 0, errors.Errorf("deployment type %v isn't supported", deployment.Spec.Strategy.Type)
return 0, fmt.Errorf("deployment type %v isn't supported", deployment.Spec.Strategy.Type)
}
}

Expand Down Expand Up @@ -698,3 +696,10 @@ func ComputeHash(template *clusterv1.MachineTemplateSpec) uint32 {
DeepHashObject(machineTemplateSpecHasher, *template)
return machineTemplateSpecHasher.Sum32()
}

func max(a, b int32) int32 {
if a > b {
return a
}
return b
}
10 changes: 7 additions & 3 deletions controllers/mdutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,29 @@ func TestGetReplicaCountForMachineSets(t *testing.T) {
*(ms1.Spec.Replicas) = 1
ms1.Status.Replicas = 2
ms2 := generateMS(generateDeployment("bar"))
*(ms2.Spec.Replicas) = 2
*(ms2.Spec.Replicas) = 5
ms2.Status.Replicas = 3

tests := []struct {
Name string
Sets []*clusterv1.MachineSet
ExpectedCount int32
ExpectedActual int32
ExpectedTotal int32
}{
{
Name: "1:2 Replicas",
Sets: []*clusterv1.MachineSet{&ms1},
ExpectedCount: 1,
ExpectedActual: 2,
ExpectedTotal: 2,
},
{
Name: "3:5 Replicas",
Name: "6:5 Replicas",
Sets: []*clusterv1.MachineSet{&ms1, &ms2},
ExpectedCount: 3,
ExpectedCount: 6,
ExpectedActual: 5,
ExpectedTotal: 7,
},
}

Expand All @@ -440,6 +443,7 @@ func TestGetReplicaCountForMachineSets(t *testing.T) {

g.Expect(GetReplicaCountForMachineSets(test.Sets)).To(Equal(test.ExpectedCount))
g.Expect(GetActualReplicaCountForMachineSets(test.Sets)).To(Equal(test.ExpectedActual))
g.Expect(GetTotalReplicaCountForMachineSets(test.Sets)).To(Equal(test.ExpectedTotal))
})
}
}
Expand Down

0 comments on commit adcf5fa

Please sign in to comment.