Skip to content

Commit

Permalink
Fixed listing for daemonsets and secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnedo committed Aug 3, 2024
1 parent a96af91 commit a1af7bd
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 222 deletions.
11 changes: 6 additions & 5 deletions src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async fn create_node(args: CreateNodeArgs) -> Result<(), Box<dyn Error>> {

config.persist(Some(args.config.skateconfig.clone()))?;

/// Refresh state so that we can apply coredns later
// Refresh state so that we can apply coredns later
let state = refreshed_state(&cluster.name, &all_conns, &config).await?;
state.persist()?;

Expand All @@ -182,10 +182,11 @@ async fn create_node(args: CreateNodeArgs) -> Result<(), Box<dyn Error>> {
}

async fn install_manifests(args: &CreateNodeArgs, config: &Cluster, node: &Node) -> Result<(), Box<dyn Error>> {
/// COREDNS
/// coredns listens on port 53 and 5533
/// port 53 serves .cluster.skate by forwarding to all coredns instances on port 5553
/// uses fanout plugin

// COREDNS
// coredns listens on port 53 and 5533
// port 53 serves .cluster.skate by forwarding to all coredns instances on port 5553
// uses fanout plugin

// replace forward list in coredns config with that of other hosts
let fanout_list = config.nodes.iter().map(|n| n.host.clone() + ":5553").join(" ");
Expand Down
1 change: 0 additions & 1 deletion src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub async fn delete(args: DeleteArgs) -> Result<(), Box<dyn Error>> {
DeleteCommands::Ingress(args) => delete_resource(ResourceType::Ingress, args).await?,
DeleteCommands::Cronjob(args) => delete_resource(ResourceType::CronJob, args).await?,
DeleteCommands::Secret(args) => delete_resource(ResourceType::Secret, args).await?,
_ => todo!()
}
Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl Describer<NodeState> for NodeDescriber {
fn find(&self, filters: &DescribeObjectArgs, state: &ClusterState) -> Option<NodeState> {
let id = filters.id.as_ref().and_then(|cmd| match cmd {
IdCommand::Id(ids) => ids.first().and_then(|id| Some((*id).clone())),
_ => None
});
let id = match id {
Some(id) => id,
Expand Down
2 changes: 1 addition & 1 deletion src/get/cronjob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Lister<ObjectListItem> for CronjobsLister {

// TODO - record last run and how many running (somehow)
fn print(&self, resources: Vec<ObjectListItem>) {
macro_rules! cols { () => ("{0: <10} {1: <10} {2: <10} {3: <10} {4: <10} {5: <10} {6: <15} {7: <10}") };
macro_rules! cols { () => ("{0: <10} {1: <10} {2: <10} {3: <10} {4: <10} {5: <10} {6: <15} {7: <10}") }
println!(
cols!(),
"NAMESPACE", "NAME", "SCHEDULE", "TIMEZONE", "SUSPEND", "ACTIVE", "LAST SCHEDULE", "AGE"
Expand Down
35 changes: 22 additions & 13 deletions src/get/daemonset.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::collections::HashMap;
use chrono::{Local};
use chrono::Local;
use itertools::Itertools;
use crate::get::{GetObjectArgs, IdCommand, Lister};
use crate::skatelet::{PodmanPodInfo, PodmanPodStatus, SystemInfo};
use crate::skatelet::SystemInfo;
use crate::skatelet::system::podman::{PodmanPodInfo, PodmanPodStatus};
use crate::state::state::ClusterState;
use crate::util::age;

pub(crate) struct DaemonsetLister {}

impl Lister<(String, PodmanPodInfo)> for DaemonsetLister {
fn selector(&self, _si: &SystemInfo, _ns: &str, _id: &str) -> Option<Vec<(String, PodmanPodInfo)>> {
impl Lister<(usize, String, PodmanPodInfo)> for DaemonsetLister {
fn selector(&self, _si: &SystemInfo, _ns: &str, _id: &str) -> Option<Vec<(usize, String, PodmanPodInfo)>> {
todo!()
}
fn list(&self, args: &GetObjectArgs, state: &ClusterState) -> Vec<(String, PodmanPodInfo)> {
fn list(&self, args: &GetObjectArgs, state: &ClusterState) -> Vec<(usize, String, PodmanPodInfo)> {
let pods: Vec<_> = state.nodes.iter().filter_map(|n| {
let items: Vec<_> = n.host_info.clone()?.system_info?.pods.unwrap_or_default().into_iter().filter_map(|p| {
let ns = args.namespace.clone().unwrap_or("default".to_string());
Expand All @@ -38,7 +39,7 @@ impl Lister<(String, PodmanPodInfo)> for DaemonsetLister {
None => false
};
if match_ns || match_id || (id.is_none() && ns == "" && daemonset_ns != "skate" ) {
return Some((daemonset, p));
return Some((state.nodes.len(), daemonset, p));
}
None
}).collect();
Expand All @@ -50,17 +51,22 @@ impl Lister<(String, PodmanPodInfo)> for DaemonsetLister {
pods
}

fn print(&self, items: Vec<(String, PodmanPodInfo)>) {
fn print(&self, items: Vec<(usize, String, PodmanPodInfo)>) {
// NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
macro_rules! cols {
() => ("{0: <15} {1: <15} {2: <12} {3: <12} {4: <12} {5: <12} {6: <12} {7: <50} {8: <15}")
}
println!(
"{0: <30} {1: <10} {2: <10} {3: <10} {4: <30}",
"NAME", "READY", "STATUS", "RESTARTS", "AGE"
cols!(),
"NAMESPACE", "NAME", "DESIRED", "CURRENT", "READY", "UP-TO-DATE", "AVAILABLE", "NODE SELECTOR", "AGE"
);
let pods = items.into_iter().fold(HashMap::<String, Vec<PodmanPodInfo>>::new(), |mut acc, (depl, pod)| {
let num_nodes = items.first().unwrap().0;
let pods = items.into_iter().fold(HashMap::<String, Vec<PodmanPodInfo>>::new(), |mut acc, (num_nodes, depl, pod)| {
acc.entry(depl).or_insert(vec![]).push(pod);
acc
});

for (deployment, pods) in pods {
for (name, pods) in pods {
let health_pods = pods.iter().filter(|p| PodmanPodStatus::Running == p.status).collect_vec().len();
let all_pods = pods.len();
let created = pods.iter().fold(Local::now(), |acc, item| {
Expand All @@ -69,10 +75,13 @@ impl Lister<(String, PodmanPodInfo)> for DaemonsetLister {
}
return acc;
});
let namespace = pods.first().unwrap().labels.get("skate.io/namespace").unwrap_or(&"default".to_string()).clone();
let node_selector = pods.first().unwrap().labels.iter().filter(|(k, _)| k.starts_with("nodeselector/")).map(|(k, v)| format!("{}={}", k, v)).collect_vec().join(",");

// assuming that we want same number as nodes, that's wrong but anyway
println!(
"{0: <30} {1: <10} {2: <10} {3: <10} {4: <30}",
deployment, format!("{}/{}", health_pods, all_pods), "", "", age(created)
cols!(),
namespace, name, num_nodes, pods.len(), health_pods, "", "", node_selector, age(created)
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/get/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use chrono::{Local};
use chrono::Local;
use itertools::Itertools;
use crate::get::{GetObjectArgs, IdCommand, Lister};
use crate::skatelet::{PodmanPodInfo, PodmanPodStatus, SystemInfo};
use crate::skatelet::SystemInfo;
use crate::skatelet::system::podman::{PodmanPodInfo, PodmanPodStatus};
use crate::state::state::ClusterState;
use crate::util::age;

Expand Down
5 changes: 3 additions & 2 deletions src/get/pod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

use crate::get::{Lister};
use crate::get::Lister;
use crate::get::lister::NameFilters;
use crate::skatelet::{PodmanPodInfo, SystemInfo};
use crate::skatelet::SystemInfo;
use crate::skatelet::system::podman::PodmanPodInfo;
use crate::util::age;

pub (crate) struct PodLister {}
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use k8s_openapi::Metadata;


use crate::skate::SupportedResources;
use crate::skatelet::PodmanPodStatus;
use crate::skatelet::system::podman::PodmanPodStatus;
use crate::ssh::{SshClients};
use crate::state::state::{ClusterState, NodeState};
use crate::util::{CHECKBOX_EMOJI, CROSS_EMOJI, EQUAL_EMOJI, hash_k8s_resource, INFO_EMOJI, metadata_name};
Expand Down
4 changes: 1 addition & 3 deletions src/skatelet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod skatelet;
mod apply;

mod system;
pub(crate) mod system;
mod cni;
mod template;
mod delete;

pub use skatelet::skatelet;
pub use system::SystemInfo;
pub use system::PodmanPodInfo;
pub use system::PodmanPodStatus;

189 changes: 3 additions & 186 deletions src/skatelet/system.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
mod podman;
pub(crate) mod podman;

use std::collections::{BTreeMap};
use std::env::consts::ARCH;
use sysinfo::{CpuRefreshKind, DiskKind, Disks, MemoryRefreshKind, RefreshKind, System};
use std::error::Error;


use anyhow::anyhow;
use chrono::{DateTime, Local};
use clap::{Args, Subcommand};

use k8s_openapi::api::core::v1::{Pod, PodSpec, PodStatus as K8sPodStatus, Secret};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use k8s_openapi::api::core::v1::Secret;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use strum_macros::{Display, EnumString};
use podman::PodmanPodInfo;
use crate::filestore::{FileStore, ObjectListItem};

use crate::skate::{Distribution, exec_cmd, Platform};
Expand Down Expand Up @@ -70,188 +67,8 @@ pub struct SystemInfo {
pub hostname: String,
}


#[derive(Clone, Debug, EnumString, Display, Serialize, Deserialize, PartialEq)]
pub enum PodmanPodStatus {
Created,
Running,
Stopped,
Exited,
Dead,
Degraded,
Error,
}

impl PodmanPodStatus {
fn to_pod_phase(self) -> String {
match self {
PodmanPodStatus::Running => "Running",
PodmanPodStatus::Stopped => "Succeeded",
PodmanPodStatus::Exited => "Succeeded",
PodmanPodStatus::Dead => "Failed",
PodmanPodStatus::Degraded => "Running",
PodmanPodStatus::Created => "Pending",
PodmanPodStatus::Error => "Failed",
}.to_string()
}
fn from_pod_phase(phase: &str) -> Self {
match phase {
"Running" => PodmanPodStatus::Running,
// lossy
"Succeeded" => PodmanPodStatus::Exited,
"Failed" => PodmanPodStatus::Dead,
"Pending" => PodmanPodStatus::Created,
_ => PodmanPodStatus::Created,
}
}
}

// TODO - have more generic ObjectMeta type for explaining existing resources

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct PodmanPodInfo {
pub id: String,
pub name: String,
pub status: PodmanPodStatus,
pub created: DateTime<Local>,
pub labels: BTreeMap<String, String>,
pub containers: Option<Vec<PodmanContainerInfo>>,
}


impl PodmanPodInfo {
pub fn namespace(&self) -> String {
self.labels.get("skate.io/namespace").map(|ns| ns.clone()).unwrap_or("".to_string())
}
pub fn deployment(&self) -> String {
self.labels.get("skate.io/deployment").map(|d| d.clone()).unwrap_or("".to_string())
}
}

impl From<Pod> for PodmanPodInfo {
fn from(value: Pod) -> Self {
PodmanPodInfo {
id: value.metadata.uid.unwrap_or("".to_string()),
name: value.metadata.name.unwrap_or("".to_string()),
status: PodmanPodStatus::from_pod_phase(value.status.and_then(|s| s.phase.and_then(|p| {
Some(p)
})).unwrap_or("".to_string()).as_str()),
created: value.metadata.creation_timestamp.and_then(|ts| Some(DateTime::from(ts.0))).unwrap_or(DateTime::from(Local::now())),
labels: value.metadata.labels.unwrap_or(BTreeMap::new()),
containers: None, // TODO
}
}
}

impl Into<Pod> for PodmanPodInfo {
fn into(self) -> Pod {
Pod {
metadata: ObjectMeta {
annotations: None,
creation_timestamp: Some(k8s_openapi::apimachinery::pkg::apis::meta::v1::Time(DateTime::from(self.created))),
deletion_grace_period_seconds: None,
deletion_timestamp: None,
finalizers: None,
generate_name: None,
generation: None,
labels: match self.labels.len() {
0 => None,
_ => Some(self.labels.iter().filter_map(|(k, v)| {
if k.starts_with("nodeselector/") {
None
} else {
Some((k.clone(), v.clone()))
}
}).collect())
},
managed_fields: None,
name: Some(self.name.clone()),
namespace: Some(self.namespace()),
owner_references: None,
resource_version: None,
self_link: None,
uid: Some(self.id),
},
spec: Some(PodSpec {
active_deadline_seconds: None,
affinity: None,
automount_service_account_token: None,
containers: vec![],
dns_config: None,
dns_policy: None,
enable_service_links: None,
ephemeral_containers: None,
host_aliases: None,
host_ipc: None,
host_network: None,
host_pid: None,
host_users: None,
hostname: None,
image_pull_secrets: None,
init_containers: None,
node_name: None,
node_selector: Some(self.labels.iter().filter_map(|(k, v)| {
if k.starts_with("nodeselector/") {
Some(((*k.clone()).strip_prefix("nodeselector/").unwrap_or(k).to_string(), (*v).clone()))
} else {
None
}
}).collect::<BTreeMap<String, String>>()),
os: None,
overhead: None,
preemption_policy: None,
priority: None,
priority_class_name: None,
readiness_gates: None,
resource_claims: None,
restart_policy: None,
runtime_class_name: None,
scheduler_name: None,
scheduling_gates: None,
security_context: None,
service_account: None,
service_account_name: None,
set_hostname_as_fqdn: None,
share_process_namespace: None,
subdomain: None,
termination_grace_period_seconds: None,
tolerations: None,
topology_spread_constraints: None,
volumes: None,
}), // TODO
status: Some(K8sPodStatus
{
conditions: None,
container_statuses: None,
ephemeral_container_statuses: None,
host_ip: None,
host_ips: None,
init_container_statuses: None,
message: None,
nominated_node_name: None,
phase: Some(self.status.to_pod_phase()),
pod_ip: None,
pod_ips: None,
qos_class: None,
reason: None,
resize: None,
resource_claim_statuses: None,
start_time: None,
}),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct PodmanContainerInfo {
pub id: String,
pub names: String,
pub status: String,
pub restart_count: Option<usize>,
}

// returns (external, internal)
fn internal_ip() -> Result<Option<String>, Box<dyn Error>> {
let iface_cmd = match exec_cmd("which", &["ifconfig"]) {
Expand Down
Loading

0 comments on commit a1af7bd

Please sign in to comment.