|
| 1 | +using k8s.Models; |
| 2 | + |
| 3 | +namespace k8s.kubectl.beta; |
| 4 | + |
| 5 | +public partial class AsyncKubectl |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Describes a pair of Kubernetes resource and its metrics. |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="TResource">The type of Kubernetes resource.</typeparam> |
| 11 | + /// <typeparam name="TMetrics">The type of metrics.</typeparam> |
| 12 | + public class ResourceMetrics<TResource, TMetrics> |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Gets or sets the Kubernetes resource. |
| 16 | + /// </summary> |
| 17 | + public required TResource Resource { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets or sets the metrics for the resource. |
| 21 | + /// </summary> |
| 22 | + public required TMetrics Metrics { get; set; } |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Get top nodes sorted by CPU or memory usage. |
| 27 | + /// </summary> |
| 28 | + /// <param name="metric">The metric to sort by ("cpu" or "memory"). Defaults to "cpu".</param> |
| 29 | + /// <param name="cancellationToken">Cancellation token.</param> |
| 30 | + /// <returns>A list of nodes with their metrics, sorted by the specified metric in descending order.</returns> |
| 31 | + public async Task<List<ResourceMetrics<V1Node, NodeMetrics>>> TopNodesAsync(string metric = "cpu", CancellationToken cancellationToken = default) |
| 32 | + { |
| 33 | + if (metric != "cpu" && metric != "memory") |
| 34 | + { |
| 35 | + throw new ArgumentException("Metric must be either 'cpu' or 'memory'", nameof(metric)); |
| 36 | + } |
| 37 | + |
| 38 | + // Get all nodes |
| 39 | + var nodes = await client.CoreV1.ListNodeAsync(cancellationToken: cancellationToken).ConfigureAwait(false); |
| 40 | + |
| 41 | + // Get node metrics |
| 42 | + var nodeMetrics = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false); |
| 43 | + |
| 44 | + // Create a dictionary for quick lookup of metrics by node name |
| 45 | + var metricsDict = nodeMetrics.Items.ToDictionary(m => m.Metadata.Name, m => m); |
| 46 | + |
| 47 | + // Combine nodes with their metrics and calculate percentage |
| 48 | + var result = new List<ResourceMetrics<V1Node, NodeMetrics>>(); |
| 49 | + foreach (var node in nodes.Items) |
| 50 | + { |
| 51 | + if (metricsDict.TryGetValue(node.Metadata.Name, out var metrics)) |
| 52 | + { |
| 53 | + result.Add(new ResourceMetrics<V1Node, NodeMetrics> |
| 54 | + { |
| 55 | + Resource = node, |
| 56 | + Metrics = metrics, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Sort by metric value (percentage of capacity) in descending order |
| 62 | + result.Sort((a, b) => |
| 63 | + { |
| 64 | + var percentageA = CalculateNodePercentage(a.Resource, a.Metrics, metric); |
| 65 | + var percentageB = CalculateNodePercentage(b.Resource, b.Metrics, metric); |
| 66 | + return percentageB.CompareTo(percentageA); // Descending order |
| 67 | + }); |
| 68 | + |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Get top pods in a namespace sorted by CPU or memory usage. |
| 74 | + /// </summary> |
| 75 | + /// <param name="namespace">The namespace to get pod metrics from.</param> |
| 76 | + /// <param name="metric">The metric to sort by ("cpu" or "memory"). Defaults to "cpu".</param> |
| 77 | + /// <param name="cancellationToken">Cancellation token.</param> |
| 78 | + /// <returns>A list of pods with their metrics, sorted by the specified metric in descending order.</returns> |
| 79 | + public async Task<List<ResourceMetrics<V1Pod, PodMetrics>>> TopPodsAsync(string @namespace, string metric = "cpu", CancellationToken cancellationToken = default) |
| 80 | + { |
| 81 | + if (string.IsNullOrEmpty(@namespace)) |
| 82 | + { |
| 83 | + throw new ArgumentException("Namespace cannot be null or empty", nameof(@namespace)); |
| 84 | + } |
| 85 | + |
| 86 | + if (metric != "cpu" && metric != "memory") |
| 87 | + { |
| 88 | + throw new ArgumentException("Metric must be either 'cpu' or 'memory'", nameof(metric)); |
| 89 | + } |
| 90 | + |
| 91 | + // Get all pods in the namespace |
| 92 | + var pods = await client.CoreV1.ListNamespacedPodAsync(@namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 93 | + |
| 94 | + // Get pod metrics for the namespace |
| 95 | + var podMetrics = await client.GetKubernetesPodsMetricsByNamespaceAsync(@namespace).ConfigureAwait(false); |
| 96 | + |
| 97 | + // Create a dictionary for quick lookup of metrics by pod name |
| 98 | + var metricsDict = podMetrics.Items.ToDictionary(m => m.Metadata.Name, m => m); |
| 99 | + |
| 100 | + // Combine pods with their metrics |
| 101 | + var result = new List<ResourceMetrics<V1Pod, PodMetrics>>(); |
| 102 | + foreach (var pod in pods.Items) |
| 103 | + { |
| 104 | + if (metricsDict.TryGetValue(pod.Metadata.Name, out var metrics)) |
| 105 | + { |
| 106 | + result.Add(new ResourceMetrics<V1Pod, PodMetrics> |
| 107 | + { |
| 108 | + Resource = pod, |
| 109 | + Metrics = metrics, |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Sort by metric value (sum across all containers) in descending order |
| 115 | + result.Sort((a, b) => |
| 116 | + { |
| 117 | + var sumA = CalculatePodMetricSum(a.Metrics, metric); |
| 118 | + var sumB = CalculatePodMetricSum(b.Metrics, metric); |
| 119 | + return sumB.CompareTo(sumA); // Descending order |
| 120 | + }); |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Calculate the percentage of node capacity used by the specified metric. |
| 127 | + /// </summary> |
| 128 | + private static double CalculateNodePercentage(V1Node node, NodeMetrics metrics, string metric) |
| 129 | + { |
| 130 | + if (metrics?.Usage == null || !metrics.Usage.TryGetValue(metric, out var usage)) |
| 131 | + { |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + if (node?.Status?.Capacity == null || !node.Status.Capacity.TryGetValue(metric, out var capacity)) |
| 136 | + { |
| 137 | + return double.PositiveInfinity; |
| 138 | + } |
| 139 | + |
| 140 | + var usageValue = usage.ToDouble(); |
| 141 | + var capacityValue = capacity.ToDouble(); |
| 142 | + |
| 143 | + if (capacityValue == 0) |
| 144 | + { |
| 145 | + return double.PositiveInfinity; |
| 146 | + } |
| 147 | + |
| 148 | + return usageValue / capacityValue; |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Calculate the sum of a metric across all containers in a pod. |
| 153 | + /// </summary> |
| 154 | + private static double CalculatePodMetricSum(PodMetrics podMetrics, string metric) |
| 155 | + { |
| 156 | + if (podMetrics?.Containers == null) |
| 157 | + { |
| 158 | + return 0; |
| 159 | + } |
| 160 | + |
| 161 | + double sum = 0; |
| 162 | + foreach (var container in podMetrics.Containers) |
| 163 | + { |
| 164 | + if (container?.Usage != null && container.Usage.TryGetValue(metric, out var value)) |
| 165 | + { |
| 166 | + sum += value.ToDouble(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return sum; |
| 171 | + } |
| 172 | +} |
0 commit comments