From d10c98c9dfc9b46164a52dc3b86bcb067eade134 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Wed, 13 Nov 2024 19:56:40 +0100 Subject: [PATCH] Improve v1beta2 condition ordering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- util/conditions/v1beta2/sort.go | 112 ++++++++++++++++++++++----- util/conditions/v1beta2/sort_test.go | 4 +- 2 files changed, 94 insertions(+), 22 deletions(-) diff --git a/util/conditions/v1beta2/sort.go b/util/conditions/v1beta2/sort.go index 0eca3158a1eb..c4ba58718e86 100644 --- a/util/conditions/v1beta2/sort.go +++ b/util/conditions/v1beta2/sort.go @@ -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 @@ -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: +// +// | 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 | +// | APIServerPodHealthy | | | | | | x | +// | ControllerManagerPodHealthy | | | | | | x | +// | SchedulerPodHealthy | | | | | | x | +// | EtcdPodHealthy | | | | | | x | +// | EtcdMemberHealthy | | | | | | 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, + kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition, + kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition, + kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition, + kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition, + kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition, + 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" + kubeadmControlPlaneMachineAPIServerPodHealthyV1Beta2Condition = "APIServerPodHealthy" + kubeadmControlPlaneMachineControllerManagerPodHealthyV1Beta2Condition = "ControllerManagerPodHealthy" + kubeadmControlPlaneMachineSchedulerPodHealthyV1Beta2Condition = "SchedulerPodHealthy" + kubeadmControlPlaneMachineEtcdPodHealthyV1Beta2Condition = "EtcdPodHealthy" + kubeadmControlPlaneMachineEtcdMemberHealthyV1Beta2Condition = "EtcdMemberHealthy" +) diff --git a/util/conditions/v1beta2/sort_test.go b/util/conditions/v1beta2/sort_test.go index 2badfcd5313e..9bc8d40e458d 100644 --- a/util/conditions/v1beta2/sort_test.go +++ b/util/conditions/v1beta2/sort_test.go @@ -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}, })) }