kelper 0.0.30

A CLI tool to serve as swiss-army knife for your operations on Kubernetes pods and nodes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use crate::utils::{strip_registry, KNOWN_REGISTRIES};
use anyhow::{Context, Result};
use k8s_openapi::api::apps::v1::Deployment;
use k8s_openapi::api::core::v1::Pod;
use kube::{api::ListParams, Api, Client};
use thiserror::Error;
use tracing::{debug, error, info, instrument};

/// Represents a container image running in a Kubernetes pod
#[derive(Debug, Clone)]
pub struct PodImage {
    /// Name of the pod containing the image
    pub pod_name: String,
    /// Name of the node where the pod is running
    pub node_name: String,
    /// Kubernetes namespace of the pod
    pub namespace: String,
    /// Name of the container using this image
    pub container_name: String,
    /// Name of the container image
    pub image_name: String,
    /// Version/tag of the container image
    pub image_version: String,
    /// Registry where the image is hosted
    pub registry: String,
    /// Image digest (if available)
    pub digest: String,
}

/// Errors that can occur when interacting with Kubernetes
#[derive(Debug, Error)]
pub enum K8sError {
    /// Configuration-related errors
    #[error("Configuration error: {0}")]
    ConfigError(String),
    /// Connection-related errors
    #[error("Connection error: {0}")]
    ConnectionError(String),
    /// API-related errors
    #[error("API error: {0}")]
    ApiError(String),
    /// Resource not found errors
    #[error("Resource not found: {0}")]
    ResourceNotFound(String),
}

/// Client for interacting with Kubernetes clusters
pub struct K8sClient {
    /// The underlying Kubernetes client
    client: Client,
}

impl K8sClient {
    /// Create a new Kubernetes client
    ///
    /// # Returns
    ///
    /// * `Result<Self>` - A new K8sClient instance or an error if initialization fails
    #[instrument(skip_all)]
    pub async fn new() -> Result<Self> {
        debug!("Initializing Kubernetes client");

        let kubeconfig_path = Self::get_kubeconfig_path()?;
        debug!(path = %kubeconfig_path, "Using kubeconfig path");

        let client = Client::try_default()
            .await
            .context("Failed to create Kubernetes client")?;

        let k8s_client = Self { client };

        // Verify cluster accessibility
        if !k8s_client.is_accessible().await? {
            return Err(
                K8sError::ConnectionError("Kubernetes cluster is not accessible".into()).into(),
            );
        }

        info!("Successfully initialized Kubernetes client");
        Ok(k8s_client)
    }

    /// Get the path to the kubeconfig file
    ///
    /// # Returns
    ///
    /// * `Result<String>` - The path to the kubeconfig file or an error if not found
    fn get_kubeconfig_path() -> Result<String> {
        if let Ok(path) = std::env::var("KUBECONFIG") {
            info!("Using kubeconfig from KUBECONFIG environment variable");
            return Ok(path);
        }

        debug!("KUBECONFIG not set, checking default location");
        let home_dir = std::env::var("HOME").context("Failed to get HOME directory")?;
        let default_kubeconfig = format!("{}/.kube/config", home_dir);

        if !std::path::Path::new(&default_kubeconfig).exists() {
            return Err(
                K8sError::ConfigError("No kubeconfig found at default location".into()).into(),
            );
        }

        info!("Using default kubeconfig location");
        Ok(default_kubeconfig)
    }

    /// Check if the Kubernetes cluster is accessible
    ///
    /// # Returns
    ///
    /// * `Result<bool>` - True if the cluster is accessible, false otherwise
    #[instrument(skip(self))]
    pub async fn is_accessible(&self) -> Result<bool> {
        debug!("Checking cluster accessibility");
        let api: Api<Pod> = Api::namespaced(self.client.clone(), "default");

        match api.list(&Default::default()).await {
            Ok(_) => {
                debug!("Successfully connected to cluster");
                Ok(true)
            }
            Err(e) => match e {
                kube::Error::Api(api_err) => {
                    error!("Kubernetes API error occurred");
                    Err(
                        K8sError::ApiError(format!("{} ({})", api_err.message, api_err.reason))
                            .into(),
                    )
                }
                _ => {
                    error!("Failed to connect to Kubernetes cluster");
                    Err(
                        K8sError::ConnectionError("Failed to connect to Kubernetes cluster".into())
                            .into(),
                    )
                }
            },
        }
    }

    /// Get pod images matching the specified criteria
    ///
    /// # Arguments
    ///
    /// * `namespace` - The namespace to search in
    /// * `node_name` - Optional node name filter
    /// * `pod_name` - Optional pod name filter
    /// * `registry_filter` - Optional registry filter
    /// * `all_namespaces` - Whether to search in all namespaces
    ///
    /// # Returns
    ///
    /// * `Result<Vec<PodImage>>` - List of matching pod images or an error
    #[instrument(skip(self), fields(
        namespace = %namespace,
        node = ?node_name,
        pod = ?pod_name,
        registry = ?registry_filter,
        all_namespaces = %all_namespaces
    ))]
    pub async fn get_pod_images(
        &self,
        namespace: &str,
        node_name: Option<&str>,
        pod_name: Option<&str>,
        registry_filter: Option<&str>,
        all_namespaces: bool,
    ) -> Result<Vec<PodImage>> {
        debug!(
            namespace = %namespace,
            node = ?node_name,
            pod = ?pod_name,
            registry = ?registry_filter,
            all_namespaces = %all_namespaces,
            "Fetching pod images"
        );

        if !all_namespaces && !self.namespace_exists(namespace).await? {
            let resource = format!("Namespace {} not found", namespace);
            return Err(K8sError::ResourceNotFound(resource).into());
        }

        let list_params = Self::build_list_params(node_name, pod_name);
        let pods = self.get_pods_api(namespace, all_namespaces, node_name)?;

        let pods_list = pods
            .list(&list_params)
            .await
            .context("Failed to list pods")?;

        debug!("Found {} pods", pods_list.items.len());

        if pods_list.items.is_empty() {
            let resource = match (node_name, pod_name) {
                (Some(node), Some(pod)) => format!("pod {} on node {}", pod, node),
                (Some(node), None) => format!("pods on node {}", node),
                (None, Some(pod)) => format!("pod {}", pod),
                (None, None) => format!("pods in namespace {}", namespace),
            };
            return Err(K8sError::ResourceNotFound(resource).into());
        }

        let mut all_images = Vec::new();
        for pod in pods_list {
            if !Self::should_process_pod(&pod, all_namespaces, node_name, pod_name) {
                continue;
            }

            let pod_images = process_pod(&pod);
            debug!(images = pod_images.len(), "Processed pod images");
            all_images.extend(pod_images);
        }

        if let Some(registry_filter) = registry_filter {
            let before_count = all_images.len();
            all_images.retain(|image| image.registry == registry_filter);
            debug!(
                before = before_count,
                after = all_images.len(),
                "Filtered images by registry"
            );
        }

        info!(
            total_images = all_images.len(),
            "Successfully retrieved pod images"
        );
        Ok(all_images)
    }

    /// Build list parameters for pod queries
    fn build_list_params(node_name: Option<&str>, pod_name: Option<&str>) -> ListParams {
        let mut field_selectors = Vec::new();

        if let Some(node) = node_name {
            field_selectors.push(format!("spec.nodeName={}", node));
        }

        if let Some(name) = pod_name {
            field_selectors.push(format!("metadata.name={}", name));
        }

        ListParams::default().fields(&field_selectors.join(","))
    }

    /// Get the pods API for the specified namespace
    fn get_pods_api(
        &self,
        namespace: &str,
        all_namespaces: bool,
        _node_name: Option<&str>,
    ) -> Result<Api<Pod>> {
        let api = if all_namespaces {
            Api::all(self.client.clone())
        } else {
            Api::namespaced(self.client.clone(), namespace)
        };
        Ok(api)
    }

    /// Check if a pod should be processed based on filters
    fn should_process_pod(
        pod: &Pod,
        _all_namespaces: bool,
        node_name: Option<&str>,
        pod_name: Option<&str>,
    ) -> bool {
        if let Some(name) = pod_name {
            if pod.metadata.name.as_deref() != Some(name) {
                return false;
            }
        }

        if let Some(node) = node_name {
            if pod.spec.as_ref().and_then(|s| s.node_name.as_deref()) != Some(node) {
                return false;
            }
        }

        true
    }

    /// Get unique container image registries used in the cluster
    ///
    /// # Arguments
    ///
    /// * `namespace` - The namespace to search in
    /// * `all_namespaces` - Whether to search in all namespaces
    ///
    /// # Returns
    ///
    /// * `Result<Vec<String>>` - List of unique registries or an error
    #[instrument(skip(self), fields(
        namespace = %namespace,
        all_namespaces = %all_namespaces
    ))]
    pub async fn get_unique_registries(
        &self,
        namespace: &str,
        all_namespaces: bool,
    ) -> Result<Vec<String>> {
        debug!(
            namespace = %namespace,
            all_namespaces = %all_namespaces,
            "Fetching unique registries from deployments"
        );

        if !all_namespaces && !self.namespace_exists(namespace).await? {
            let resource = format!("Namespace {} not found", namespace);
            return Err(K8sError::ResourceNotFound(resource).into());
        }

        let deployments_api: Api<Deployment> = if all_namespaces {
            Api::all(self.client.clone())
        } else {
            Api::namespaced(self.client.clone(), namespace)
        };

        let deployments = deployments_api
            .list(&Default::default())
            .await
            .context("Failed to list deployments")?;

        debug!("Found {} deployments", deployments.items.len());

        if deployments.items.is_empty() {
            let resource = format!("deployments in namespace {}", namespace);
            return Err(K8sError::ResourceNotFound(resource).into());
        }

        let mut registries = std::collections::HashSet::new();
        for deploy in deployments {
            if let Some(spec) = deploy.spec {
                if let Some(pod_spec) = spec.template.spec {
                    for container in pod_spec.containers {
                        if let Some(image) = container.image {
                            let registry = extract_registry(&image);
                            registries.insert(registry);
                        }
                    }
                }
            }
        }

        let mut registries_vec: Vec<String> = registries.into_iter().collect();
        registries_vec.sort();

        info!(
            total_registries = registries_vec.len(),
            "Successfully retrieved unique registries from deployments"
        );
        Ok(registries_vec)
    }

    /// Check if a namespace exists
    ///
    /// # Arguments
    ///
    /// * `namespace` - The name of the namespace to check
    ///
    /// # Returns
    ///
    /// * `Result<bool>` - True if the namespace exists, false otherwise, or an error if the API call fails
    #[instrument(skip(self), fields(namespace = %namespace))]
    pub async fn namespace_exists(&self, namespace: &str) -> Result<bool> {
        debug!(namespace = %namespace, "Checking if namespace exists");
        let namespaces_api: Api<k8s_openapi::api::core::v1::Namespace> =
            Api::all(self.client.clone());
        match namespaces_api.get(namespace).await {
            Ok(_) => {
                debug!(namespace = %namespace, "Namespace found");
                Ok(true)
            }
            Err(kube::Error::Api(api_err)) if api_err.code == 404 => {
                debug!(namespace = %namespace, "Namespace not found");
                Ok(false)
            }
            Err(e) => {
                error!(namespace = %namespace, error = %e, "Failed to check namespace existence");
                Err(
                    K8sError::ApiError(format!("Failed to check namespace {}: {}", namespace, e))
                        .into(),
                )
            }
        }
    }
}

/// Extract the registry from a container image reference
///
/// # Arguments
///
/// * `image` - The container image reference
///
/// # Returns
///
/// * `String` - The registry name
pub fn extract_registry(image: &str) -> String {
    // Split the image string by '/'
    let parts: Vec<&str> = image.split('/').collect();

    // If there's only one part (e.g., "ubuntu" or "nginx"), it's a Docker Hub official image
    if parts.len() == 1 {
        return "docker.io".to_string();
    }

    // If there are two parts without dots or colons in the first part (e.g., "library/ubuntu"),
    // it's likely a Docker Hub image with namespace
    if parts.len() == 2 && !parts[0].contains('.') && !parts[0].contains(':') {
        return "docker.io".to_string();
    }

    // Get the potential registry (first part)
    let potential_registry = parts[0];

    // Check for localhost variants (with or without port)
    if potential_registry == "localhost"
        || potential_registry.starts_with("localhost:")
        || potential_registry.starts_with("127.0.0.1")
        || potential_registry.starts_with("0.0.0.0")
        || potential_registry.starts_with("[::1]")
    {
        return potential_registry.to_string();
    }

    // Check for IPv4 address (with or without port)
    let ip_parts: Vec<&str> = potential_registry.split(':').collect();
    let ip = ip_parts[0];
    if ip.split('.').filter(|&p| !p.is_empty()).count() == 4
        && ip.split('.').all(|p| p.parse::<u8>().is_ok())
    {
        return potential_registry.to_string();
    }

    // Check for IPv6 address (with or without port)
    if potential_registry.starts_with('[') && potential_registry.contains(']') {
        return potential_registry.to_string();
    }

    // Check for known public registries
    let known_registries = KNOWN_REGISTRIES;
    for registry in &known_registries {
        if potential_registry == *registry || potential_registry.ends_with(*registry) {
            return potential_registry.to_string();
        }
    }

    // For any domain with dots (e.g., "my-registry.example.com") or with port (e.g., "registry:5000")
    if potential_registry.contains('.') || potential_registry.contains(':') {
        return potential_registry.to_string();
    }

    // Default to Docker Hub if none of the above matches
    "docker.io".to_string()
}

/// Split a container image reference into name and version
///
/// # Arguments
///
/// * `image` - The container image reference
///
/// # Returns
///
/// * `(String, String)` - Tuple of (image name, image version)
pub fn split_image(image: &str) -> (String, String) {
    // First check for a digest (SHA)
    if let Some(digest_index) = image.find('@') {
        // We have a digest, get the part before the digest
        let image_with_tag = &image[..digest_index];
        let digest = &image[digest_index..]; // includes the @ symbol

        // Find the last colon which separates the image name from the tag
        if let Some(tag_index) = image_with_tag.rfind(':') {
            // Check if this colon is part of a port number in the registry
            // Look for slashes to determine if this is likely a registry port
            let last_slash_index = image_with_tag.rfind('/').unwrap_or(0);

            if tag_index > last_slash_index {
                // This colon is after the last slash, so it's a tag separator
                let name = &image_with_tag[..tag_index];
                let tag = &image_with_tag[tag_index + 1..];
                (name.to_string(), format!("{}@{}", tag, &digest[1..]))
            } else {
                // This colon is part of the registry address, no tag specified
                (
                    image_with_tag.to_string(),
                    format!("latest@{}", &digest[1..]),
                )
            }
        } else {
            // No tag present, use "latest" with the digest
            (
                image_with_tag.to_string(),
                format!("latest@{}", &digest[1..]),
            )
        }
    } else {
        // No digest, handle image name and tag
        // Find the last colon which might separate the image name from the tag
        if let Some(tag_index) = image.rfind(':') {
            // Check if this colon is part of a port number in the registry
            // Look for slashes to determine if this is likely a registry port
            let last_slash_index = image.rfind('/').unwrap_or(0);

            if tag_index > last_slash_index {
                // This colon is after the last slash, so it's a tag separator
                let name = &image[..tag_index];
                let tag = &image[tag_index + 1..];
                return (name.to_string(), tag.to_string());
            }
        }

        // No valid tag separator found
        (image.to_string(), "latest".to_string())
    }
}

/// Extract the digest of a container from a pod
///
/// # Arguments
///
/// * `pod` - The pod containing the container
/// * `container_name` - The name of the container
///
/// # Returns
///
/// * `Option<String>` - The container digest if available
fn extract_container_digest(pod: &Pod, container_name: &str) -> Option<String> {
    pod.status
        .as_ref()?
        .container_statuses
        .as_ref()?
        .iter()
        .find(|cs| cs.name == container_name)?
        .image_id
        .split(':')
        .nth(1)
        .map(String::from)
}

/// Process a pod to extract information about its container images
///
/// # Arguments
///
/// * `pod` - The pod to process
///
/// # Returns
///
/// * `Vec<PodImage>` - List of container images in the pod
pub fn process_pod(pod: &Pod) -> Vec<PodImage> {
    let mut pod_images = Vec::new();
    let pod_name = pod.metadata.name.clone().unwrap_or_default();
    let namespace = pod.metadata.namespace.clone().unwrap_or_default();
    let node_name = pod
        .spec
        .as_ref()
        .and_then(|spec| spec.node_name.clone())
        .unwrap_or_default();

    if let Some(spec) = &pod.spec {
        let containers = &spec.containers;
        for container in containers {
            if let Some(image) = &container.image {
                let registry = extract_registry(image);
                let (_image_name, image_version) = split_image(image);
                let image_name = strip_registry(&_image_name, &registry);
                let digest = extract_container_digest(pod, &container.name).unwrap_or_default();

                pod_images.push(PodImage {
                    pod_name: pod_name.clone(),
                    namespace: namespace.clone(),
                    container_name: container.name.clone(),
                    image_name,
                    image_version,
                    node_name: node_name.clone(),
                    registry,
                    digest,
                });
            }
        }
    }

    pod_images
}