Skip to content

Commit 1e8ed45

Browse files
committed
Improve v1beta2 condition ordering
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent 7702621 commit 1e8ed45

File tree

2 files changed

+94
-22
lines changed

2 files changed

+94
-22
lines changed

util/conditions/v1beta2/sort.go

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ import (
2222
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2323
)
2424

25+
var orderMap = map[string]int{}
26+
27+
func init() {
28+
for i, c := range order {
29+
orderMap[c] = i
30+
}
31+
}
32+
2533
// defaultSortLessFunc returns true if a condition is less than another with regards to the
2634
// order of conditions designed for convenience of the consumer, i.e. kubectl get.
2735
// According to this order the Available and the Ready condition always goes first, Deleting and Paused always goes last,
2836
// and all the other conditions are sorted by Type.
2937
func defaultSortLessFunc(i, j metav1.Condition) bool {
30-
fi, oki := first[i.Type]
31-
fj, okj := first[j.Type]
38+
fi, oki := orderMap[i.Type]
39+
fj, okj := orderMap[j.Type]
3240
switch {
3341
case oki && !okj:
3442
return true
@@ -38,26 +46,90 @@ func defaultSortLessFunc(i, j metav1.Condition) bool {
3846
return fi < fj
3947
}
4048

41-
li, oki := last[i.Type]
42-
lj, okj := last[j.Type]
43-
switch {
44-
case oki && !okj:
45-
return false
46-
case !oki && okj:
47-
return true
48-
case oki && okj:
49-
return li < lj
50-
}
51-
5249
return i.Type < j.Type
5350
}
5451

55-
var first = map[string]int{
56-
clusterv1.AvailableV1Beta2Condition: 0,
57-
clusterv1.ReadyV1Beta2Condition: 1,
52+
// The order array below leads to the following condition ordering:
53+
//
54+
// | Condition | Cluster | KCP | MD | MS | MP | Machine |
55+
// |--------------------------------|---------|-----|:---|----|----|---------|
56+
// | -- Availability conditions -- | | | | | | |
57+
// | Available | x | x | x | | x | x |
58+
// | Ready | | | | | | x |
59+
// | UpToDate | | | | | | x |
60+
// | RemoteConnectionProbe | x | | | | | |
61+
// | BootstrapConfigReady | | | | | x | x |
62+
// | InfrastructureReady | x | | | | x | x |
63+
// | ControlPlaneInitialized | x | | | | | |
64+
// | ControlPlaneAvailable | x | | | | | |
65+
// | WorkersAvailable | x | | | | | |
66+
// | CertificatesAvailable | | x | | | | |
67+
// | Initialized | | x | | | | |
68+
// | EtcdClusterHealthy | | x | | | | |
69+
// | ControlPlaneComponentsHealthy | | x | | | | |
70+
// | NodeHealthy | | | | | | x |
71+
// | NodeReady | | | | | | x |
72+
// | EtcdPodHealthy | | | | | | x |
73+
// | EtcdMemberHealthy | | | | | | x |
74+
// | APIServerPodHealthy | | | | | | x |
75+
// | ControllerManagerPodHealthy | | | | | | x |
76+
// | SchedulerPodHealthy | | | | | | x |
77+
// | HealthCheckSucceeded | | | | | | x |
78+
// | OwnerRemediated | | | | | | x |
79+
// | -- Operations -- | | | | | | |
80+
// | TopologyReconciled | x | | | | | |
81+
// | Remediating | x | x | x | x | x | |
82+
// | ScalingDown | x | x | x | x | x | |
83+
// | ScalingUp | x | x | x | x | x | |
84+
// | -- Aggregated from Machines -- | | | | | | |
85+
// | MachinesReady | x | x | x | x | x | |
86+
// | MachinesUpToDate | x | x | x | x | x | |
87+
// | -- Misc -- | | | | | | |
88+
// | Paused | x | x | x | x | x | x |
89+
// | Deleting | x | x | x | x | x | x |
90+
// .
91+
var order = []string{
92+
clusterv1.AvailableV1Beta2Condition,
93+
clusterv1.ReadyV1Beta2Condition,
94+
clusterv1.MachineUpToDateV1Beta2Condition,
95+
clusterv1.ClusterRemoteConnectionProbeV1Beta2Condition,
96+
clusterv1.BootstrapConfigReadyV1Beta2Condition,
97+
clusterv1.InfrastructureReadyV1Beta2Condition,
98+
clusterv1.ClusterControlPlaneInitializedV1Beta2Condition,
99+
clusterv1.ClusterControlPlaneAvailableV1Beta2Condition,
100+
clusterv1.ClusterWorkersAvailableV1Beta2Condition,
101+
kubeadmControlPlaneCertificatesAvailableV1Beta2Condition,
102+
kubeadmControlPlaneInitializedV1Beta2Condition,
103+
kubeadmControlPlaneEtcdClusterHealthyV1Beta2Condition,
104+
kubeadmControlPlaneControlPlaneComponentsHealthyV1Beta2Condition,
105+
clusterv1.MachineNodeHealthyV1Beta2Condition,
106+
clusterv1.MachineNodeReadyV1Beta2Condition,
107+
kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition,
108+
kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition,
109+
kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition,
110+
kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition,
111+
kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition,
112+
clusterv1.MachineHealthCheckSucceededV1Beta2Condition,
113+
clusterv1.MachineOwnerRemediatedV1Beta2Condition,
114+
clusterv1.ClusterTopologyReconciledV1Beta2Condition,
115+
clusterv1.RemediatingV1Beta2Condition,
116+
clusterv1.ScalingDownV1Beta2Condition,
117+
clusterv1.ScalingUpV1Beta2Condition,
118+
clusterv1.MachinesReadyV1Beta2Condition,
119+
clusterv1.MachinesUpToDateV1Beta2Condition,
120+
clusterv1.PausedV1Beta2Condition,
121+
clusterv1.DeletingV1Beta2Condition,
58122
}
59123

60-
var last = map[string]int{
61-
clusterv1.PausedV1Beta2Condition: 0,
62-
clusterv1.DeletingV1Beta2Condition: 1,
63-
}
124+
// Constants inlined for ordering (we want to avoid importing the KCP API package).
125+
const (
126+
kubeadmControlPlaneCertificatesAvailableV1Beta2Condition = "CertificatesAvailable"
127+
kubeadmControlPlaneInitializedV1Beta2Condition = "Initialized"
128+
kubeadmControlPlaneEtcdClusterHealthyV1Beta2Condition = "EtcdClusterHealthy"
129+
kubeadmControlPlaneControlPlaneComponentsHealthyV1Beta2Condition = "ControlPlaneComponentsHealthy"
130+
kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition = "APIServerPodHealthy"
131+
kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition = "ControllerManagerPodHealthy"
132+
kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition = "SchedulerPodHealthy"
133+
kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition = "EtcdPodHealthy"
134+
kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition = "EtcdMemberHealthy"
135+
)

util/conditions/v1beta2/sort_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ func TestDefaultSortLessFunc(t *testing.T) {
4646
g.Expect(conditions).To(Equal([]metav1.Condition{
4747
{Type: clusterv1.AvailableV1Beta2Condition},
4848
{Type: clusterv1.ReadyV1Beta2Condition},
49+
{Type: clusterv1.PausedV1Beta2Condition},
50+
{Type: clusterv1.DeletingV1Beta2Condition},
4951
{Type: "A"},
5052
{Type: "B"},
5153
{Type: "C!"},
52-
{Type: clusterv1.PausedV1Beta2Condition},
53-
{Type: clusterv1.DeletingV1Beta2Condition},
5454
}))
5555
}

0 commit comments

Comments
 (0)