@@ -17,6 +17,7 @@ limitations under the License.
17
17
package internal
18
18
19
19
import (
20
+ "fmt"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
@@ -492,7 +493,7 @@ func TestUpdateEtcdConditions(t *testing.T) {
492
493
493
494
func TestUpdateStaticPodConditions (t * testing.T ) {
494
495
n1APIServerPodName := staticPodName ("kube-apiserver" , "n1" )
495
- n1APIServerPodkey := client.ObjectKey {
496
+ n1APIServerPodKey := client.ObjectKey {
496
497
Namespace : metav1 .NamespaceSystem ,
497
498
Name : n1APIServerPodName ,
498
499
}.String ()
@@ -623,7 +624,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
623
624
Items : []corev1.Node {* fakeNode ("n1" )},
624
625
},
625
626
get : map [string ]interface {}{
626
- n1APIServerPodkey : fakePod (n1APIServerPodName ,
627
+ n1APIServerPodKey : fakePod (n1APIServerPodName ,
627
628
withPhase (corev1 .PodRunning ),
628
629
withCondition (corev1 .PodReady , corev1 .ConditionTrue ),
629
630
),
@@ -659,7 +660,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
659
660
Items : []corev1.Node {* fakeNode ("n1" )},
660
661
},
661
662
get : map [string ]interface {}{
662
- n1APIServerPodkey : fakePod (n1APIServerPodName ,
663
+ n1APIServerPodKey : fakePod (n1APIServerPodName ,
663
664
withPhase (corev1 .PodRunning ),
664
665
withCondition (corev1 .PodReady , corev1 .ConditionTrue ),
665
666
),
@@ -708,7 +709,7 @@ func TestUpdateStaticPodConditions(t *testing.T) {
708
709
Items : []corev1.Node {* fakeNode ("n1" )},
709
710
},
710
711
get : map [string ]interface {}{
711
- n1APIServerPodkey : fakePod (n1APIServerPodName ,
712
+ n1APIServerPodKey : fakePod (n1APIServerPodName ,
712
713
withPhase (corev1 .PodRunning ),
713
714
withCondition (corev1 .PodReady , corev1 .ConditionTrue ),
714
715
),
@@ -1032,6 +1033,16 @@ func withNodeRef(ref string) fakeMachineOption {
1032
1033
}
1033
1034
}
1034
1035
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
+
1035
1046
type fakePodOption func (* corev1.Pod )
1036
1047
1037
1048
func fakePod (name string , options ... fakePodOption ) * corev1.Pod {
@@ -1068,3 +1079,83 @@ func withCondition(condition corev1.PodConditionType, status corev1.ConditionSta
1068
1079
pod .Status .Conditions = append (pod .Status .Conditions , c )
1069
1080
}
1070
1081
}
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