Skip to content

Commit

Permalink
Improve v1beta2 condition ordering
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Nov 13, 2024
1 parent 7702621 commit bb52574
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
97 changes: 77 additions & 20 deletions util/conditions/v1beta2/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

var orderMap = map[string]int{}

func init() {
for i, c := range order {
orderMap[c] = i
}
}

// defaultSortLessFunc returns true if a condition is less than another with regards to the
// order of conditions designed for convenience of the consumer, i.e. kubectl get.
// According to this order the Available and the Ready condition always goes first, Deleting and Paused always goes last,
// and all the other conditions are sorted by Type.
func defaultSortLessFunc(i, j metav1.Condition) bool {
fi, oki := first[i.Type]
fj, okj := first[j.Type]
fi, oki := orderMap[i.Type]
fj, okj := orderMap[j.Type]
switch {
case oki && !okj:
return true
Expand All @@ -38,26 +46,75 @@ func defaultSortLessFunc(i, j metav1.Condition) bool {
return fi < fj
}

li, oki := last[i.Type]
lj, okj := last[j.Type]
switch {
case oki && !okj:
return false
case !oki && okj:
return true
case oki && okj:
return li < lj
}

return i.Type < j.Type
}

var first = map[string]int{
clusterv1.AvailableV1Beta2Condition: 0,
clusterv1.ReadyV1Beta2Condition: 1,
// The order array below leads to the following condition ordering:
//
// | Condition | Cluster | KCP | MD | MS | MP | Machine |
// |--------------------------------|---------|-----|:---|----|----|---------|
// | -- Availability conditions -- | | | | | | |
// | Available | x | x | x | | x | x |
// | Ready | | | | | | x |
// | RemoteConnectionProbe | x | | | | | |
// | BootstrapConfigReady | | | | | x | x |
// | InfrastructureReady | x | | | | x | x |
// | ControlPlaneInitialized | x | | | | | |
// | ControlPlaneAvailable | x | | | | | |
// | WorkersAvailable | x | | | | | |
// | CertificatesAvailable | | x | | | | |
// | Initialized | | x | | | | |
// | EtcdClusterHealthy | | x | | | | |
// | ControlPlaneComponentsHealthy | | x | | | | |
// | NodeHealthy | | | | | | x |
// | NodeReady | | | | | | x |
// | HealthCheckSucceeded | | | | | | x |
// | OwnerRemediated | | | | | | x |
// | -- Operations -- | | | | | | |
// | TopologyReconciled | x | | | | | |
// | Remediating | x | x | x | x | x | |
// | ScalingDown | x | x | x | x | x | |
// | ScalingUp | x | x | x | x | x | |
// | UpToDate | | | | | | x |
// | -- Aggregated from Machines -- | | | | | | |
// | MachinesReady | x | x | x | x | x | |
// | MachinesUpToDate | x | x | x | x | x | |
// | -- Misc -- | | | | | | |
// | Paused | x | x | x | x | x | x |
// | Deleting | x | x | x | x | x | x |
// .
var order = []string{
clusterv1.AvailableV1Beta2Condition,
clusterv1.ReadyV1Beta2Condition,
clusterv1.ClusterRemoteConnectionProbeV1Beta2Condition,
clusterv1.BootstrapConfigReadyV1Beta2Condition,
clusterv1.InfrastructureReadyV1Beta2Condition,
clusterv1.ClusterControlPlaneInitializedV1Beta2Condition,
clusterv1.ClusterControlPlaneAvailableV1Beta2Condition,
clusterv1.ClusterWorkersAvailableV1Beta2Condition,
kubeadmControlPlaneCertificatesAvailableV1Beta2Condition,
kubeadmControlPlaneInitializedV1Beta2Condition,
kubeadmControlPlaneEtcdClusterHealthyV1Beta2Condition,
kubeadmControlPlaneControlPlaneComponentsHealthyV1Beta2Condition,
clusterv1.MachineNodeHealthyV1Beta2Condition,
clusterv1.MachineNodeReadyV1Beta2Condition,
clusterv1.MachineHealthCheckSucceededV1Beta2Condition,
clusterv1.MachineOwnerRemediatedV1Beta2Condition,
clusterv1.ClusterTopologyReconciledV1Beta2Condition,
clusterv1.RemediatingV1Beta2Condition,
clusterv1.ScalingDownV1Beta2Condition,
clusterv1.ScalingUpV1Beta2Condition,
clusterv1.MachineUpToDateV1Beta2Condition,
clusterv1.MachinesReadyV1Beta2Condition,
clusterv1.MachinesUpToDateV1Beta2Condition,
clusterv1.PausedV1Beta2Condition,
clusterv1.DeletingV1Beta2Condition,
}

var last = map[string]int{
clusterv1.PausedV1Beta2Condition: 0,
clusterv1.DeletingV1Beta2Condition: 1,
}
// Constants inlined for ordering (we want to avoid importing the KCP API package).
const (
kubeadmControlPlaneCertificatesAvailableV1Beta2Condition = "CertificatesAvailable"
kubeadmControlPlaneInitializedV1Beta2Condition = "Initialized"
kubeadmControlPlaneEtcdClusterHealthyV1Beta2Condition = "EtcdClusterHealthy"
kubeadmControlPlaneControlPlaneComponentsHealthyV1Beta2Condition = "ControlPlaneComponentsHealthy"
)
4 changes: 2 additions & 2 deletions util/conditions/v1beta2/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func TestDefaultSortLessFunc(t *testing.T) {
g.Expect(conditions).To(Equal([]metav1.Condition{
{Type: clusterv1.AvailableV1Beta2Condition},
{Type: clusterv1.ReadyV1Beta2Condition},
{Type: clusterv1.PausedV1Beta2Condition},
{Type: clusterv1.DeletingV1Beta2Condition},
{Type: "A"},
{Type: "B"},
{Type: "C!"},
{Type: clusterv1.PausedV1Beta2Condition},
{Type: clusterv1.DeletingV1Beta2Condition},
}))
}

0 comments on commit bb52574

Please # to comment.