|
| 1 | +//! Helper functions which are not tied to a specific controller step |
| 2 | +
|
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +use snafu::{OptionExt, ResultExt, Snafu}; |
| 6 | +use stackable_operator::kube::runtime::reflector::Lookup; |
| 7 | +use strum::{EnumDiscriminants, IntoStaticStr}; |
| 8 | + |
| 9 | +use crate::framework::types::{ |
| 10 | + kubernetes::{NamespaceName, Uid}, |
| 11 | + operator::ClusterName, |
| 12 | +}; |
| 13 | + |
| 14 | +#[derive(Snafu, Debug, EnumDiscriminants)] |
| 15 | +#[strum_discriminants(derive(IntoStaticStr))] |
| 16 | +pub enum Error { |
| 17 | + #[snafu(display("failed to get the cluster name"))] |
| 18 | + GetClusterName {}, |
| 19 | + |
| 20 | + #[snafu(display("failed to get the namespace"))] |
| 21 | + GetNamespace {}, |
| 22 | + |
| 23 | + #[snafu(display("failed to get the UID"))] |
| 24 | + GetUid {}, |
| 25 | + |
| 26 | + #[snafu(display("failed to set the cluster name"))] |
| 27 | + ParseClusterName { |
| 28 | + source: crate::framework::macros::attributed_string_type::Error, |
| 29 | + }, |
| 30 | + |
| 31 | + #[snafu(display("failed to set the namespace"))] |
| 32 | + ParseNamespace { |
| 33 | + source: crate::framework::macros::attributed_string_type::Error, |
| 34 | + }, |
| 35 | + |
| 36 | + #[snafu(display("failed to set the UID"))] |
| 37 | + ParseUid { |
| 38 | + source: crate::framework::macros::attributed_string_type::Error, |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +type Result<T, E = Error> = std::result::Result<T, E>; |
| 43 | + |
| 44 | +/// Get the cluster name from the given resource |
| 45 | +pub fn get_cluster_name(cluster: &impl Lookup) -> Result<ClusterName> { |
| 46 | + let raw_cluster_name = cluster.name().context(GetClusterNameSnafu)?; |
| 47 | + let cluster_name = ClusterName::from_str(&raw_cluster_name).context(ParseClusterNameSnafu)?; |
| 48 | + |
| 49 | + Ok(cluster_name) |
| 50 | +} |
| 51 | + |
| 52 | +/// Get the namespace from the given resource |
| 53 | +pub fn get_namespace(resource: &impl Lookup) -> Result<NamespaceName> { |
| 54 | + let raw_namespace = resource.namespace().context(GetNamespaceSnafu)?; |
| 55 | + let namespace = NamespaceName::from_str(&raw_namespace).context(ParseNamespaceSnafu)?; |
| 56 | + |
| 57 | + Ok(namespace) |
| 58 | +} |
| 59 | + |
| 60 | +/// Get the UID from the given resource |
| 61 | +pub fn get_uid(resource: &impl Lookup) -> Result<Uid> { |
| 62 | + let raw_uid = resource.uid().context(GetUidSnafu)?; |
| 63 | + let uid = Uid::from_str(&raw_uid).context(ParseUidSnafu)?; |
| 64 | + |
| 65 | + Ok(uid) |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use stackable_operator::kube::runtime::reflector::Lookup; |
| 71 | + use uuid::uuid; |
| 72 | + |
| 73 | + use super::{ErrorDiscriminants, get_cluster_name, get_namespace, get_uid}; |
| 74 | + use crate::framework::types::{ |
| 75 | + kubernetes::{NamespaceName, Uid}, |
| 76 | + operator::ClusterName, |
| 77 | + }; |
| 78 | + |
| 79 | + #[derive(Debug, Default)] |
| 80 | + struct TestResource { |
| 81 | + name: Option<&'static str>, |
| 82 | + namespace: Option<&'static str>, |
| 83 | + uid: Option<&'static str>, |
| 84 | + } |
| 85 | + |
| 86 | + impl Lookup for TestResource { |
| 87 | + type DynamicType = (); |
| 88 | + |
| 89 | + fn kind(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> { |
| 90 | + "TestResource".into() |
| 91 | + } |
| 92 | + |
| 93 | + fn group(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> { |
| 94 | + "stackable.tech".into() |
| 95 | + } |
| 96 | + |
| 97 | + fn version(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> { |
| 98 | + "v1".into() |
| 99 | + } |
| 100 | + |
| 101 | + fn plural(_dyntype: &Self::DynamicType) -> std::borrow::Cow<'_, str> { |
| 102 | + "testresources".into() |
| 103 | + } |
| 104 | + |
| 105 | + fn name(&self) -> Option<std::borrow::Cow<'_, str>> { |
| 106 | + self.name.map(std::borrow::Cow::Borrowed) |
| 107 | + } |
| 108 | + |
| 109 | + fn namespace(&self) -> Option<std::borrow::Cow<'_, str>> { |
| 110 | + self.namespace.map(std::borrow::Cow::Borrowed) |
| 111 | + } |
| 112 | + |
| 113 | + fn resource_version(&self) -> Option<std::borrow::Cow<'_, str>> { |
| 114 | + Some("1".into()) |
| 115 | + } |
| 116 | + |
| 117 | + fn uid(&self) -> Option<std::borrow::Cow<'_, str>> { |
| 118 | + self.uid.map(std::borrow::Cow::Borrowed) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_get_cluster_name() { |
| 124 | + assert_eq!( |
| 125 | + ClusterName::from_str_unsafe("test-cluster"), |
| 126 | + get_cluster_name(&TestResource { |
| 127 | + name: Some("test-cluster"), |
| 128 | + ..TestResource::default() |
| 129 | + }) |
| 130 | + .expect("should contain a valid cluster name") |
| 131 | + ); |
| 132 | + |
| 133 | + assert_eq!( |
| 134 | + Err(ErrorDiscriminants::GetClusterName), |
| 135 | + get_cluster_name(&TestResource { |
| 136 | + name: None, |
| 137 | + ..TestResource::default() |
| 138 | + }) |
| 139 | + .map_err(ErrorDiscriminants::from) |
| 140 | + ); |
| 141 | + |
| 142 | + assert_eq!( |
| 143 | + Err(ErrorDiscriminants::ParseClusterName), |
| 144 | + get_cluster_name(&TestResource { |
| 145 | + name: Some("invalid cluster name"), |
| 146 | + ..TestResource::default() |
| 147 | + }) |
| 148 | + .map_err(ErrorDiscriminants::from) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_get_namespace() { |
| 154 | + assert_eq!( |
| 155 | + NamespaceName::from_str_unsafe("test-namespace"), |
| 156 | + get_namespace(&TestResource { |
| 157 | + namespace: Some("test-namespace"), |
| 158 | + ..TestResource::default() |
| 159 | + }) |
| 160 | + .expect("should contain a valid namespace") |
| 161 | + ); |
| 162 | + |
| 163 | + assert_eq!( |
| 164 | + Err(ErrorDiscriminants::GetNamespace), |
| 165 | + get_namespace(&TestResource { |
| 166 | + namespace: None, |
| 167 | + ..TestResource::default() |
| 168 | + }) |
| 169 | + .map_err(ErrorDiscriminants::from) |
| 170 | + ); |
| 171 | + |
| 172 | + assert_eq!( |
| 173 | + Err(ErrorDiscriminants::ParseNamespace), |
| 174 | + get_namespace(&TestResource { |
| 175 | + namespace: Some("invalid namespace"), |
| 176 | + ..TestResource::default() |
| 177 | + }) |
| 178 | + .map_err(ErrorDiscriminants::from) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn test_get_uid() { |
| 184 | + assert_eq!( |
| 185 | + Uid::from(uuid!("e6ac237d-a6d4-43a1-8135-f36506110912")), |
| 186 | + get_uid(&TestResource { |
| 187 | + uid: Some("e6ac237d-a6d4-43a1-8135-f36506110912"), |
| 188 | + ..TestResource::default() |
| 189 | + }) |
| 190 | + .expect("should contain a valid UID") |
| 191 | + ); |
| 192 | + |
| 193 | + assert_eq!( |
| 194 | + Err(ErrorDiscriminants::GetUid), |
| 195 | + get_uid(&TestResource { |
| 196 | + uid: None, |
| 197 | + ..TestResource::default() |
| 198 | + }) |
| 199 | + .map_err(ErrorDiscriminants::from) |
| 200 | + ); |
| 201 | + |
| 202 | + assert_eq!( |
| 203 | + Err(ErrorDiscriminants::ParseUid), |
| 204 | + get_uid(&TestResource { |
| 205 | + uid: Some("invalid UID"), |
| 206 | + ..TestResource::default() |
| 207 | + }) |
| 208 | + .map_err(ErrorDiscriminants::from) |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
0 commit comments