bindy/reconcilers/bind9cluster/config.rs
1// Copyright (c) 2025 Erick Bourgeois, firestoned
2// SPDX-License-Identifier: MIT
3
4//! Cluster `ConfigMap` management for `Bind9Cluster` resources.
5//!
6//! This module handles creating and updating the shared cluster-level
7//! `ConfigMap` that contains BIND9 configuration shared across all instances.
8
9#[allow(clippy::wildcard_imports)]
10use super::types::*;
11
12/// Create or update the shared cluster-level `ConfigMap`.
13///
14/// This `ConfigMap` contains BIND9 configuration that is shared across all instances
15/// in the cluster. It is created from `spec.global` configuration.
16///
17/// If custom `ConfigMap`s are referenced at the cluster level (`spec.common.configMapRefs`),
18/// this function skips creation to avoid conflicts.
19///
20/// # Arguments
21///
22/// * `client` - Kubernetes API client
23/// * `cluster` - The `Bind9Cluster` resource
24///
25/// # Errors
26///
27/// Returns an error if:
28/// - Failed to create or update the `ConfigMap`
29/// - Kubernetes API operations fail
30pub(super) async fn create_or_update_cluster_configmap(
31 client: &Client,
32 cluster: &Bind9Cluster,
33) -> Result<()> {
34 use crate::bind9_resources::build_cluster_configmap;
35
36 let namespace = cluster.namespace().unwrap_or_default();
37 let name = cluster.name_any();
38
39 // Check if custom ConfigMaps are referenced at the cluster level
40 if let Some(refs) = &cluster.spec.common.config_map_refs {
41 if refs.named_conf.is_some() || refs.named_conf_options.is_some() {
42 info!(
43 "Cluster {}/{} uses custom ConfigMaps, skipping cluster ConfigMap creation",
44 namespace, name
45 );
46 return Ok(());
47 }
48 }
49
50 info!(
51 "Creating/updating shared ConfigMap for cluster {}/{}",
52 namespace, name
53 );
54
55 // Build the cluster ConfigMap
56 let configmap = build_cluster_configmap(&name, &namespace, cluster)?;
57
58 let cm_api: Api<ConfigMap> = Api::namespaced(client.clone(), &namespace);
59 let cm_name = format!("{name}-config");
60
61 if (cm_api.get(&cm_name).await).is_ok() {
62 // ConfigMap exists, update it
63 info!("Updating cluster ConfigMap {}/{}", namespace, cm_name);
64 cm_api
65 .replace(&cm_name, &PostParams::default(), &configmap)
66 .await?;
67 } else {
68 // ConfigMap doesn't exist, create it
69 info!("Creating cluster ConfigMap {}/{}", namespace, cm_name);
70 cm_api.create(&PostParams::default(), &configmap).await?;
71 }
72
73 Ok(())
74}
75
76#[cfg(test)]
77#[path = "config_tests.rs"]
78mod config_tests;