zonefile_crds/lib.rs
1use std::collections::BTreeMap;
2
3use kube::{CustomResource, ResourceExt};
4use kubizone_crds::v1alpha1::ZoneRef;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Label attached to [`Zone`](kubizone_crds::Zone)s as backreferences
9/// to a single downstream [`ZoneFile`] generated from it.
10///
11/// Used by the controller to trigger reconciliation when upstream
12/// zones change.
13pub const TARGET_ZONEFILE_LABEL: &str = "kubi.zone/zonefile";
14
15/// A [`ZoneFile`] references an upstream [`Zone`](kubizone_crds::Zone) and (re)builds
16/// a configmap of the same name, whenever the zone changes, automatically incrementing
17/// serials as necessary.
18#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema, Hash)]
19#[kube(
20 group = "kubi.zone",
21 version = "v1alpha1",
22 kind = "ZoneFile",
23 namespaced
24)]
25#[kube(status = "ZoneFileStatus")]
26//#[kube(printcolumn = r#"{"name":"zone", "jsonPath": ".spec.zoneRef.name", "type": "string"}"#)]
27//#[kube(printcolumn = r#"{"name":"serial", "jsonPath": ".status.serial", "type": "string"}"#)]
28//#[kube(printcolumn = r#"{"name":"hash", "jsonPath": ".status.hash", "type": "string"}"#)]
29#[serde(rename_all = "camelCase")]
30pub struct ZoneFileSpec {
31 /// Reference to a [`Zone`](kubizone_crds::Zone), optionally in a different namespace.
32 pub zone_refs: Vec<ZoneRef>,
33
34 #[serde(default)]
35 pub config_map_name: Option<String>,
36}
37
38impl ZoneFile {
39 /// Retrieve the [`ZoneFile`]'s `zoneRef`, but populate the `namespace` variable,
40 /// if not specified by the zoneref itself.
41 pub fn zone_ref(&self) -> Vec<ZoneRef> {
42 self.spec
43 .zone_refs
44 .iter()
45 .map(|zone_ref| ZoneRef {
46 name: zone_ref.name.clone(),
47 namespace: zone_ref
48 .namespace
49 .as_ref()
50 .or(self.namespace().as_ref())
51 .cloned(),
52 })
53 .collect()
54 }
55}
56
57/// Describes the current state of the [`ZoneFile`], tracks state of
58/// the upstream [`Zone`](kubizone_crds::Zone), to determine when the
59/// output `ConfigMap` should be re-generated.
60#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
61#[serde(rename_all = "camelCase")]
62pub struct ZoneFileStatus {
63 /// Last observed hash of the upstream [`Zone`](kubizone_crds::Zone)
64 ///
65 /// Used by the zonefile controller to trigger configmap rebuilds
66 /// and zone serial rotation.
67 pub hash: BTreeMap<String, String>,
68
69 /// Serial of the latest generated zonefile.
70 ///
71 /// The zonefile controller will automatically increment this value
72 /// whenever the zonefile configmap is rebuilt, in accordance with
73 /// [RFC 1912](https://datatracker.ietf.org/doc/html/rfc1912#section-2.2)
74 pub serial: BTreeMap<String, u32>,
75}