|
| 1 | +use stackable_operator::{ |
| 2 | + builder::pdb::PodDisruptionBudgetBuilder, |
| 3 | + k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector, |
| 4 | + kube::{Resource, api::ObjectMeta}, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::framework::{ |
| 8 | + HasName, HasUid, NameIsValidLabelValue, |
| 9 | + types::operator::{ControllerName, OperatorName, ProductName, RoleName}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Infallible variant of |
| 13 | +/// [`stackable_operator::builder::pdb::PodDisruptionBudgetBuilder::new_with_role`] |
| 14 | +pub fn pod_disruption_budget_builder_with_role( |
| 15 | + owner: &(impl Resource<DynamicType = ()> + HasName + NameIsValidLabelValue + HasUid), |
| 16 | + product_name: &ProductName, |
| 17 | + role_name: &RoleName, |
| 18 | + operator_name: &OperatorName, |
| 19 | + controller_name: &ControllerName, |
| 20 | +) -> PodDisruptionBudgetBuilder<ObjectMeta, LabelSelector, ()> { |
| 21 | + PodDisruptionBudgetBuilder::new_with_role( |
| 22 | + owner, |
| 23 | + &product_name.to_label_value(), |
| 24 | + &role_name.to_label_value(), |
| 25 | + &operator_name.to_label_value(), |
| 26 | + &controller_name.to_label_value(), |
| 27 | + ) |
| 28 | + .expect( |
| 29 | + "PodDisruptionBudgetBuilder should be created because the owner has an object name and UID \ |
| 30 | + and all given parameters produce valid label values.", |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg(test)] |
| 35 | +mod tests { |
| 36 | + use std::borrow::Cow; |
| 37 | + |
| 38 | + use stackable_operator::{ |
| 39 | + k8s_openapi::{ |
| 40 | + api::policy::v1::{PodDisruptionBudget, PodDisruptionBudgetSpec}, |
| 41 | + apimachinery::pkg::{ |
| 42 | + apis::meta::v1::{LabelSelector, ObjectMeta, OwnerReference}, |
| 43 | + util::intstr::IntOrString, |
| 44 | + }, |
| 45 | + }, |
| 46 | + kube::Resource, |
| 47 | + }; |
| 48 | + |
| 49 | + use crate::framework::{ |
| 50 | + HasName, HasUid, NameIsValidLabelValue, |
| 51 | + builder::pdb::pod_disruption_budget_builder_with_role, |
| 52 | + types::{ |
| 53 | + kubernetes::Uid, |
| 54 | + operator::{ControllerName, OperatorName, ProductName, RoleName}, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + struct Cluster { |
| 59 | + object_meta: ObjectMeta, |
| 60 | + } |
| 61 | + |
| 62 | + impl Cluster { |
| 63 | + fn new() -> Self { |
| 64 | + Cluster { |
| 65 | + object_meta: ObjectMeta { |
| 66 | + name: Some("cluster-name".to_owned()), |
| 67 | + uid: Some("a6b89911-d48e-4328-88d6-b9251226583d".to_owned()), |
| 68 | + ..ObjectMeta::default() |
| 69 | + }, |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + impl Resource for Cluster { |
| 75 | + type DynamicType = (); |
| 76 | + type Scope = (); |
| 77 | + |
| 78 | + fn kind(_dt: &Self::DynamicType) -> Cow<'_, str> { |
| 79 | + Cow::from("kind") |
| 80 | + } |
| 81 | + |
| 82 | + fn group(_dt: &Self::DynamicType) -> Cow<'_, str> { |
| 83 | + Cow::from("group") |
| 84 | + } |
| 85 | + |
| 86 | + fn version(_dt: &Self::DynamicType) -> Cow<'_, str> { |
| 87 | + Cow::from("version") |
| 88 | + } |
| 89 | + |
| 90 | + fn plural(_dt: &Self::DynamicType) -> Cow<'_, str> { |
| 91 | + Cow::from("plural") |
| 92 | + } |
| 93 | + |
| 94 | + fn meta(&self) -> &ObjectMeta { |
| 95 | + &self.object_meta |
| 96 | + } |
| 97 | + |
| 98 | + fn meta_mut(&mut self) -> &mut ObjectMeta { |
| 99 | + &mut self.object_meta |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + impl HasName for Cluster { |
| 104 | + fn to_name(&self) -> String { |
| 105 | + self.object_meta |
| 106 | + .name |
| 107 | + .clone() |
| 108 | + .expect("should be set in Cluster::new") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + impl HasUid for Cluster { |
| 113 | + fn to_uid(&self) -> Uid { |
| 114 | + Uid::from_str_unsafe( |
| 115 | + &self |
| 116 | + .object_meta |
| 117 | + .uid |
| 118 | + .clone() |
| 119 | + .expect("should be set in Cluster::new"), |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + impl NameIsValidLabelValue for Cluster { |
| 125 | + fn to_label_value(&self) -> String { |
| 126 | + self.object_meta |
| 127 | + .name |
| 128 | + .clone() |
| 129 | + .expect("should be set in Cluster::new") |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_pod_disruption_budget_builder_with_role() { |
| 135 | + let actual_pdb = pod_disruption_budget_builder_with_role( |
| 136 | + &Cluster::new(), |
| 137 | + &ProductName::from_str_unsafe("my-product"), |
| 138 | + &RoleName::from_str_unsafe("my-role"), |
| 139 | + &OperatorName::from_str_unsafe("my-operator"), |
| 140 | + &ControllerName::from_str_unsafe("my-controller"), |
| 141 | + ) |
| 142 | + .with_max_unavailable(2) |
| 143 | + .build(); |
| 144 | + |
| 145 | + let expected_pdb = PodDisruptionBudget { |
| 146 | + metadata: ObjectMeta { |
| 147 | + labels: Some( |
| 148 | + [ |
| 149 | + ("app.kubernetes.io/component", "my-role"), |
| 150 | + ("app.kubernetes.io/instance", "cluster-name"), |
| 151 | + ("app.kubernetes.io/managed-by", "my-operator_my-controller"), |
| 152 | + ("app.kubernetes.io/name", "my-product"), |
| 153 | + ] |
| 154 | + .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| 155 | + .into(), |
| 156 | + ), |
| 157 | + name: Some("cluster-name-my-role".to_owned()), |
| 158 | + owner_references: Some(vec![OwnerReference { |
| 159 | + api_version: "group/version".to_owned(), |
| 160 | + controller: Some(true), |
| 161 | + kind: "kind".to_owned(), |
| 162 | + name: "cluster-name".to_owned(), |
| 163 | + uid: "a6b89911-d48e-4328-88d6-b9251226583d".to_owned(), |
| 164 | + ..OwnerReference::default() |
| 165 | + }]), |
| 166 | + ..ObjectMeta::default() |
| 167 | + }, |
| 168 | + spec: Some(PodDisruptionBudgetSpec { |
| 169 | + max_unavailable: Some(IntOrString::Int(2)), |
| 170 | + selector: Some(LabelSelector { |
| 171 | + match_labels: Some( |
| 172 | + [ |
| 173 | + ("app.kubernetes.io/component", "my-role"), |
| 174 | + ("app.kubernetes.io/instance", "cluster-name"), |
| 175 | + ("app.kubernetes.io/name", "my-product"), |
| 176 | + ] |
| 177 | + .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| 178 | + .into(), |
| 179 | + ), |
| 180 | + ..LabelSelector::default() |
| 181 | + }), |
| 182 | + ..PodDisruptionBudgetSpec::default() |
| 183 | + }), |
| 184 | + ..PodDisruptionBudget::default() |
| 185 | + }; |
| 186 | + |
| 187 | + assert_eq!(expected_pdb, actual_pdb); |
| 188 | + } |
| 189 | +} |
0 commit comments