Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: List only pods for current node #174

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hyper = { version = "0.14", features = ["full"], optional = true }
tokio = { version = "1", features = ["full"], optional = true}

Expand Down
4 changes: 4 additions & 0 deletions helm/scaphandre/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ spec:
{{- end }}
{{- end }}
env:
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{- if .Values.scaphandre.rustBacktrace }}
- name: RUST_BACKTRACE
value: '{{ .Values.scaphandre.rustBacktrace }}'
Expand Down
12 changes: 11 additions & 1 deletion src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -595,7 +597,15 @@ 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.is_empty() {
list_options.field_selector = Some(&selector);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the cleanest way I could see to set the field selector as it uses a &str but it's very possible I'm missing something :)

}
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();
Expand Down