Skip to content

Commit 7ee0cd4

Browse files
authored
Merge pull request #8132 from wm775825/main
🐛 bugfix function aggregateFromMachinesToKCP
2 parents 2db955c + e24d273 commit 7ee0cd4

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

controlplane/kubeadm/internal/workload_cluster_conditions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ func aggregateFromMachinesToKCP(input aggregateFromMachinesToKCPInput) {
559559
}
560560

561561
// In case of no errors, no warning, and at least one machine with info, report false, info.
562-
if len(kcpMachinesWithWarnings) > 0 {
563-
conditions.MarkFalse(input.controlPlane.KCP, input.condition, input.unhealthyReason, clusterv1.ConditionSeverityWarning, "Following machines are reporting %s info: %s", input.note, strings.Join(sets.List(kcpMachinesWithInfo), ", "))
562+
if len(kcpMachinesWithInfo) > 0 {
563+
conditions.MarkFalse(input.controlPlane.KCP, input.condition, input.unhealthyReason, clusterv1.ConditionSeverityInfo, "Following machines are reporting %s info: %s", input.note, strings.Join(sets.List(kcpMachinesWithInfo), ", "))
564564
return
565565
}
566566

controlplane/kubeadm/internal/workload_cluster_conditions_test.go

+95-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package internal
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
@@ -492,7 +493,7 @@ func TestUpdateEtcdConditions(t *testing.T) {
492493

493494
func TestUpdateStaticPodConditions(t *testing.T) {
494495
n1APIServerPodName := staticPodName("kube-apiserver", "n1")
495-
n1APIServerPodkey := client.ObjectKey{
496+
n1APIServerPodKey := client.ObjectKey{
496497
Namespace: metav1.NamespaceSystem,
497498
Name: n1APIServerPodName,
498499
}.String()
@@ -623,7 +624,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
623624
Items: []corev1.Node{*fakeNode("n1")},
624625
},
625626
get: map[string]interface{}{
626-
n1APIServerPodkey: fakePod(n1APIServerPodName,
627+
n1APIServerPodKey: fakePod(n1APIServerPodName,
627628
withPhase(corev1.PodRunning),
628629
withCondition(corev1.PodReady, corev1.ConditionTrue),
629630
),
@@ -659,7 +660,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
659660
Items: []corev1.Node{*fakeNode("n1")},
660661
},
661662
get: map[string]interface{}{
662-
n1APIServerPodkey: fakePod(n1APIServerPodName,
663+
n1APIServerPodKey: fakePod(n1APIServerPodName,
663664
withPhase(corev1.PodRunning),
664665
withCondition(corev1.PodReady, corev1.ConditionTrue),
665666
),
@@ -708,7 +709,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
708709
Items: []corev1.Node{*fakeNode("n1")},
709710
},
710711
get: map[string]interface{}{
711-
n1APIServerPodkey: fakePod(n1APIServerPodName,
712+
n1APIServerPodKey: fakePod(n1APIServerPodName,
712713
withPhase(corev1.PodRunning),
713714
withCondition(corev1.PodReady, corev1.ConditionTrue),
714715
),
@@ -1032,6 +1033,16 @@ func withNodeRef(ref string) fakeMachineOption {
10321033
}
10331034
}
10341035

1036+
func withMachineReadyCondition(status corev1.ConditionStatus, severity clusterv1.ConditionSeverity) fakeMachineOption {
1037+
return func(machine *clusterv1.Machine) {
1038+
machine.Status.Conditions = append(machine.Status.Conditions, clusterv1.Condition{
1039+
Type: clusterv1.MachinesReadyCondition,
1040+
Status: status,
1041+
Severity: severity,
1042+
})
1043+
}
1044+
}
1045+
10351046
type fakePodOption func(*corev1.Pod)
10361047

10371048
func fakePod(name string, options ...fakePodOption) *corev1.Pod {
@@ -1068,3 +1079,83 @@ func withCondition(condition corev1.PodConditionType, status corev1.ConditionSta
10681079
pod.Status.Conditions = append(pod.Status.Conditions, c)
10691080
}
10701081
}
1082+
1083+
func TestAggregateFromMachinesToKCP(t *testing.T) {
1084+
conditionType := controlplanev1.ControlPlaneComponentsHealthyCondition
1085+
unhealthyReason := "unhealthy reason"
1086+
unknownReason := "unknown reason"
1087+
note := "some notes"
1088+
1089+
tests := []struct {
1090+
name string
1091+
machines []*clusterv1.Machine
1092+
kcpErrors []string
1093+
expectedCondition clusterv1.Condition
1094+
}{
1095+
{
1096+
name: "kcp machines with errors",
1097+
machines: []*clusterv1.Machine{
1098+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionFalse, clusterv1.ConditionSeverityError)),
1099+
},
1100+
expectedCondition: *conditions.FalseCondition(conditionType, unhealthyReason, clusterv1.ConditionSeverityError, fmt.Sprintf("Following machines are reporting %s errors: %s", note, "m1")),
1101+
},
1102+
{
1103+
name: "input kcp errors",
1104+
machines: []*clusterv1.Machine{
1105+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionTrue, clusterv1.ConditionSeverityNone)),
1106+
},
1107+
kcpErrors: []string{"something error"},
1108+
expectedCondition: *conditions.FalseCondition(conditionType, unhealthyReason, clusterv1.ConditionSeverityError, "something error"),
1109+
},
1110+
{
1111+
name: "kcp machines with warnings",
1112+
machines: []*clusterv1.Machine{
1113+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionFalse, clusterv1.ConditionSeverityWarning)),
1114+
},
1115+
expectedCondition: *conditions.FalseCondition(conditionType, unhealthyReason, clusterv1.ConditionSeverityWarning, fmt.Sprintf("Following machines are reporting %s warnings: %s", note, "m1")),
1116+
},
1117+
{
1118+
name: "kcp machines with info",
1119+
machines: []*clusterv1.Machine{
1120+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionFalse, clusterv1.ConditionSeverityInfo)),
1121+
},
1122+
expectedCondition: *conditions.FalseCondition(conditionType, unhealthyReason, clusterv1.ConditionSeverityInfo, fmt.Sprintf("Following machines are reporting %s info: %s", note, "m1")),
1123+
},
1124+
{
1125+
name: "kcp machines with true",
1126+
machines: []*clusterv1.Machine{
1127+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionTrue, clusterv1.ConditionSeverityNone)),
1128+
},
1129+
expectedCondition: *conditions.TrueCondition(conditionType),
1130+
},
1131+
{
1132+
name: "kcp machines with unknown",
1133+
machines: []*clusterv1.Machine{
1134+
fakeMachine("m1", withMachineReadyCondition(corev1.ConditionUnknown, clusterv1.ConditionSeverityNone)),
1135+
},
1136+
expectedCondition: *conditions.UnknownCondition(conditionType, unknownReason, fmt.Sprintf("Following machines are reporting unknown %s status: %s", note, "m1")),
1137+
},
1138+
}
1139+
1140+
for _, tt := range tests {
1141+
t.Run(tt.name, func(t *testing.T) {
1142+
g := NewWithT(t)
1143+
1144+
input := aggregateFromMachinesToKCPInput{
1145+
controlPlane: &ControlPlane{
1146+
KCP: &controlplanev1.KubeadmControlPlane{},
1147+
Machines: collections.FromMachines(tt.machines...),
1148+
},
1149+
machineConditions: []clusterv1.ConditionType{clusterv1.MachinesReadyCondition},
1150+
kcpErrors: tt.kcpErrors,
1151+
condition: conditionType,
1152+
unhealthyReason: unhealthyReason,
1153+
unknownReason: unknownReason,
1154+
note: note,
1155+
}
1156+
aggregateFromMachinesToKCP(input)
1157+
1158+
g.Expect(*conditions.Get(input.controlPlane.KCP, conditionType)).To(conditions.MatchCondition(tt.expectedCondition))
1159+
})
1160+
}
1161+
}

0 commit comments

Comments
 (0)