Skip to content

Commit cf5979e

Browse files
authored
Merge pull request #11330 from fabriziopandini/improve-machine-ready-v1beta2-condition
🌱 Improve machine Ready v1beta2 condition
2 parents 508beae + 879e278 commit cf5979e

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed

internal/controllers/machine/machine_controller_status.go

+78-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func setBootstrapReadyCondition(_ context.Context, machine *clusterv1.Machine, b
9494
v1beta2conditions.FallbackCondition{
9595
Status: v1beta2conditions.BoolToStatus(machine.Status.BootstrapReady),
9696
Reason: clusterv1.MachineBootstrapConfigReadyNoReasonReportedV1Beta2Reason,
97-
Message: fmt.Sprintf("%s status.ready is %t", machine.Spec.Bootstrap.ConfigRef.Kind, machine.Status.BootstrapReady),
97+
Message: bootstrapConfigReadyFallBackMessage(machine.Spec.Bootstrap.ConfigRef.Kind, machine.Status.BootstrapReady),
9898
},
9999
); err != nil {
100100
v1beta2conditions.Set(machine, metav1.Condition{
@@ -142,6 +142,10 @@ func setBootstrapReadyCondition(_ context.Context, machine *clusterv1.Machine, b
142142
})
143143
}
144144

145+
func bootstrapConfigReadyFallBackMessage(kind string, ready bool) string {
146+
return fmt.Sprintf("%s status.ready is %t", kind, ready)
147+
}
148+
145149
func setInfrastructureReadyCondition(_ context.Context, machine *clusterv1.Machine, infraMachine *unstructured.Unstructured, infraMachineIsNotFound bool) {
146150
if infraMachine != nil {
147151
if err := v1beta2conditions.SetMirrorConditionFromUnstructured(
@@ -150,7 +154,7 @@ func setInfrastructureReadyCondition(_ context.Context, machine *clusterv1.Machi
150154
v1beta2conditions.FallbackCondition{
151155
Status: v1beta2conditions.BoolToStatus(machine.Status.InfrastructureReady),
152156
Reason: clusterv1.MachineInfrastructureReadyNoReasonReportedV1Beta2Reason,
153-
Message: fmt.Sprintf("%s status.ready is %t", machine.Spec.InfrastructureRef.Kind, machine.Status.InfrastructureReady),
157+
Message: infrastructureReadyFallBackMessage(machine.Spec.InfrastructureRef.Kind, machine.Status.InfrastructureReady),
154158
},
155159
); err != nil {
156160
v1beta2conditions.Set(machine, metav1.Condition{
@@ -220,6 +224,10 @@ func setInfrastructureReadyCondition(_ context.Context, machine *clusterv1.Machi
220224
})
221225
}
222226

227+
func infrastructureReadyFallBackMessage(kind string, ready bool) string {
228+
return fmt.Sprintf("%s status.ready is %t", kind, ready)
229+
}
230+
223231
func setNodeHealthyAndReadyConditions(ctx context.Context, machine *clusterv1.Machine, node *corev1.Node, nodeGetErr error, lastProbeSuccessTime time.Time, remoteConditionsGracePeriod time.Duration) {
224232
if time.Since(lastProbeSuccessTime) > remoteConditionsGracePeriod {
225233
var msg string
@@ -591,11 +599,27 @@ func setReadyCondition(ctx context.Context, machine *clusterv1.Machine) {
591599
},
592600
}
593601

602+
// Add overrides for conditions we don't to surface in the Ready condition with slightly different messages,
603+
// mostly to improve when we will aggregate the Ready condition from many machines on MS, MD etc.
604+
var overrideConditions v1beta2conditions.OverrideConditions
594605
if !machine.DeletionTimestamp.IsZero() {
595-
summaryOpts = append(summaryOpts, v1beta2conditions.OverrideConditions{calculateDeletingConditionForSummary(machine)})
606+
overrideConditions = append(overrideConditions, calculateDeletingConditionForSummary(machine))
607+
}
608+
609+
if infrastructureReadyCondition := calculateInfrastructureReadyForSummary(machine); infrastructureReadyCondition != nil {
610+
overrideConditions = append(overrideConditions, *infrastructureReadyCondition)
611+
}
612+
613+
if bootstrapReadyCondition := calculateBootstrapConfigReadyForSummary(machine); bootstrapReadyCondition != nil {
614+
overrideConditions = append(overrideConditions, *bootstrapReadyCondition)
615+
}
616+
617+
if len(overrideConditions) > 0 {
618+
summaryOpts = append(summaryOpts, overrideConditions)
596619
}
597620

598621
readyCondition, err := v1beta2conditions.NewSummaryCondition(machine, clusterv1.MachineReadyV1Beta2Condition, summaryOpts...)
622+
599623
if err != nil {
600624
// Note, this could only happen if we hit edge cases in computing the summary, which should not happen due to the fact
601625
// that we are passing a non empty list of ForConditionTypes.
@@ -654,6 +678,57 @@ func calculateDeletingConditionForSummary(machine *clusterv1.Machine) v1beta2con
654678
}
655679
}
656680

681+
func calculateInfrastructureReadyForSummary(machine *clusterv1.Machine) *v1beta2conditions.ConditionWithOwnerInfo {
682+
infrastructureReadyCondition := v1beta2conditions.Get(machine, clusterv1.MachineInfrastructureReadyV1Beta2Condition)
683+
684+
if infrastructureReadyCondition == nil {
685+
return nil
686+
}
687+
688+
message := infrastructureReadyCondition.Message
689+
if infrastructureReadyCondition.Status == metav1.ConditionTrue && infrastructureReadyCondition.Message == infrastructureReadyFallBackMessage(machine.Spec.InfrastructureRef.Kind, machine.Status.InfrastructureReady) {
690+
message = ""
691+
}
692+
693+
return &v1beta2conditions.ConditionWithOwnerInfo{
694+
OwnerResource: v1beta2conditions.ConditionOwnerInfo{
695+
Kind: "Machine",
696+
Name: machine.Name,
697+
},
698+
Condition: metav1.Condition{
699+
Type: infrastructureReadyCondition.Type,
700+
Status: infrastructureReadyCondition.Status,
701+
Reason: infrastructureReadyCondition.Reason,
702+
Message: message,
703+
},
704+
}
705+
}
706+
707+
func calculateBootstrapConfigReadyForSummary(machine *clusterv1.Machine) *v1beta2conditions.ConditionWithOwnerInfo {
708+
bootstrapConfigReadyCondition := v1beta2conditions.Get(machine, clusterv1.MachineBootstrapConfigReadyV1Beta2Condition)
709+
if bootstrapConfigReadyCondition == nil {
710+
return nil
711+
}
712+
713+
message := bootstrapConfigReadyCondition.Message
714+
if bootstrapConfigReadyCondition.Status == metav1.ConditionTrue && machine.Spec.Bootstrap.ConfigRef != nil && bootstrapConfigReadyCondition.Message == bootstrapConfigReadyFallBackMessage(machine.Spec.Bootstrap.ConfigRef.Kind, machine.Status.BootstrapReady) {
715+
message = ""
716+
}
717+
718+
return &v1beta2conditions.ConditionWithOwnerInfo{
719+
OwnerResource: v1beta2conditions.ConditionOwnerInfo{
720+
Kind: "Machine",
721+
Name: machine.Name,
722+
},
723+
Condition: metav1.Condition{
724+
Type: bootstrapConfigReadyCondition.Type,
725+
Status: bootstrapConfigReadyCondition.Status,
726+
Reason: bootstrapConfigReadyCondition.Reason,
727+
Message: message,
728+
},
729+
}
730+
}
731+
657732
func setAvailableCondition(ctx context.Context, machine *clusterv1.Machine) {
658733
log := ctrl.LoggerFrom(ctx)
659734
readyCondition := v1beta2conditions.Get(machine, clusterv1.MachineReadyV1Beta2Condition)

internal/controllers/machine/machine_controller_status_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,60 @@ func TestSetReadyCondition(t *testing.T) {
11241124
Message: "Deleting: Machine deletion in progress, stage: WaitingForPreDrainHook",
11251125
},
11261126
},
1127+
{
1128+
name: "Drops messages from BootstrapConfigReady and Infrastructure ready when using fallback to fields",
1129+
machine: &clusterv1.Machine{
1130+
ObjectMeta: metav1.ObjectMeta{
1131+
Name: "machine-test",
1132+
Namespace: metav1.NamespaceDefault,
1133+
},
1134+
Spec: clusterv1.MachineSpec{
1135+
Bootstrap: clusterv1.Bootstrap{
1136+
ConfigRef: &corev1.ObjectReference{
1137+
Kind: "KubeadmConfig",
1138+
},
1139+
},
1140+
InfrastructureRef: corev1.ObjectReference{
1141+
Kind: "AWSMachine",
1142+
},
1143+
},
1144+
Status: clusterv1.MachineStatus{
1145+
InfrastructureReady: true,
1146+
BootstrapReady: true,
1147+
V1Beta2: &clusterv1.MachineV1Beta2Status{
1148+
Conditions: []metav1.Condition{
1149+
{
1150+
Type: clusterv1.MachineBootstrapConfigReadyV1Beta2Condition,
1151+
Status: metav1.ConditionTrue,
1152+
Reason: "Foo",
1153+
Message: bootstrapConfigReadyFallBackMessage("KubeadmConfig", true),
1154+
},
1155+
{
1156+
Type: clusterv1.InfrastructureReadyV1Beta2Condition,
1157+
Status: metav1.ConditionTrue,
1158+
Reason: "Bar",
1159+
Message: infrastructureReadyFallBackMessage("AWSMachine", true),
1160+
},
1161+
{
1162+
Type: clusterv1.MachineNodeHealthyV1Beta2Condition,
1163+
Status: metav1.ConditionTrue,
1164+
Reason: "AllGood",
1165+
},
1166+
{
1167+
Type: clusterv1.MachineDeletingV1Beta2Condition,
1168+
Status: metav1.ConditionFalse,
1169+
Reason: clusterv1.MachineDeletingDeletionTimestampNotSetV1Beta2Reason,
1170+
},
1171+
},
1172+
},
1173+
},
1174+
},
1175+
expectCondition: metav1.Condition{
1176+
Type: clusterv1.MachineReadyV1Beta2Condition,
1177+
Status: metav1.ConditionTrue,
1178+
Reason: v1beta2conditions.MultipleInfoReportedReason,
1179+
},
1180+
},
11271181
{
11281182
name: "Aggregates Ready condition correctly while the machine is deleting (waiting for Node drain)",
11291183
machine: &clusterv1.Machine{

0 commit comments

Comments
 (0)