Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Improve v1beta2 condition ordering #11418

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 92 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,90 @@ 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:
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
//
// | Condition | Cluster | KCP | MD | MS | MP | Machine |
// |--------------------------------|---------|-----|:---|----|----|---------|
// | -- Availability conditions -- | | | | | | |
// | Available | x | x | x | | x | x |
// | Ready | | | | | | x |
// | UpToDate | | | | | | 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 |
// | EtcdPodHealthy | | | | | | x |
// | EtcdMemberHealthy | | | | | | x |
// | APIServerPodHealthy | | | | | | x |
// | ControllerManagerPodHealthy | | | | | | x |
// | SchedulerPodHealthy | | | | | | 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 | |
// | -- 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.MachineUpToDateV1Beta2Condition,
clusterv1.ClusterRemoteConnectionProbeV1Beta2Condition,
clusterv1.BootstrapConfigReadyV1Beta2Condition,
clusterv1.InfrastructureReadyV1Beta2Condition,
clusterv1.ClusterControlPlaneInitializedV1Beta2Condition,
clusterv1.ClusterControlPlaneAvailableV1Beta2Condition,
clusterv1.ClusterWorkersAvailableV1Beta2Condition,
kubeadmControlPlaneCertificatesAvailableV1Beta2Condition,
kubeadmControlPlaneInitializedV1Beta2Condition,
kubeadmControlPlaneEtcdClusterHealthyV1Beta2Condition,
kubeadmControlPlaneControlPlaneComponentsHealthyV1Beta2Condition,
clusterv1.MachineNodeHealthyV1Beta2Condition,
clusterv1.MachineNodeReadyV1Beta2Condition,
kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition,
kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition,
kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition,
kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition,
kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition,
clusterv1.MachineHealthCheckSucceededV1Beta2Condition,
clusterv1.MachineOwnerRemediatedV1Beta2Condition,
clusterv1.ClusterTopologyReconciledV1Beta2Condition,
clusterv1.RemediatingV1Beta2Condition,
clusterv1.ScalingDownV1Beta2Condition,
clusterv1.ScalingUpV1Beta2Condition,
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"
kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition = "APIServerPodHealthy"
kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition = "ControllerManagerPodHealthy"
kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition = "SchedulerPodHealthy"
kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition = "EtcdPodHealthy"
kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition = "EtcdMemberHealthy"
)
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},
}))
}