From 59426b32417e4fcacdf896ddb189327ac6d87d3d Mon Sep 17 00:00:00 2001 From: David Backeus Date: Tue, 29 Oct 2024 15:00:34 +0100 Subject: [PATCH] Add k node:pvcs command to list all PVCs --- k | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/k b/k index 123932b..fbc5da3 100755 --- a/k +++ b/k @@ -166,6 +166,7 @@ def print_commands puts "k logs:search []" + gray(" search application logs via grafana") puts "k node:failover " + gray(" failover all databases on a node in a kubernetes cluster") puts "k node:purge-pvcs " + gray(" delete all PVCs on a node in a kubernetes cluster") + puts "k node:pvcs []" + gray(" list PVCs in a kubernetes cluster") puts "k pg" + gray(" list postgres related commands") puts "k redis" + gray(" list redis related commands") puts "k releases " + gray(" lists the past 15 releases for an application") @@ -2209,6 +2210,34 @@ def generate_resource puts "Please inspect and tweak all changes before you commit and push" end +def node_pvcs + all_pvcs = YAML.load read_kubectl("get pvc --all-namespaces -o yaml") + + pvcs_per_node = all_pvcs.fetch("items").each_with_object({}) do |pvc, hash| + node = pvc.dig("metadata", "annotations", "volume.kubernetes.io/selected-node") + hash[node] ||= [] + hash[node] << "#{pvc.dig("metadata", "namespace")}/#{pvc.dig("metadata", "name")}" + end + + node = ARGV.delete_at(0) + if node + pvcs = pvcs_per_node[node] + if pvcs + puts + puts bold(node) + puts pvcs + else + puts "No PVCs found on '#{node}'." + end + else + pvcs_per_node.sort_by(&:first).each do |node, pvcs| + puts + puts bold(node) + puts pvcs + end + end +end + # Failover all primary Postgres and Redis databases on a node def node_failover node_name = ARGV.delete_at(0)