@@ -17,6 +17,7 @@ limitations under the License.
1717package internal
1818
1919import (
20+ "fmt"
2021 "testing"
2122
2223 . "github.com/onsi/gomega"
@@ -492,7 +493,7 @@ func TestUpdateEtcdConditions(t *testing.T) {
492493
493494func 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+
10351046type fakePodOption func (* corev1.Pod )
10361047
10371048func 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