]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: Adding sort-by support for ceph orch ps
authorRedouane Kachach <rkachach@redhat.com>
Thu, 29 Jun 2023 10:02:21 +0000 (12:02 +0200)
committerAdam King <adking@redhat.com>
Fri, 6 Oct 2023 19:23:52 +0000 (15:23 -0400)
Fixes: https://tracker.ceph.com/issues/61856
Signed-off-by: Redouane Kachach <rkachach@redhat.com>
(cherry picked from commit b347106af329e238a927f47436acd54999c4d9af)

src/pybind/mgr/orchestrator/module.py

index a4ca7704f041a9a8055ca2cc4887b0f3b43a9fc5..db680ab842972ad2e3fa75389b9566d9aed3f8d9 100644 (file)
@@ -134,6 +134,16 @@ class HostDetails:
 yaml.add_representer(HostDetails, HostDetails.yaml_representer)
 
 
+class DaemonFields(enum.Enum):
+    name = 'name'
+    host = 'host'
+    status = 'status'
+    refreshed = 'refreshed'
+    age = 'age'
+    mem_use = 'mem_use'
+    mem_lim = 'mem_lim'
+    image = 'image'
+
 class ServiceType(enum.Enum):
     mon = 'mon'
     mgr = 'mgr'
@@ -814,6 +824,7 @@ class OrchestratorCli(OrchestratorClientMixin, MgrModule,
                       service_name: Optional[str] = None,
                       daemon_type: Optional[str] = None,
                       daemon_id: Optional[str] = None,
+                      sort_by: Optional[DaemonFields] = DaemonFields.name,
                       format: Format = Format.plain,
                       refresh: bool = False) -> HandleCommandResult:
         """
@@ -829,6 +840,27 @@ class OrchestratorCli(OrchestratorClientMixin, MgrModule,
 
         def ukn(s: Optional[str]) -> str:
             return '<unknown>' if s is None else s
+
+        def sort_by_field(d: DaemonDescription) -> Optional[str]:
+            if sort_by == DaemonFields.name:
+                return d.name()
+            elif sort_by == DaemonFields.host:
+                return d.hostname
+            elif sort_by == DaemonFields.status:
+                return d.status
+            elif sort_by == DaemonFields.refreshed:
+                return d.last_refresh
+            elif sort_by == DaemonFields.age:
+                return d.created
+            elif sort_by == DaemonFields.mem_use:
+                return d.memory_usage
+            elif sort_by == DaemonFields.mem_lim:
+                return d.memory_request
+            elif sort_by == DaemonFields.image:
+                return d.container_image_id
+            else:
+                return None
+
         # Sort the list for display
         daemons.sort(key=lambda s: (ukn(s.daemon_type), ukn(s.hostname), ukn(s.daemon_id)))
 
@@ -852,7 +884,7 @@ class OrchestratorCli(OrchestratorClientMixin, MgrModule,
             table._align['MEM LIM'] = 'r'
             table.left_padding_width = 0
             table.right_padding_width = 2
-            for s in natsorted(daemons, key=lambda d: d.name()):
+            for s in natsorted(daemons, key=lambda d: sort_by_field(d)):
                 if s.status_desc:
                     status = s.status_desc
                 else: