1use k8s_openapi::api::apps::v1::Deployment;
9use k8s_openapi::api::core::v1::Service;
10use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, Time};
11use k8s_openapi::jiff::Timestamp;
12use kaniop_k8s_util::error::{Error, Result};
13use kube::api::{Api, Patch};
14use kube::{Client, Resource, ResourceExt};
15use serde_json::json;
16use time::OffsetDateTime;
17use uuid::Uuid;
18
19use crate::crd::{KanidmRadiusServer, KanidmRadiusServerStatus};
20use crate::reconcile::apply::apply_params;
21use crate::reconcile::error::{Failure, RadiusBlock};
22
23pub const TYPE_IDENTITY_READY: &str = "IdentityReady";
24pub const TYPE_CONFIGURATION_READY: &str = "ConfigurationReady";
25pub const TYPE_AVAILABLE: &str = "Available";
26pub const TYPE_EXPOSED: &str = "Exposed";
27pub const TYPE_ROTATING: &str = "Rotating";
28
29const MAX_MESSAGE_LEN: usize = 512;
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum ConditionKind {
35 IdentityReady,
36 ConfigurationReady,
37 Available,
38 Exposed,
39 Rotating,
40}
41
42impl ConditionKind {
43 pub const ALL: [ConditionKind; 5] = [
44 Self::IdentityReady,
45 Self::ConfigurationReady,
46 Self::Available,
47 Self::Exposed,
48 Self::Rotating,
49 ];
50
51 pub fn type_name(self) -> &'static str {
52 match self {
53 Self::IdentityReady => TYPE_IDENTITY_READY,
54 Self::ConfigurationReady => TYPE_CONFIGURATION_READY,
55 Self::Available => TYPE_AVAILABLE,
56 Self::Exposed => TYPE_EXPOSED,
57 Self::Rotating => TYPE_ROTATING,
58 }
59 }
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum Truth {
65 True,
66 False,
67 Unknown,
68}
69
70impl Truth {
71 pub fn as_str(self) -> &'static str {
72 match self {
73 Self::True => "True",
74 Self::False => "False",
75 Self::Unknown => "Unknown",
76 }
77 }
78}
79
80#[derive(Clone, Debug, PartialEq, Eq)]
82pub struct Verdict {
83 pub truth: Truth,
84 pub reason: String,
85 pub message: String,
86}
87
88impl Verdict {
89 pub fn new(truth: Truth, reason: &str, message: impl Into<String>) -> Self {
90 Self {
91 truth,
92 reason: reason.to_string(),
93 message: message.into(),
94 }
95 }
96
97 pub fn yes(reason: &str, message: impl Into<String>) -> Self {
98 Self::new(Truth::True, reason, message)
99 }
100
101 pub fn no(reason: &str, message: impl Into<String>) -> Self {
102 Self::new(Truth::False, reason, message)
103 }
104
105 pub fn unknown(reason: &str, message: impl Into<String>) -> Self {
106 Self::new(Truth::Unknown, reason, message)
107 }
108
109 pub fn not_evaluated() -> Self {
111 Self::unknown(
112 "NotEvaluated",
113 "not evaluated: an earlier step did not complete",
114 )
115 }
116}
117
118#[derive(Clone, Debug, Default, PartialEq, Eq)]
120pub struct TokenFacts {
121 pub current: Option<(Uuid, OffsetDateTime)>,
123 pub current_expiry: Option<OffsetDateTime>,
125 pub config_secret_name: Option<String>,
126 pub retiring: Vec<Uuid>,
127}
128
129#[derive(Clone, Debug, PartialEq, Eq)]
134pub struct StatusReport {
135 pub identity: Verdict,
136 pub configuration: Verdict,
137 pub rotating: Verdict,
138 pub available: Option<Verdict>,
139 pub exposed: Option<Verdict>,
140 pub tokens: Option<TokenFacts>,
141}
142
143impl Default for StatusReport {
144 fn default() -> Self {
145 Self {
146 identity: Verdict::not_evaluated(),
147 configuration: Verdict::not_evaluated(),
148 rotating: Verdict::not_evaluated(),
149 available: None,
150 exposed: None,
151 tokens: None,
152 }
153 }
154}
155
156impl StatusReport {
157 pub fn record_failure(&mut self, failure: &Failure) {
160 match failure {
161 Failure::Block(block) => self.set(block.condition(), block_verdict(block)),
162 Failure::Transient { condition, error } => self.set(
163 *condition,
164 Verdict::unknown("TransientError", bounded(error.to_string())),
165 ),
166 }
167 }
168
169 fn set(&mut self, kind: ConditionKind, verdict: Verdict) {
170 match kind {
171 ConditionKind::IdentityReady => self.identity = verdict,
172 ConditionKind::ConfigurationReady => self.configuration = verdict,
173 ConditionKind::Rotating => self.rotating = verdict,
174 ConditionKind::Available => self.available = Some(verdict),
175 ConditionKind::Exposed => self.exposed = Some(verdict),
176 }
177 }
178}
179
180pub fn block_verdict(block: &RadiusBlock) -> Verdict {
182 let truth = match block.condition() {
185 ConditionKind::Rotating => Truth::True,
186 _ => Truth::False,
187 };
188 Verdict::new(truth, block.reason(), bounded(block.to_string()))
189}
190
191fn bounded(mut message: String) -> String {
192 if message.len() > MAX_MESSAGE_LEN {
193 let mut end = MAX_MESSAGE_LEN - 3;
194 while !message.is_char_boundary(end) {
195 end -= 1;
196 }
197 message.truncate(end);
198 message.push_str("...");
199 }
200 message
201}
202
203pub fn available_verdict(deployment: Option<&Deployment>) -> Verdict {
206 let Some(deployment) = deployment else {
207 return Verdict::no("DeploymentMissing", "the RADIUS Deployment does not exist");
208 };
209 let desired = deployment
210 .spec
211 .as_ref()
212 .and_then(|s| s.replicas)
213 .unwrap_or(1);
214 let status = deployment.status.clone().unwrap_or_default();
215 let stuck = status.conditions.iter().flatten().any(|c| {
216 c.type_ == "Progressing"
217 && c.status == "False"
218 && c.reason.as_deref() == Some("ProgressDeadlineExceeded")
219 });
220 if stuck {
221 return Verdict::no(
222 "ProgressDeadlineExceeded",
223 "the Deployment rollout exceeded its progress deadline",
224 );
225 }
226 let observed = match (deployment.metadata.generation, status.observed_generation) {
227 (Some(generation), Some(observed)) => observed >= generation,
228 (Some(_), None) => false,
229 (None, _) => true,
230 };
231 let total = status.replicas.unwrap_or(0);
232 let updated = status.updated_replicas.unwrap_or(0);
233 let available = status.available_replicas.unwrap_or(0);
234 if !observed || updated < desired || total > updated {
235 return Verdict::no(
236 "RolloutInProgress",
237 format!("{updated}/{desired} replicas updated, {available} available, {total} total"),
238 );
239 }
240 if available < desired {
241 return Verdict::no(
242 "ReplicasUnavailable",
243 format!("{available}/{desired} replicas available"),
244 );
245 }
246 Verdict::yes(
247 "RolloutComplete",
248 format!("{available}/{desired} replicas available"),
249 )
250}
251
252pub fn exposed_verdict(service: Option<&Service>) -> (Verdict, Option<String>) {
254 let Some(spec) = service.and_then(|s| s.spec.as_ref()) else {
255 return (
256 Verdict::no("ServiceMissing", "the RADIUS Service does not exist"),
257 None,
258 );
259 };
260 let port = spec.ports.iter().flatten().next();
261 let service_port = port.map_or(0, |p| p.port);
262 match spec.type_.as_deref().unwrap_or("ClusterIP") {
263 "LoadBalancer" => {
264 let address = service
265 .and_then(|s| s.status.as_ref())
266 .and_then(|s| s.load_balancer.as_ref())
267 .and_then(|lb| lb.ingress.as_ref())
268 .and_then(|ingress| {
269 ingress
270 .iter()
271 .find_map(|i| i.ip.clone().or_else(|| i.hostname.clone()))
272 });
273 match address {
274 Some(address) => (
275 Verdict::yes(
276 "LoadBalancerReady",
277 format!("load balancer address {address}"),
278 ),
279 Some(host_port(&address, service_port)),
280 ),
281 None => (
282 Verdict::no(
283 "LoadBalancerPending",
284 "waiting for the load balancer to assign an address",
285 ),
286 None,
287 ),
288 }
289 }
290 "NodePort" => match port.and_then(|p| p.node_port) {
291 Some(node_port) => (
292 Verdict::yes(
293 "NodePortAssigned",
294 format!("node port {node_port} on every node"),
295 ),
296 Some(format!("*:{node_port}")),
297 ),
298 None => (
299 Verdict::no("NodePortPending", "waiting for a node port to be assigned"),
300 None,
301 ),
302 },
303 _ => match spec
304 .cluster_ip
305 .as_deref()
306 .filter(|ip| !ip.is_empty() && *ip != "None")
307 {
308 Some(ip) => (
309 Verdict::yes("ClusterIPAssigned", format!("cluster IP {ip}")),
310 Some(host_port(ip, service_port)),
311 ),
312 None => (
313 Verdict::no("ClusterIPPending", "waiting for a cluster IP"),
314 None,
315 ),
316 },
317 }
318}
319
320fn host_port(host: &str, port: i32) -> String {
322 if host.contains(':') {
323 format!("[{host}]:{port}")
324 } else {
325 format!("{host}:{port}")
326 }
327}
328
329pub fn is_ready(conditions: &[Condition]) -> bool {
332 [
333 TYPE_IDENTITY_READY,
334 TYPE_CONFIGURATION_READY,
335 TYPE_AVAILABLE,
336 TYPE_EXPOSED,
337 ]
338 .iter()
339 .all(|type_| {
340 conditions
341 .iter()
342 .any(|c| c.type_ == *type_ && c.status == Truth::True.as_str())
343 })
344}
345
346pub fn kube_time(at: OffsetDateTime) -> Option<Time> {
348 let nanos = i32::try_from(at.nanosecond()).ok()?;
349 Timestamp::new(at.unix_timestamp(), nanos).ok().map(Time)
350}
351
352pub fn build_status(
355 generation: Option<i64>,
356 previous: Option<&KanidmRadiusServerStatus>,
357 report: &StatusReport,
358 deployment: Option<&Deployment>,
359 service: Option<&Service>,
360 now: &Time,
361) -> KanidmRadiusServerStatus {
362 let (exposed, endpoint) = exposed_verdict(service);
363 let available = report
364 .available
365 .clone()
366 .unwrap_or_else(|| available_verdict(deployment));
367 let exposed = report.exposed.clone().unwrap_or(exposed);
368 let previous_conditions = previous
369 .and_then(|p| p.conditions.as_deref())
370 .unwrap_or(&[]);
371 let conditions: Vec<Condition> = ConditionKind::ALL
372 .iter()
373 .map(|kind| {
374 let verdict = match kind {
375 ConditionKind::IdentityReady => &report.identity,
376 ConditionKind::ConfigurationReady => &report.configuration,
377 ConditionKind::Available => &available,
378 ConditionKind::Exposed => &exposed,
379 ConditionKind::Rotating => &report.rotating,
380 };
381 let type_ = kind.type_name();
382 let status = verdict.truth.as_str();
383 let last_transition_time = previous_conditions
385 .iter()
386 .find(|c| c.type_ == type_ && c.status == status)
387 .map_or_else(|| now.clone(), |c| c.last_transition_time.clone());
388 Condition {
389 type_: type_.to_string(),
390 status: status.to_string(),
391 reason: verdict.reason.clone(),
392 message: verdict.message.clone(),
393 last_transition_time,
394 observed_generation: generation,
395 }
396 })
397 .collect();
398
399 let (token_id, token_issued_at, config_secret_name, retiring_token_ids) = match &report.tokens {
400 Some(facts) => (
401 facts.current.map(|(id, _)| id.to_string()),
402 facts.current.and_then(|(_, at)| kube_time(at)),
403 facts.config_secret_name.clone(),
404 facts.retiring.iter().map(Uuid::to_string).collect(),
405 ),
406 None => (
407 previous.and_then(|p| p.token_id.clone()),
408 previous.and_then(|p| p.token_issued_at.clone()),
409 previous.and_then(|p| p.config_secret_name.clone()),
410 previous
411 .map(|p| p.retiring_token_ids.clone())
412 .unwrap_or_default(),
413 ),
414 };
415
416 KanidmRadiusServerStatus {
417 ready: is_ready(&conditions),
418 conditions: Some(conditions),
419 observed_generation: generation,
420 endpoint,
421 token_id,
422 token_issued_at,
423 config_secret_name,
424 deployment_name: deployment.map(ResourceExt::name_any),
425 service_name: service.map(ResourceExt::name_any),
426 replicas: deployment
427 .and_then(|d| d.spec.as_ref())
428 .and_then(|s| s.replicas),
429 ready_replicas: deployment
430 .and_then(|d| d.status.as_ref())
431 .and_then(|s| s.ready_replicas),
432 retiring_token_ids,
433 }
434}
435
436pub async fn patch_status(
439 client: Client,
440 radius: &KanidmRadiusServer,
441 status: KanidmRadiusServerStatus,
442) -> Result<bool> {
443 if radius.status.as_ref() == Some(&status) {
444 return Ok(false);
445 }
446 let namespace = radius.namespace().unwrap_or_default();
447 let name = radius.name_any();
448 let patch = Patch::Apply(json!({
449 "apiVersion": KanidmRadiusServer::api_version(&()),
450 "kind": KanidmRadiusServer::kind(&()),
451 "status": status,
452 }));
453 Api::<KanidmRadiusServer>::namespaced(client, &namespace)
454 .patch_status(&name, &apply_params(), &patch)
455 .await
456 .map_err(|e| Error::kube_status_error("KanidmRadiusServer", &namespace, name, e))?;
457 Ok(true)
458}
459
460#[cfg(test)]
461mod tests {
462 use k8s_openapi::api::apps::v1::{DeploymentCondition, DeploymentSpec, DeploymentStatus};
463 use k8s_openapi::api::core::v1::{
464 LoadBalancerIngress, LoadBalancerStatus, ServicePort, ServiceSpec, ServiceStatus,
465 };
466 use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
467
468 use super::*;
469 use crate::reconcile::error::ExecError;
470 use crate::reconcile::inputs::RadiusPreflightError;
471
472 const TOKEN: Uuid = Uuid::from_u128(0xa);
473 const OLD_TOKEN: Uuid = Uuid::from_u128(0xb);
474
475 fn at(seconds: i64) -> Time {
476 Time(Timestamp::from_second(seconds).unwrap())
477 }
478
479 fn deployment(desired: i32, updated: i32, total: i32, available: i32) -> Deployment {
480 Deployment {
481 metadata: ObjectMeta {
482 name: Some("campus".into()),
483 generation: Some(3),
484 ..Default::default()
485 },
486 spec: Some(DeploymentSpec {
487 replicas: Some(desired),
488 ..Default::default()
489 }),
490 status: Some(DeploymentStatus {
491 observed_generation: Some(3),
492 replicas: Some(total),
493 updated_replicas: Some(updated),
494 available_replicas: Some(available),
495 ready_replicas: Some(available),
496 ..Default::default()
497 }),
498 }
499 }
500
501 fn healthy_deployment() -> Deployment {
502 deployment(2, 2, 2, 2)
503 }
504
505 fn service(type_: &str) -> Service {
506 Service {
507 metadata: ObjectMeta {
508 name: Some("campus".into()),
509 ..Default::default()
510 },
511 spec: Some(ServiceSpec {
512 type_: Some(type_.into()),
513 cluster_ip: Some("10.96.0.10".into()),
514 ports: Some(vec![ServicePort {
515 port: 1812,
516 node_port: (type_ != "ClusterIP").then_some(31812),
517 protocol: Some("UDP".into()),
518 ..Default::default()
519 }]),
520 ..Default::default()
521 }),
522 status: None,
523 }
524 }
525
526 fn load_balancer(ingress: Vec<LoadBalancerIngress>) -> Service {
527 Service {
528 status: Some(ServiceStatus {
529 load_balancer: Some(LoadBalancerStatus {
530 ingress: Some(ingress),
531 }),
532 ..Default::default()
533 }),
534 ..service("LoadBalancer")
535 }
536 }
537
538 fn all_good() -> StatusReport {
539 StatusReport {
540 identity: Verdict::yes("AccountReady", "ok"),
541 configuration: Verdict::yes("ConfigurationValid", "ok"),
542 rotating: Verdict::no("Idle", "ok"),
543 available: None,
544 exposed: None,
545 tokens: Some(TokenFacts {
546 current: Some((TOKEN, OffsetDateTime::from_unix_timestamp(1_000).unwrap())),
547 current_expiry: None,
548 config_secret_name: Some("campus-0123456789".into()),
549 retiring: vec![],
550 }),
551 }
552 }
553
554 fn condition<'a>(status: &'a KanidmRadiusServerStatus, type_: &str) -> &'a Condition {
555 status
556 .conditions
557 .as_ref()
558 .unwrap()
559 .iter()
560 .find(|c| c.type_ == type_)
561 .unwrap_or_else(|| panic!("{type_} missing"))
562 }
563
564 fn build(
565 report: &StatusReport,
566 deployment: Option<&Deployment>,
567 service: Option<&Service>,
568 ) -> KanidmRadiusServerStatus {
569 build_status(Some(7), None, report, deployment, service, &at(100))
570 }
571
572 #[test]
573 fn always_emits_all_five_conditions_in_order() {
574 let cases = [
575 (StatusReport::default(), None, None),
576 (
577 all_good(),
578 Some(healthy_deployment()),
579 Some(service("ClusterIP")),
580 ),
581 ];
582 for (report, deployment, service) in cases {
583 let status = build(&report, deployment.as_ref(), service.as_ref());
584 let types: Vec<_> = status
585 .conditions
586 .as_ref()
587 .unwrap()
588 .iter()
589 .map(|c| c.type_.as_str())
590 .collect();
591 assert_eq!(
592 types,
593 [
594 "IdentityReady",
595 "ConfigurationReady",
596 "Available",
597 "Exposed",
598 "Rotating"
599 ]
600 );
601 for c in status.conditions.as_ref().unwrap() {
602 assert_eq!(c.observed_generation, Some(7));
603 assert!(!c.reason.is_empty());
604 }
605 assert_eq!(status.observed_generation, Some(7));
606 }
607 }
608
609 #[test]
610 fn healthy_world_is_ready_and_reports_fields() {
611 let deployment = healthy_deployment();
612 let service = service("ClusterIP");
613 let status = build(&all_good(), Some(&deployment), Some(&service));
614 assert!(status.ready);
615 assert_eq!(status.token_id.as_deref(), Some(TOKEN.to_string().as_str()));
616 assert_eq!(status.token_issued_at, Some(at(1_000)));
617 assert_eq!(
618 status.config_secret_name.as_deref(),
619 Some("campus-0123456789")
620 );
621 assert_eq!(status.deployment_name.as_deref(), Some("campus"));
622 assert_eq!(status.service_name.as_deref(), Some("campus"));
623 assert_eq!(status.replicas, Some(2));
624 assert_eq!(status.ready_replicas, Some(2));
625 assert_eq!(status.endpoint.as_deref(), Some("10.96.0.10:1812"));
626 assert!(status.retiring_token_ids.is_empty());
627 }
628
629 #[test]
630 fn last_transition_time_is_kept_while_status_is_unchanged() {
631 let deployment = healthy_deployment();
632 let service = service("ClusterIP");
633 let first = build_status(
634 Some(7),
635 None,
636 &all_good(),
637 Some(&deployment),
638 Some(&service),
639 &at(100),
640 );
641 let second = build_status(
642 Some(7),
643 Some(&first),
644 &all_good(),
645 Some(&deployment),
646 Some(&service),
647 &at(200),
648 );
649 assert_eq!(
650 first, second,
651 "an unchanged world must give an identical status"
652 );
653
654 let mut report = all_good();
656 report.rotating = Verdict::no("Idle", "different message");
657 let third = build_status(
658 Some(7),
659 Some(&second),
660 &report,
661 Some(&deployment),
662 Some(&service),
663 &at(300),
664 );
665 assert_eq!(
666 condition(&third, TYPE_ROTATING).last_transition_time,
667 at(100)
668 );
669
670 report.rotating = Verdict::yes("Rotating", "minted");
672 let fourth = build_status(
673 Some(7),
674 Some(&third),
675 &report,
676 Some(&deployment),
677 Some(&service),
678 &at(400),
679 );
680 assert_eq!(
681 condition(&fourth, TYPE_ROTATING).last_transition_time,
682 at(400)
683 );
684 assert_eq!(
685 condition(&fourth, TYPE_AVAILABLE).last_transition_time,
686 at(100)
687 );
688 assert_eq!(
689 condition(&fourth, TYPE_IDENTITY_READY).last_transition_time,
690 at(100)
691 );
692 }
693
694 #[test]
695 fn ready_follows_the_condition_matrix() {
696 let deployment = healthy_deployment();
697 let service = service("ClusterIP");
698 let base = build(&all_good(), Some(&deployment), Some(&service));
699 assert!(base.ready);
700
701 let mut rotating = all_good();
703 rotating.rotating = Verdict::yes("Rotating", "retiring");
704 assert!(build(&rotating, Some(&deployment), Some(&service)).ready);
705
706 for kind in [
707 ConditionKind::IdentityReady,
708 ConditionKind::ConfigurationReady,
709 ConditionKind::Available,
710 ConditionKind::Exposed,
711 ] {
712 for truth in [Truth::False, Truth::Unknown] {
713 let mut report = all_good();
714 report.set(kind, Verdict::new(truth, "Blocked", "x"));
715 let status = build(&report, Some(&deployment), Some(&service));
716 assert!(!status.ready, "{kind:?}={truth:?} must not be ready");
717 assert!(!is_ready(status.conditions.as_ref().unwrap()));
718 }
719 }
720 assert!(!build(&all_good(), None, Some(&service)).ready);
722 assert!(!build(&all_good(), Some(&deployment), None).ready);
723 }
724
725 #[test]
726 fn available_requires_a_complete_rollout() {
727 let cases = [
728 (Some(deployment(2, 2, 2, 2)), Truth::True, "RolloutComplete"),
729 (
731 Some(deployment(2, 1, 3, 2)),
732 Truth::False,
733 "RolloutInProgress",
734 ),
735 (
737 Some(deployment(2, 2, 3, 2)),
738 Truth::False,
739 "RolloutInProgress",
740 ),
741 (
742 Some(deployment(2, 2, 2, 1)),
743 Truth::False,
744 "ReplicasUnavailable",
745 ),
746 (None, Truth::False, "DeploymentMissing"),
747 ];
748 for (deployment, truth, reason) in cases {
749 let verdict = available_verdict(deployment.as_ref());
750 assert_eq!((verdict.truth, verdict.reason.as_str()), (truth, reason));
751 }
752
753 let mut stale = deployment(2, 2, 2, 2);
755 stale.status.as_mut().unwrap().observed_generation = Some(2);
756 assert_eq!(available_verdict(Some(&stale)).reason, "RolloutInProgress");
757
758 let mut stuck = deployment(2, 1, 3, 2);
759 stuck.status.as_mut().unwrap().conditions = Some(vec![DeploymentCondition {
760 type_: "Progressing".into(),
761 status: "False".into(),
762 reason: Some("ProgressDeadlineExceeded".into()),
763 ..Default::default()
764 }]);
765 assert_eq!(
766 available_verdict(Some(&stuck)).reason,
767 "ProgressDeadlineExceeded"
768 );
769 }
770
771 #[test]
772 fn endpoint_per_service_type() {
773 let (verdict, endpoint) = exposed_verdict(Some(&service("ClusterIP")));
774 assert_eq!(verdict.truth, Truth::True);
775 assert_eq!(endpoint.as_deref(), Some("10.96.0.10:1812"));
776
777 let (verdict, endpoint) = exposed_verdict(Some(&service("NodePort")));
778 assert_eq!(verdict.truth, Truth::True);
779 assert_eq!(endpoint.as_deref(), Some("*:31812"));
780
781 let (verdict, endpoint) = exposed_verdict(Some(&service("LoadBalancer")));
784 assert_eq!(
785 (verdict.truth, verdict.reason.as_str()),
786 (Truth::False, "LoadBalancerPending")
787 );
788 assert_eq!(endpoint, None);
789 let (verdict, endpoint) = exposed_verdict(Some(&load_balancer(vec![])));
790 assert_eq!(verdict.truth, Truth::False);
791 assert_eq!(endpoint, None);
792
793 let (verdict, endpoint) =
794 exposed_verdict(Some(&load_balancer(vec![LoadBalancerIngress {
795 ip: Some("203.0.113.7".into()),
796 ..Default::default()
797 }])));
798 assert_eq!(verdict.truth, Truth::True);
799 assert_eq!(endpoint.as_deref(), Some("203.0.113.7:1812"));
800
801 let (verdict, endpoint) =
802 exposed_verdict(Some(&load_balancer(vec![LoadBalancerIngress {
803 hostname: Some("radius.example.net".into()),
804 ..Default::default()
805 }])));
806 assert_eq!(verdict.truth, Truth::True);
807 assert_eq!(endpoint.as_deref(), Some("radius.example.net:1812"));
808
809 let (_, endpoint) = exposed_verdict(Some(&load_balancer(vec![LoadBalancerIngress {
810 ip: Some("2001:db8::7".into()),
811 ..Default::default()
812 }])));
813 assert_eq!(endpoint.as_deref(), Some("[2001:db8::7]:1812"));
814
815 let (verdict, endpoint) = exposed_verdict(None);
816 assert_eq!(
817 (verdict.truth, verdict.reason.as_str()),
818 (Truth::False, "ServiceMissing")
819 );
820 assert_eq!(endpoint, None);
821 }
822
823 #[test]
824 fn invalid_input_keeps_available_reflecting_the_running_deployment() {
825 let previous = build(
828 &all_good(),
829 Some(&healthy_deployment()),
830 Some(&service("ClusterIP")),
831 );
832 let mut report = StatusReport::default();
833 report.record_failure(&Failure::Block(RadiusBlock::Inputs(
834 RadiusPreflightError::NasSecretKeyUnavailable,
835 )));
836 let status = build_status(
837 Some(8),
838 Some(&previous),
839 &report,
840 Some(&healthy_deployment()),
841 Some(&service("ClusterIP")),
842 &at(500),
843 );
844 let configuration = condition(&status, TYPE_CONFIGURATION_READY);
845 assert_eq!(configuration.status, "False");
846 assert_eq!(configuration.reason, "NasSecretKeyInvalid");
847 assert_eq!(condition(&status, TYPE_AVAILABLE).status, "True");
848 assert_eq!(condition(&status, TYPE_EXPOSED).status, "True");
849 assert!(!status.ready);
850 assert_eq!(status.token_id, previous.token_id);
852 assert_eq!(status.config_secret_name, previous.config_secret_name);
853 }
854
855 #[test]
856 fn failures_land_on_their_condition() {
857 let cases: Vec<(Failure, ConditionKind, Truth, &str)> = vec![
858 (
859 Failure::exec(ConditionKind::IdentityReady)(ExecError::AccountNotOwned {
860 account: "kaniop_radius_x".into(),
861 }),
862 ConditionKind::IdentityReady,
863 Truth::False,
864 "OwnershipConflict",
865 ),
866 (
867 Failure::exec(ConditionKind::Available)(ExecError::ForeignObject {
868 kind: "Deployment".into(),
869 namespace: "ns".into(),
870 name: "campus".into(),
871 }),
872 ConditionKind::Available,
873 Truth::False,
874 "ForeignObject",
875 ),
876 (
877 Failure::exec(ConditionKind::Exposed)(ExecError::ForeignObject {
878 kind: "Service".into(),
879 namespace: "ns".into(),
880 name: "campus".into(),
881 }),
882 ConditionKind::Exposed,
883 Truth::False,
884 "ForeignObject",
885 ),
886 (
887 Failure::Block(RadiusBlock::TooManyTokens),
888 ConditionKind::Rotating,
889 Truth::True,
890 "TooManyTokens",
891 ),
892 (
895 Failure::exec(ConditionKind::Rotating)(ExecError::ForeignObject {
896 kind: "Deployment".into(),
897 namespace: "ns".into(),
898 name: "campus".into(),
899 }),
900 ConditionKind::Available,
901 Truth::False,
902 "ForeignObject",
903 ),
904 (
906 Failure::exec(ConditionKind::IdentityReady)(ExecError::MissingMetadata {
907 kind: "Entry",
908 field: "uuid",
909 }),
910 ConditionKind::IdentityReady,
911 Truth::False,
912 "MissingMetadata",
913 ),
914 (
915 Failure::exec(ConditionKind::Rotating)(ExecError::ForeignObject {
916 kind: "Secret".into(),
917 namespace: "ns".into(),
918 name: "campus-0a1b2c3d4e".into(),
919 }),
920 ConditionKind::Rotating,
921 Truth::True,
922 "ForeignObject",
923 ),
924 (
925 Failure::exec(ConditionKind::ConfigurationReady)(ExecError::ConcurrentMutation {
926 kind: "Secret".into(),
927 namespace: "ns".into(),
928 name: "campus-0a1b2c3d4e".into(),
929 }),
930 ConditionKind::ConfigurationReady,
931 Truth::False,
932 "ConcurrentMutation",
933 ),
934 (
935 Failure::Transient {
936 condition: ConditionKind::IdentityReady,
937 error: Error::MissingData("kanidm unreachable".into()),
938 },
939 ConditionKind::IdentityReady,
940 Truth::Unknown,
941 "TransientError",
942 ),
943 ];
944 for (failure, kind, truth, reason) in cases {
945 let mut report = all_good();
946 report.record_failure(&failure);
947 let status = build(
948 &report,
949 Some(&healthy_deployment()),
950 Some(&service("ClusterIP")),
951 );
952 let c = condition(&status, kind.type_name());
953 assert_eq!(
954 (c.status.as_str(), c.reason.as_str()),
955 (truth.as_str(), reason),
956 "{failure:?}"
957 );
958 assert!(!c.message.is_empty());
959 }
960 }
961
962 #[test]
963 fn transient_error_keeps_its_source_and_writes_the_condition_first() {
964 let source = kube::Error::Api(
967 kube::core::Status::failure("etcd timeout", "InternalError")
968 .with_code(500)
969 .boxed(),
970 );
971 let failure = Failure::Transient {
972 condition: ConditionKind::ConfigurationReady,
973 error: Error::kube_error("get", "Secret", "ns", "nas", source),
974 };
975 let mut report = all_good();
976 report.record_failure(&failure);
977 let status = build(
978 &report,
979 Some(&healthy_deployment()),
980 Some(&service("ClusterIP")),
981 );
982 let c = condition(&status, TYPE_CONFIGURATION_READY);
983 assert_eq!(
984 (c.status.as_str(), c.reason.as_str()),
985 ("Unknown", "TransientError")
986 );
987 assert!(c.message.contains("etcd timeout"), "{}", c.message);
988 let Failure::Transient { error, .. } = failure else {
989 unreachable!()
990 };
991 assert!(error.is_retryable());
992 assert!(std::error::Error::source(&error).is_some());
993 }
994
995 #[test]
996 fn retiring_tokens_are_reported_by_id() {
997 let mut report = all_good();
998 report.tokens.as_mut().unwrap().retiring = vec![OLD_TOKEN];
999 let status = build(
1000 &report,
1001 Some(&healthy_deployment()),
1002 Some(&service("ClusterIP")),
1003 );
1004 assert_eq!(status.retiring_token_ids, [OLD_TOKEN.to_string()]);
1005 }
1006
1007 #[test]
1008 fn long_transient_messages_are_bounded() {
1009 let mut report = all_good();
1010 report.record_failure(&Failure::Transient {
1011 condition: ConditionKind::Rotating,
1012 error: Error::MissingData("x".repeat(4096)),
1013 });
1014 let status = build(&report, None, None);
1015 assert!(condition(&status, TYPE_ROTATING).message.len() <= MAX_MESSAGE_LEN);
1016 }
1017}