Compare commits

..

3 Commits

5 changed files with 54 additions and 116 deletions

View File

@@ -194,11 +194,3 @@ impl From<String> for InterpretError {
}
}
}
impl From<serde_yaml::Error> for InterpretError {
fn from(value: serde_yaml::Error) -> Self {
Self {
msg: format!("InterpretError : {value}"),
}
}
}

View File

@@ -2,9 +2,10 @@ use derive_new::new;
use k8s_openapi::{
ClusterResourceScope, NamespaceResourceScope,
api::{apps::v1::Deployment, core::v1::Pod},
apimachinery::pkg::version::Info,
};
use kube::{
Client, Config, Error, Resource,
Client, Config, Discovery, Error, Resource,
api::{Api, AttachParams, DeleteParams, ListParams, Patch, PatchParams, ResourceExt},
config::{KubeConfigOptions, Kubeconfig},
core::ErrorResponse,
@@ -53,6 +54,17 @@ impl K8sClient {
})
}
pub async fn get_apiserver_version(&self) -> Result<Info, Error> {
let client: Client = self.client.clone();
let version_info: Info = client.apiserver_version().await?;
Ok(version_info)
}
pub async fn discovery(&self) -> Result<Discovery, Error> {
let discovery: Discovery = Discovery::new(self.client.clone()).run().await?;
Ok(discovery)
}
pub async fn get_resource_json_value(
&self,
name: &str,

View File

@@ -47,6 +47,13 @@ struct K8sState {
message: String,
}
#[derive(Debug, Clone)]
pub enum KubernetesDistribution {
OpenshiftFamily,
K3sFamily,
Default,
}
#[derive(Debug, Clone)]
enum K8sSource {
LocalK3d,
@@ -57,6 +64,7 @@ enum K8sSource {
pub struct K8sAnywhereTopology {
k8s_state: Arc<OnceCell<Option<K8sState>>>,
tenant_manager: Arc<OnceCell<K8sTenantManager>>,
flavour: Arc<OnceCell<KubernetesDistribution>>,
config: Arc<K8sAnywhereConfig>,
}
@@ -162,6 +170,7 @@ impl K8sAnywhereTopology {
Self {
k8s_state: Arc::new(OnceCell::new()),
tenant_manager: Arc::new(OnceCell::new()),
flavour: Arc::new(OnceCell::new()),
config: Arc::new(K8sAnywhereConfig::from_env()),
}
}
@@ -170,10 +179,42 @@ impl K8sAnywhereTopology {
Self {
k8s_state: Arc::new(OnceCell::new()),
tenant_manager: Arc::new(OnceCell::new()),
flavour: Arc::new(OnceCell::new()),
config: Arc::new(config),
}
}
pub async fn get_k8s_distribution(&self) -> Result<&KubernetesDistribution, PreparationError> {
self.flavour
.get_or_try_init(async || {
let client = self.k8s_client().await.unwrap();
let discovery = client.discovery().await.map_err(|e| {
PreparationError::new(format!("Could not discover API groups: {}", e))
})?;
let version = client.get_apiserver_version().await.map_err(|e| {
PreparationError::new(format!("Could not get server version: {}", e))
})?;
// OpenShift / OKD
if discovery
.groups()
.any(|g| g.name() == "project.openshift.io")
{
return Ok(KubernetesDistribution::OpenshiftFamily);
}
// K3d / K3s
if version.git_version.contains("k3s") {
return Ok(KubernetesDistribution::K3sFamily);
}
return Ok(KubernetesDistribution::Default);
})
.await
}
async fn get_cluster_observability_operator_prometheus_application_score(
&self,
sender: RHOBObservability,

View File

@@ -1,106 +0,0 @@
use std::sync::Arc;
use async_trait::async_trait;
use harmony_types::id::Id;
use serde::Serialize;
use crate::{
data::Version,
interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome},
inventory::Inventory,
score::Score,
topology::{K8sclient, Topology, k8s::K8sClient},
};
#[derive(Clone, Serialize, Debug)]
pub struct GenerateCaCertScore {
cluster_issuer_name: String,
dns_names: String,
operator_namespace: String,
}
impl<T: Topology + K8sclient> Score<T> for GenerateCaCertScore {
fn name(&self) -> String {
"GenerateCaCertScore".to_string()
}
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
Box::new(GenerateCaCertIntepret {
score: self.clone(),
})
}
}
#[derive(Clone, Serialize, Debug)]
pub struct GenerateCaCertIntepret {
score: GenerateCaCertScore,
}
#[async_trait]
impl<T: Topology + K8sclient> Interpret<T> for GenerateCaCertIntepret {
async fn execute(
&self,
_inventory: &Inventory,
topology: &T,
) -> Result<Outcome, InterpretError> {
let client = topology.k8s_client().await.unwrap();
let cert_yaml = self
.build_cert_request_yaml(&self.score.cluster_issuer_name, &self.score.dns_names)
.unwrap();
self.apply_cert_request(&client, cert_yaml, &self.score.operator_namespace)
.await?;
Ok(Outcome::success("created ca cert".to_string()))
}
fn get_name(&self) -> InterpretName {
InterpretName::Custom("GenerateCaCertInterpret")
}
fn get_version(&self) -> Version {
todo!()
}
fn get_status(&self) -> InterpretStatus {
todo!()
}
fn get_children(&self) -> Vec<Id> {
todo!()
}
}
impl GenerateCaCertIntepret {
pub fn build_cert_request_yaml(
&self,
cluster_issuer_name: &str,
dns_names: &str,
) -> Result<serde_yaml::Value, InterpretError> {
let cert_yaml = format!(
r#"
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: ingress-cert
namespace: openshift-ingress
spec:
secretName: ingress-cert-tls
issuerRef:
name: {cluster_issuer_name}
kind: ClusterIssuer
dnsNames:
- "*.{dns_names}"
"#
);
Ok(serde_yaml::to_value(cert_yaml)?)
}
pub async fn apply_cert_request(
&self,
client: &Arc<K8sClient>,
cert_yaml: serde_yaml::Value,
operator_namespace: &str,
) -> Result<(), InterpretError> {
Ok(client
.apply_yaml(&cert_yaml, Some(operator_namespace))
.await?)
}
}

View File

@@ -1,3 +1,2 @@
mod gen_ca_cert;
mod helm;
pub use helm::*;