|
| 1 | +use super::BitteFind; |
| 2 | +use crate::nomad::alloc::NomadAlloc; |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use std::net::IpAddr; |
| 5 | + |
| 6 | +impl BitteFind for super::BitteNodes { |
| 7 | + fn find_with_job( |
| 8 | + self, |
| 9 | + name: &str, |
| 10 | + group: &str, |
| 11 | + index: &str, |
| 12 | + namespace: &str, |
| 13 | + ) -> Result<(Self::Item, NomadAlloc)> { |
| 14 | + let node = self |
| 15 | + .into_iter() |
| 16 | + .find(|node| { |
| 17 | + let client = &node.nomad_client; |
| 18 | + if client.is_none() { |
| 19 | + return false; |
| 20 | + }; |
| 21 | + |
| 22 | + let allocs = &client.as_ref().unwrap().allocs; |
| 23 | + if allocs.is_none() || allocs.as_ref().unwrap().is_empty() { |
| 24 | + return false; |
| 25 | + }; |
| 26 | + |
| 27 | + allocs.as_ref().unwrap().iter().any(|alloc| { |
| 28 | + alloc.namespace == namespace |
| 29 | + && alloc.job_id == name |
| 30 | + && alloc.task_group == group |
| 31 | + && alloc.index.get() == index.parse().ok() |
| 32 | + && alloc.status == "running" |
| 33 | + }) |
| 34 | + }) |
| 35 | + .with_context(|| { |
| 36 | + format!( |
| 37 | + "{}, {}, {} does not match any running nomad allocations in namespace {}", |
| 38 | + name, group, index, namespace |
| 39 | + ) |
| 40 | + })?; |
| 41 | + let alloc = node |
| 42 | + .nomad_client |
| 43 | + .as_ref() |
| 44 | + .unwrap() |
| 45 | + .allocs |
| 46 | + .as_ref() |
| 47 | + .unwrap() |
| 48 | + .iter() |
| 49 | + .find(|alloc| { |
| 50 | + alloc.namespace == namespace |
| 51 | + && alloc.job_id == name |
| 52 | + && alloc.task_group == group |
| 53 | + && alloc.index.get() == index.parse().ok() |
| 54 | + && alloc.status == "running" |
| 55 | + }) |
| 56 | + .unwrap() |
| 57 | + .clone(); |
| 58 | + Ok((node, alloc)) |
| 59 | + } |
| 60 | + |
| 61 | + fn find_needle(self, needle: &str) -> Result<Self::Item> { |
| 62 | + self.into_iter() |
| 63 | + .find(|node| { |
| 64 | + let ip = needle.parse::<IpAddr>().ok(); |
| 65 | + |
| 66 | + node.id == needle |
| 67 | + || node.name == needle |
| 68 | + || node |
| 69 | + .nomad_client |
| 70 | + .as_ref() |
| 71 | + .unwrap_or(&Default::default()) |
| 72 | + .id |
| 73 | + .hyphenated() |
| 74 | + .to_string() |
| 75 | + == needle |
| 76 | + || Some(node.priv_ip) == ip |
| 77 | + || Some(node.pub_ip) == ip |
| 78 | + }) |
| 79 | + .with_context(|| format!("{} does not match any nodes", needle)) |
| 80 | + } |
| 81 | + |
| 82 | + fn find_clients(self, node_class: Option<String>) -> Self { |
| 83 | + match node_class { |
| 84 | + Some(class) => self |
| 85 | + .into_iter() |
| 86 | + .filter(|node| match &node.nomad_client { |
| 87 | + Some(client) => client.node_class.clone().unwrap_or_default() == class, |
| 88 | + None => false, |
| 89 | + }) |
| 90 | + .collect(), |
| 91 | + None => self.into_iter().filter(|node| node.asg.is_some()).collect(), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn find_needles(self, needles: Vec<&str>) -> Self { |
| 96 | + self.into_iter() |
| 97 | + .filter(|node| { |
| 98 | + let ips: Vec<Option<IpAddr>> = needles |
| 99 | + .iter() |
| 100 | + .map(|needle| needle.parse::<IpAddr>().ok()) |
| 101 | + .collect(); |
| 102 | + |
| 103 | + needles.contains(&&*node.id) |
| 104 | + || needles.contains(&&*node.name) |
| 105 | + || needles.contains( |
| 106 | + &&*node |
| 107 | + .nomad_client |
| 108 | + .as_ref() |
| 109 | + .unwrap_or(&Default::default()) |
| 110 | + .id |
| 111 | + .hyphenated() |
| 112 | + .to_string(), |
| 113 | + ) |
| 114 | + || ips.contains(&Some(node.priv_ip)) |
| 115 | + || ips.contains(&Some(node.pub_ip)) |
| 116 | + }) |
| 117 | + .collect() |
| 118 | + } |
| 119 | +} |
0 commit comments