Allow name override for generated Fleet cluster (#58)

* Allow name override for generated Fleet cluster

Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>

* Established default agent namespace - fleet-addon-agent

Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>

---------

Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
This commit is contained in:
Danil Grigorev 2024-06-11 12:12:09 +02:00 committed by GitHub
parent 2f85f9e849
commit 030fdb5d54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 0 deletions

View File

@ -25,6 +25,10 @@ spec:
description: Cluster controller settings
nullable: true
properties:
agent_namespace:
description: Namespace selection for the fleet agent
nullable: true
type: string
enabled:
description: |-
Enable Cluster config funtionality.
@ -32,10 +36,24 @@ spec:
This will create Fleet Cluster for each Cluster with the same name. In case the cluster specifies topology.class, the name of the ClusterClass will be added to the Fleet Cluster labels.
nullable: true
type: boolean
naming:
description: Naming settings for the fleet cluster
properties:
prefix:
description: Specify a prefix for the Cluster name, applied to created Fleet cluster
nullable: true
type: string
suffix:
description: Specify a suffix for the Cluster name, applied to created Fleet cluster
nullable: true
type: string
type: object
set_owner_references:
description: Setting to disable setting owner references on the created resources
nullable: true
type: boolean
required:
- naming
type: object
cluster_class:
description: Cluster class controller settings

View File

@ -63,18 +63,100 @@ pub struct ClusterConfig {
/// Setting to disable setting owner references on the created resources
pub set_owner_references: Option<bool>,
/// Naming settings for the fleet cluster
pub naming: NamingStrategy,
/// Namespace selection for the fleet agent
pub agent_namespace: Option<String>,
#[cfg(feature = "agent-initiated")]
/// Prepare initial cluster for agent initiated connection
pub agent_initiated: Option<bool>,
}
/// NamingStrategy is controlling Fleet cluster naming
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, Default)]
pub struct NamingStrategy {
/// Specify a prefix for the Cluster name, applied to created Fleet cluster
pub prefix: Option<String>,
/// Specify a suffix for the Cluster name, applied to created Fleet cluster
pub suffix: Option<String>,
}
impl Default for ClusterConfig {
fn default() -> Self {
Self {
set_owner_references: Some(true),
naming: Default::default(),
agent_namespace: "fleet-addon-agent".to_string().into(),
enabled: Some(true),
#[cfg(feature = "agent-initiated")]
agent_initiated: Some(true),
}
}
}
impl NamingStrategy {
pub fn apply(&self, name: Option<String>) -> Option<String> {
name.map(|name| match &self.prefix {
Some(prefix) => prefix.clone() + &name,
None => name,
})
.map(|name| match &self.suffix {
Some(suffix) => name + &suffix,
None => name,
})
}
}
#[cfg(test)]
mod tests {
use crate::api::fleet_addon_config::NamingStrategy;
#[tokio::test]
async fn test_naming_strategy() {
assert_eq!(
Some("prefixtestsuffix".to_string()),
NamingStrategy {
prefix: "prefix".to_string().into(),
suffix: "suffix".to_string().into(),
}
.apply("test".to_string().into())
);
assert_eq!(
Some("testsuffix".to_string()),
NamingStrategy {
suffix: "suffix".to_string().into(),
..Default::default()
}
.apply("test".to_string().into())
);
assert_eq!(
Some("prefixtest".to_string()),
NamingStrategy {
prefix: "prefix".to_string().into(),
..Default::default()
}
.apply("test".to_string().into())
);
assert_eq!(
Some("test".to_string()),
NamingStrategy {
..Default::default()
}
.apply("test".to_string().into())
);
assert_eq!(
None,
NamingStrategy {
prefix: "prefix".to_string().into(),
suffix: "suffix".to_string().into(),
}
.apply(None)
);
}
}

View File

@ -58,22 +58,26 @@ impl Cluster {
.set_owner_references
.is_some_and(|set| set)
.then_some(self.owner_ref(&()).into_iter().collect()),
name: config.naming.apply(self.name_any().into()),
..self.into()
},
#[cfg(feature = "agent-initiated")]
spec: match config.agent_initiated {
Some(true) => fleet_cluster::ClusterSpec {
client_id: Some(Alphanumeric.sample_string(&mut rand::thread_rng(), 64)),
agent_namespace: config.agent_namespace,
..Default::default()
},
None | Some(false) => fleet_cluster::ClusterSpec {
kube_config_secret: Some(format!("{}-kubeconfig", self.name_any())),
agent_namespace: config.agent_namespace,
..Default::default()
},
},
#[cfg(not(feature = "agent-initiated"))]
spec: fleet_cluster::ClusterSpec {
kube_config_secret: Some(format!("{}-kubeconfig", self.name_any())),
agent_namespace: config.agent_namespace,
..Default::default()
},
status: Default::default(),