From 25724750fd81150b788871c9a2e972fc3242f904 Mon Sep 17 00:00:00 2001 From: Ross Fairbanks Date: Sun, 15 May 2022 11:33:25 +0200 Subject: [PATCH 1/3] feat: Filter pods by node --- Cargo.lock | 6 +++--- Cargo.toml | 5 +++-- helm/scaphandre/templates/daemonset.yaml | 4 ++++ src/exporters/mod.rs | 10 +++++++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1a0fbf5..b18e5f15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -619,9 +619,8 @@ dependencies = [ [[package]] name = "k8s-sync" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d3c2f8f5cb2611742f8ceb73f23451690ff0d930149eac45fcb63ca86fbd443" +version = "0.2.2" +source = "git+https://github.com/rossf7/rs-k8s-sync?branch=fix/list-pods-options#c93bfd2d0d7ba73f9a874fe84d411ae6c3965eaf" dependencies = [ "base64", "chrono", @@ -1149,6 +1148,7 @@ dependencies = [ "docker-sync", "hostname", "hyper", + "k8s-openapi", "k8s-sync", "log", "loggerv", diff --git a/Cargo.toml b/Cargo.toml index 1cc7f3fc..a6b6a2e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,8 +28,9 @@ colored = "2.0.0" chrono = "0.4.19" docker-sync = { version = "0.1.2", optional = true } #docker-sync = { path = "../rs-docker-sync", optional = true } -k8s-sync = { version = "0.2.3", optional = true } -#k8s-sync = { path = "../rs-k8s-sync", optional = true } +k8s-openapi = { version = "0.13.0", features = ["v1_21"] } +#k8s-sync = { version = "0.2.3", optional = true } +k8s-sync = { git = "https://github.com/rossf7/rs-k8s-sync", branch = "fix/list-pods-options", optional = true } hyper = { version = "0.14", features = ["full"], optional = true } tokio = { version = "1", features = ["full"], optional = true} diff --git a/helm/scaphandre/templates/daemonset.yaml b/helm/scaphandre/templates/daemonset.yaml index f51767f4..48576f02 100644 --- a/helm/scaphandre/templates/daemonset.yaml +++ b/helm/scaphandre/templates/daemonset.yaml @@ -36,6 +36,10 @@ spec: {{- end }} {{- end }} env: + - name: KUBERNETES_NODE_NAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName {{- if .Values.scaphandre.rustBacktrace }} - name: RUST_BACKTRACE value: '{{ .Values.scaphandre.rustBacktrace }}' diff --git a/src/exporters/mod.rs b/src/exporters/mod.rs index 9ca12c7b..aa696760 100644 --- a/src/exporters/mod.rs +++ b/src/exporters/mod.rs @@ -14,8 +14,10 @@ use chrono::Utc; use clap::ArgMatches; use docker_sync::{container::Container, Docker}; use k8s_sync::kubernetes::Kubernetes; +use k8s_sync::ListOptional; use k8s_sync::Pod; use std::collections::HashMap; +use std::env; use std::fmt; use std::time::Duration; use utils::{get_docker_client, get_kubernetes_client, get_scaphandre_version}; @@ -595,7 +597,13 @@ impl MetricGenerator { fn gen_kubernetes_pods_basic_metadata(&mut self) { if self.watch_kubernetes { if let Some(kubernetes) = self.kubernetes_client.as_mut() { - if let Ok(pods_result) = kubernetes.list_pods("".to_string()) { + let node_name = env::var("KUBERNETES_NODE_NAME").unwrap_or_default(); + let selector = format!("spec.nodeName={}", node_name); + let mut list_options = ListOptional { ..Default::default() }; + if node_name != "" { + list_options.field_selector = Some(&selector); + } + if let Ok(pods_result) = kubernetes.list_pods("".to_string(), list_options) { self.pods = pods_result; debug!("Found {} pods", &self.pods.len()); self.pods_last_check = current_system_time_since_epoch().as_secs().to_string(); From ae6beb791ee2b4cca1aa6ed477cdc868a5747960 Mon Sep 17 00:00:00 2001 From: Ross Fairbanks Date: Tue, 17 May 2022 17:18:11 +0200 Subject: [PATCH 2/3] cargo fmt --- src/exporters/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/exporters/mod.rs b/src/exporters/mod.rs index aa696760..e2f191be 100644 --- a/src/exporters/mod.rs +++ b/src/exporters/mod.rs @@ -599,7 +599,9 @@ impl MetricGenerator { if let Some(kubernetes) = self.kubernetes_client.as_mut() { let node_name = env::var("KUBERNETES_NODE_NAME").unwrap_or_default(); let selector = format!("spec.nodeName={}", node_name); - let mut list_options = ListOptional { ..Default::default() }; + let mut list_options = ListOptional { + ..Default::default() + }; if node_name != "" { list_options.field_selector = Some(&selector); } From 0f739702e049a2051fac572ba64f28bf022aa9dc Mon Sep 17 00:00:00 2001 From: Ross Fairbanks Date: Tue, 17 May 2022 17:25:00 +0200 Subject: [PATCH 3/3] fmt: clippy --- src/exporters/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/exporters/mod.rs b/src/exporters/mod.rs index e2f191be..505c2432 100644 --- a/src/exporters/mod.rs +++ b/src/exporters/mod.rs @@ -602,7 +602,7 @@ impl MetricGenerator { let mut list_options = ListOptional { ..Default::default() }; - if node_name != "" { + if !node_name.is_empty() { list_options.field_selector = Some(&selector); } if let Ok(pods_result) = kubernetes.list_pods("".to_string(), list_options) {