Compare commits
8 Commits
feat/okd_d
...
528ee8a696
| Author | SHA1 | Date | |
|---|---|---|---|
| 528ee8a696 | |||
| 69a159711a | |||
| b0ad7bb4c4 | |||
| 5f78300d78 | |||
| 2d3c32469c | |||
| 1cec398d4d | |||
| cbbaae2ac8 | |||
| f073b7e5fb |
@@ -1,10 +1,13 @@
|
||||
use std::time::Duration;
|
||||
|
||||
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,
|
||||
@@ -20,9 +23,7 @@ use log::{debug, error, trace};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use serde_json::{Value, json};
|
||||
use similar::TextDiff;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
use crate::interpret::Outcome;
|
||||
use tokio::{io::AsyncReadExt, time::sleep};
|
||||
|
||||
#[derive(new, Clone)]
|
||||
pub struct K8sClient {
|
||||
@@ -56,55 +57,15 @@ impl K8sClient {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn ensure_deployment(
|
||||
&self,
|
||||
resource_name: &str,
|
||||
resource_namespace: &str,
|
||||
) -> Result<Outcome, Error> {
|
||||
match self
|
||||
.get_deployment(resource_name, Some(&resource_namespace))
|
||||
.await
|
||||
{
|
||||
Ok(Some(deployment)) => {
|
||||
if let Some(status) = deployment.status {
|
||||
let ready_count = status.ready_replicas.unwrap_or(0);
|
||||
if ready_count >= 1 {
|
||||
Ok(Outcome::success(format!(
|
||||
"'{}' is ready with {} replica(s).",
|
||||
resource_name, ready_count
|
||||
)))
|
||||
} else {
|
||||
Err(Error::Discovery(DiscoveryError::MissingResource(format!(
|
||||
"Deployment '{}' in namespace '{}' has 0 ready replicas",
|
||||
resource_name, resource_namespace
|
||||
))))
|
||||
}
|
||||
} else {
|
||||
Err(Error::Api(ErrorResponse {
|
||||
status: "Failure".to_string(),
|
||||
message: format!(
|
||||
"No status found for deployment '{}' in namespace '{}'",
|
||||
resource_name, resource_namespace
|
||||
),
|
||||
reason: "MissingStatus".to_string(),
|
||||
code: 404,
|
||||
}))
|
||||
}
|
||||
}
|
||||
Ok(None) => Err(Error::Discovery(DiscoveryError::MissingResource(format!(
|
||||
"Deployment '{}' not found in namespace '{}'",
|
||||
resource_name, resource_namespace
|
||||
)))),
|
||||
Err(e) => Err(Error::Api(ErrorResponse {
|
||||
status: "Failure".to_string(),
|
||||
message: format!(
|
||||
"Failed to fetch deployment '{}' in namespace '{}': {}",
|
||||
resource_name, resource_namespace, e
|
||||
),
|
||||
reason: "ApiError".to_string(),
|
||||
code: 500,
|
||||
})),
|
||||
}
|
||||
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(
|
||||
@@ -144,25 +105,6 @@ impl K8sClient {
|
||||
Ok(pods.get_opt(name).await?)
|
||||
}
|
||||
|
||||
pub async fn patch_resource_by_merge(
|
||||
&self,
|
||||
name: &str,
|
||||
namespace: Option<&str>,
|
||||
gvk: &GroupVersionKind,
|
||||
patch: Value,
|
||||
) -> Result<(), Error> {
|
||||
let gvk = ApiResource::from_gvk(gvk);
|
||||
let resource: Api<DynamicObject> = if let Some(ns) = namespace {
|
||||
Api::namespaced_with(self.client.clone(), ns, &gvk)
|
||||
} else {
|
||||
Api::default_namespaced_with(self.client.clone(), &gvk)
|
||||
};
|
||||
let pp = PatchParams::default();
|
||||
let merge = Patch::Merge(&patch);
|
||||
resource.patch(name, &pp, &merge).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn scale_deployment(
|
||||
&self,
|
||||
name: &str,
|
||||
@@ -226,6 +168,41 @@ impl K8sClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_for_pod_ready(
|
||||
&self,
|
||||
pod_name: &str,
|
||||
namespace: Option<&str>,
|
||||
) -> Result<(), Error> {
|
||||
let mut elapsed = 0;
|
||||
let interval = 5; // seconds between checks
|
||||
let timeout_secs = 120;
|
||||
loop {
|
||||
let pod = self.get_pod(pod_name, namespace).await?;
|
||||
|
||||
if let Some(p) = pod {
|
||||
if let Some(status) = p.status {
|
||||
if let Some(phase) = status.phase {
|
||||
if phase.to_lowercase() == "running" {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if elapsed >= timeout_secs {
|
||||
return Err(Error::Discovery(DiscoveryError::MissingResource(format!(
|
||||
"'{}' in ns '{}' did not become ready within {}s",
|
||||
pod_name,
|
||||
namespace.unwrap(),
|
||||
timeout_secs
|
||||
))));
|
||||
}
|
||||
|
||||
sleep(Duration::from_secs(interval)).await;
|
||||
elapsed += interval;
|
||||
}
|
||||
}
|
||||
|
||||
/// Will execute a commond in the first pod found that matches the specified label
|
||||
/// '{label}={name}'
|
||||
pub async fn exec_app_capture_output(
|
||||
@@ -492,9 +469,12 @@ impl K8sClient {
|
||||
.as_str()
|
||||
.expect("couldn't get kind as str");
|
||||
|
||||
let split: Vec<&str> = api_version.splitn(2, "/").collect();
|
||||
let g = split[0];
|
||||
let v = split[1];
|
||||
let mut it = api_version.splitn(2, '/');
|
||||
let first = it.next().unwrap();
|
||||
let (g, v) = match it.next() {
|
||||
Some(second) => (first, second),
|
||||
None => ("", first),
|
||||
};
|
||||
|
||||
let gvk = GroupVersionKind::gvk(g, v, kind);
|
||||
let api_resource = ApiResource::from_gvk(&gvk);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -186,7 +186,7 @@ impl TopologyState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum DeploymentTarget {
|
||||
LocalDev,
|
||||
Staging,
|
||||
|
||||
@@ -57,8 +57,10 @@ impl<T: Topology + K8sclient + HelmCommand + Ingress> Interpret<T> for ArgoInter
|
||||
let k8s_client = topology.k8s_client().await?;
|
||||
let svc = format!("argo-{}", self.score.namespace.clone());
|
||||
let domain = topology.get_domain(&svc).await?;
|
||||
// FIXME we now have a way to know if we're running on openshift family
|
||||
|
||||
let helm_score =
|
||||
argo_helm_chart_score(&self.score.namespace, self.score.openshift, &domain);
|
||||
argo_helm_chart_score(&self.score.namespace, self.score.openshift, &domain);
|
||||
|
||||
helm_score.interpret(inventory, topology).await?;
|
||||
|
||||
|
||||
@@ -10,12 +10,11 @@ use crate::{
|
||||
data::Version,
|
||||
inventory::Inventory,
|
||||
modules::application::{
|
||||
ApplicationFeature, HelmPackage, InstallationError, InstallationOutcome, OCICompliant,
|
||||
features::{ArgoApplication, ArgoHelmScore},
|
||||
features::{ArgoApplication, ArgoHelmScore}, webapp::Webapp, ApplicationFeature, HelmPackage, InstallationError, InstallationOutcome, OCICompliant
|
||||
},
|
||||
score::Score,
|
||||
topology::{
|
||||
DeploymentTarget, HelmCommand, K8sclient, MultiTargetTopology, Topology, ingress::Ingress,
|
||||
ingress::Ingress, DeploymentTarget, HelmCommand, K8sclient, MultiTargetTopology, Topology
|
||||
},
|
||||
};
|
||||
|
||||
@@ -47,11 +46,11 @@ use crate::{
|
||||
/// - ArgoCD to install/upgrade/rollback/inspect k8s resources
|
||||
/// - Kubernetes for runtime orchestration
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct PackagingDeployment<A: OCICompliant + HelmPackage> {
|
||||
pub struct PackagingDeployment<A: OCICompliant + HelmPackage + Webapp> {
|
||||
pub application: Arc<A>,
|
||||
}
|
||||
|
||||
impl<A: OCICompliant + HelmPackage> PackagingDeployment<A> {
|
||||
impl<A: OCICompliant + HelmPackage + Webapp> PackagingDeployment<A> {
|
||||
async fn deploy_to_local_k3d(
|
||||
&self,
|
||||
app_name: String,
|
||||
@@ -137,7 +136,7 @@ impl<A: OCICompliant + HelmPackage> PackagingDeployment<A> {
|
||||
|
||||
#[async_trait]
|
||||
impl<
|
||||
A: OCICompliant + HelmPackage + Clone + 'static,
|
||||
A: OCICompliant + HelmPackage + Webapp + Clone + 'static,
|
||||
T: Topology + HelmCommand + MultiTargetTopology + K8sclient + Ingress + 'static,
|
||||
> ApplicationFeature<T> for PackagingDeployment<A>
|
||||
{
|
||||
@@ -146,10 +145,15 @@ impl<
|
||||
topology: &T,
|
||||
) -> Result<InstallationOutcome, InstallationError> {
|
||||
let image = self.application.image_name();
|
||||
let domain = topology
|
||||
|
||||
let domain = if topology.current_target() == DeploymentTarget::Production {
|
||||
self.application.dns()
|
||||
} else {
|
||||
topology
|
||||
.get_domain(&self.application.name())
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
|
||||
// TODO Write CI/CD workflow files
|
||||
// we can autotedect the CI type using the remote url (default to github action for github
|
||||
|
||||
@@ -2,6 +2,7 @@ mod feature;
|
||||
pub mod features;
|
||||
pub mod oci;
|
||||
mod rust;
|
||||
mod webapp;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use feature::*;
|
||||
|
||||
@@ -16,6 +16,7 @@ use tar::{Builder, Header};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::config::{REGISTRY_PROJECT, REGISTRY_URL};
|
||||
use crate::modules::application::webapp::Webapp;
|
||||
use crate::{score::Score, topology::Topology};
|
||||
|
||||
use super::{Application, ApplicationFeature, ApplicationInterpret, HelmPackage, OCICompliant};
|
||||
@@ -60,6 +61,10 @@ pub struct RustWebapp {
|
||||
pub project_root: PathBuf,
|
||||
pub service_port: u32,
|
||||
pub framework: Option<RustWebFramework>,
|
||||
/// Host name that will be used in production environment.
|
||||
///
|
||||
/// This is the place to put the public host name if this is a public facing webapp.
|
||||
pub dns: String,
|
||||
}
|
||||
|
||||
impl Application for RustWebapp {
|
||||
@@ -68,6 +73,12 @@ impl Application for RustWebapp {
|
||||
}
|
||||
}
|
||||
|
||||
impl Webapp for RustWebapp {
|
||||
fn dns(&self) -> String {
|
||||
self.dns.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl HelmPackage for RustWebapp {
|
||||
async fn build_push_helm_package(
|
||||
@@ -257,7 +268,6 @@ impl RustWebapp {
|
||||
".harmony_generated",
|
||||
"harmony",
|
||||
"node_modules",
|
||||
"Dockerfile.harmony",
|
||||
];
|
||||
let mut entries: Vec<_> = WalkDir::new(project_root)
|
||||
.into_iter()
|
||||
@@ -461,52 +471,53 @@ impl RustWebapp {
|
||||
|
||||
let (image_repo, image_tag) = image_url.rsplit_once(':').unwrap_or((image_url, "latest"));
|
||||
|
||||
let app_name = &self.name;
|
||||
let service_port = self.service_port;
|
||||
// Create Chart.yaml
|
||||
let chart_yaml = format!(
|
||||
r#"
|
||||
apiVersion: v2
|
||||
name: {}
|
||||
description: A Helm chart for the {} web application.
|
||||
name: {chart_name}
|
||||
description: A Helm chart for the {app_name} web application.
|
||||
type: application
|
||||
version: 0.1.0
|
||||
appVersion: "{}"
|
||||
version: 0.2.0
|
||||
appVersion: "{image_tag}"
|
||||
"#,
|
||||
chart_name, self.name, image_tag
|
||||
);
|
||||
fs::write(chart_dir.join("Chart.yaml"), chart_yaml)?;
|
||||
|
||||
// Create values.yaml
|
||||
let values_yaml = format!(
|
||||
r#"
|
||||
# Default values for {}.
|
||||
# Default values for {chart_name}.
|
||||
# This is a YAML-formatted file.
|
||||
# Declare variables to be passed into your templates.
|
||||
|
||||
replicaCount: 1
|
||||
|
||||
image:
|
||||
repository: {}
|
||||
repository: {image_repo}
|
||||
pullPolicy: IfNotPresent
|
||||
# Overridden by the chart's appVersion
|
||||
tag: "{}"
|
||||
tag: "{image_tag}"
|
||||
|
||||
service:
|
||||
type: ClusterIP
|
||||
port: {}
|
||||
port: {service_port}
|
||||
|
||||
ingress:
|
||||
enabled: true
|
||||
tls: true
|
||||
# Annotations for cert-manager to handle SSL.
|
||||
annotations:
|
||||
# Add other annotations like nginx ingress class if needed
|
||||
# kubernetes.io/ingress.class: nginx
|
||||
hosts:
|
||||
- host: {}
|
||||
- host: {domain}
|
||||
paths:
|
||||
- path: /
|
||||
pathType: ImplementationSpecific
|
||||
"#,
|
||||
chart_name, image_repo, image_tag, self.service_port, domain,
|
||||
);
|
||||
fs::write(chart_dir.join("values.yaml"), values_yaml)?;
|
||||
|
||||
@@ -583,7 +594,11 @@ spec:
|
||||
);
|
||||
fs::write(templates_dir.join("deployment.yaml"), deployment_yaml)?;
|
||||
|
||||
let service_port = self.service_port;
|
||||
|
||||
// Create templates/ingress.yaml
|
||||
// TODO get issuer name and tls config from topology as it may be different from one
|
||||
// cluster to another, also from one version to another
|
||||
let ingress_yaml = format!(
|
||||
r#"
|
||||
{{{{- if $.Values.ingress.enabled -}}}}
|
||||
@@ -596,13 +611,11 @@ metadata:
|
||||
spec:
|
||||
{{{{- if $.Values.ingress.tls }}}}
|
||||
tls:
|
||||
{{{{- range $.Values.ingress.tls }}}}
|
||||
- hosts:
|
||||
{{{{- range .hosts }}}}
|
||||
- {{{{ . | quote }}}}
|
||||
- secretName: {{{{ include "chart.fullname" . }}}}-tls
|
||||
hosts:
|
||||
{{{{- range $.Values.ingress.hosts }}}}
|
||||
- {{{{ .host | quote }}}}
|
||||
{{{{- end }}}}
|
||||
secretName: {{{{ .secretName }}}}
|
||||
{{{{- end }}}}
|
||||
{{{{- end }}}}
|
||||
rules:
|
||||
{{{{- range $.Values.ingress.hosts }}}}
|
||||
@@ -616,12 +629,11 @@ spec:
|
||||
service:
|
||||
name: {{{{ include "chart.fullname" $ }}}}
|
||||
port:
|
||||
number: {{{{ $.Values.service.port | default {} }}}}
|
||||
number: {{{{ $.Values.service.port | default {service_port} }}}}
|
||||
{{{{- end }}}}
|
||||
{{{{- end }}}}
|
||||
{{{{- end }}}}
|
||||
"#,
|
||||
self.service_port
|
||||
);
|
||||
fs::write(templates_dir.join("ingress.yaml"), ingress_yaml)?;
|
||||
|
||||
|
||||
7
harmony/src/modules/application/webapp.rs
Normal file
7
harmony/src/modules/application/webapp.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use super::Application;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Webapp: Application {
|
||||
fn dns(&self) -> String;
|
||||
}
|
||||
20
harmony/src/modules/argocd/discover.rs
Normal file
20
harmony/src/modules/argocd/discover.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
/// Discover the current ArgoCD setup
|
||||
///
|
||||
/// 1. No argo installed
|
||||
/// 2. Argo installed in current namespace
|
||||
/// 3. Argo installed in different namespace (assuming cluster wide access)
|
||||
///
|
||||
/// For now we will go ahead with this very basic logic, there are many intricacies that can be
|
||||
/// dealt with later, such as multitenant management in a single argo instance, credentials setup t
|
||||
|
||||
#[async_trait]
|
||||
pub trait ArgoCD {
|
||||
async fn ensure_installed() {
|
||||
}
|
||||
}
|
||||
|
||||
struct CurrentNamespaceArgo;
|
||||
|
||||
|
||||
impl ArgoCD for CurrentNamespaceArgo {
|
||||
}
|
||||
2
harmony/src/modules/argocd/mod.rs
Normal file
2
harmony/src/modules/argocd/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod discover;
|
||||
pub use discover::*;
|
||||
@@ -1,3 +1,2 @@
|
||||
mod helm;
|
||||
pub mod update_default_okd_ingress_score;
|
||||
pub use helm::*;
|
||||
|
||||
@@ -1,223 +0,0 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||
use fqdn::Path;
|
||||
use harmony_types::id::Id;
|
||||
use kube::api::GroupVersionKind;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
data::Version,
|
||||
interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome},
|
||||
inventory::Inventory,
|
||||
score::Score,
|
||||
topology::{K8sclient, Topology, k8s::K8sClient},
|
||||
};
|
||||
|
||||
pub struct UpdateDefaultOkdIngressScore {
|
||||
operator_name: String,
|
||||
operator_namespace: String,
|
||||
ca_name: String,
|
||||
path_to_tls_crt: Path,
|
||||
path_to_tls_key: Path,
|
||||
path_to_ca_cert: Path,
|
||||
}
|
||||
|
||||
impl<T: Topology> Score<T> for UpdateDefaultOkdIngressScore {
|
||||
fn name(&self) -> String {
|
||||
"UpdateDefaultOkdIngressScore".to_string()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
|
||||
Box::new(UpdateDefaultOkdIngressInterpret {
|
||||
score: self.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UpdateDefaultOkdIngressInterpret {
|
||||
score: UpdateDefaultOkdIngressScore,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology + K8sclient> Interpret<T> for UpdateDefaultOkdIngressInterpret {
|
||||
async fn execute(
|
||||
&self,
|
||||
inventory: &Inventory,
|
||||
topology: &T,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let client = topology.k8s_client().await?;
|
||||
let secret_name = "ingress_ca_secret";
|
||||
self.ensure_ingress_operator(
|
||||
&client,
|
||||
&self.score.operator_name,
|
||||
&self.score.operator_namespace,
|
||||
)
|
||||
.await?;
|
||||
self.create_ca_cm(&client, self.score.path_to_ca_cert, &self.score.ca_name)
|
||||
.await?;
|
||||
self.patch_proxy(&client, &self.score.ca_name).await?;
|
||||
self.create_tls_secret(
|
||||
&client,
|
||||
self.score.path_to_tls_crt,
|
||||
self.score.path_to_tls_key,
|
||||
&self.score.operator_namespace,
|
||||
&secret_name,
|
||||
)
|
||||
.await?;
|
||||
self.patch_ingress(&client, &self.score.operator_namespace, &secret_name)
|
||||
.await?;
|
||||
}
|
||||
|
||||
fn get_name(&self) -> InterpretName {
|
||||
InterpretName::Custom("UpdateDefaultOkdIngress")
|
||||
}
|
||||
|
||||
fn get_version(&self) -> Version {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_status(&self) -> InterpretStatus {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_children(&self) -> Vec<Id> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateDefaultOkdIngressInterpret {
|
||||
async fn ensure_ingress_operator(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
operator_name: &str,
|
||||
operator_namespace: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
client
|
||||
.ensure_deployment(operator_name, Some(operator_namespace))
|
||||
.await?
|
||||
}
|
||||
|
||||
fn open_path(&self, path: Path) -> Result<String, InterpretError> {
|
||||
let mut file = match File::open(&path) {
|
||||
Ok(file) => file,
|
||||
Err(e) => InterpretError::new(format!("Could not open file {}", e)),
|
||||
};
|
||||
let s = String::new();
|
||||
match file.read_to_string(&mut s) {
|
||||
Ok(s) => Ok(s),
|
||||
Err(e) => InterpretError::new(format!("Could not read file {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_ca_cm(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
path_to_ca_cert: Path,
|
||||
ca_name: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let ca_bundle = BASE64_STANDARD.encode(self.open_path(path_to_ca_cert).unwrap().as_bytes());
|
||||
|
||||
let cm = format!(
|
||||
r"#
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: custom-ca
|
||||
namespace: openshift-config
|
||||
data:
|
||||
ca-bundle.crt: {ca_bundle}
|
||||
#"
|
||||
);
|
||||
client.apply_yaml(serde_yaml::to_value(&cm), Some("openshift-config")).await?;
|
||||
Ok(Outcome::success(format!(
|
||||
"successfully created cm : {} in openshift-config namespace",
|
||||
ca_name
|
||||
)))
|
||||
}
|
||||
|
||||
async fn patch_proxy(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
ca_name: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let gvk = GroupVersionKind {
|
||||
group: "config.openshift.io".to_string(),
|
||||
version: "v1".to_string(),
|
||||
kind: "Proxy".to_string(),
|
||||
};
|
||||
let patch = json!({
|
||||
"spec": {
|
||||
"trustedCA": {
|
||||
"name": ca_name
|
||||
}
|
||||
}
|
||||
});
|
||||
client
|
||||
.patch_resource_by_merge("cluster", None, &gvk, patch)
|
||||
.await?;
|
||||
Ok(Outcome::success(format!(
|
||||
"successfully merged trusted ca to cluster proxy"
|
||||
)))
|
||||
}
|
||||
|
||||
async fn create_tls_secret(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
tls_crt: Path,
|
||||
tls_key: Path,
|
||||
operator_namespace: &str,
|
||||
secret_name: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let base64_tls_crt = BASE64_STANDARD.encode(self.open_path(tls_crt).unwrap().as_bytes());
|
||||
let base64_tls_key = BASE64_STANDARD.encode(self.open_path(tls_key).unwrap().as_bytes());
|
||||
let secret = format!(
|
||||
r#"
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: secret-tls
|
||||
namespace: {operator_namespace}
|
||||
type: kubernetes.io/tls
|
||||
data:
|
||||
# values are base64 encoded, which obscures them but does NOT provide
|
||||
# any useful level of confidentiality
|
||||
# Replace the following values with your own base64-encoded certificate and key.
|
||||
tls.crt: "{base64_tls_crt}"
|
||||
tls.key: "{base64_tls_key}"
|
||||
"#
|
||||
);
|
||||
client
|
||||
.apply_yaml(serde_yaml::to_value(secret), Some(operator_namespace))
|
||||
.await?;
|
||||
Ok(Outcome::success(format!(
|
||||
"successfully created tls secret trusted ca to cluster proxy"
|
||||
)))
|
||||
}
|
||||
|
||||
async fn patch_ingress(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
operator_namespace: &str,
|
||||
secret_name: &str,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let gvk = GroupVersionKind {
|
||||
group: "operator.openshift.io".to_string(),
|
||||
version: "v1".to_string(),
|
||||
kind: "IngressController".to_string(),
|
||||
};
|
||||
let patch = json!(
|
||||
{"spec":{"defaultCertificate": {"name": secret_name}}});
|
||||
client
|
||||
.patch_resource_by_merge("default", Some(operator_namespace), &gvk, patch)
|
||||
.await?;
|
||||
|
||||
Ok(Outcome::success(format!("successfully pathed ingress operator to use secret {}", secret_name)))
|
||||
}
|
||||
}
|
||||
@@ -17,3 +17,4 @@ pub mod prometheus;
|
||||
pub mod storage;
|
||||
pub mod tenant;
|
||||
pub mod tftp;
|
||||
pub mod argocd;
|
||||
|
||||
@@ -4,4 +4,5 @@ pub mod application_monitoring;
|
||||
pub mod grafana;
|
||||
pub mod kube_prometheus;
|
||||
pub mod ntfy;
|
||||
pub mod okd;
|
||||
pub mod prometheus;
|
||||
|
||||
149
harmony/src/modules/monitoring/okd/enable_user_workload.rs
Normal file
149
harmony/src/modules/monitoring/okd/enable_user_workload.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
data::Version,
|
||||
interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome},
|
||||
inventory::Inventory,
|
||||
score::Score,
|
||||
topology::{K8sclient, Topology, k8s::K8sClient},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use harmony_types::id::Id;
|
||||
use k8s_openapi::api::core::v1::ConfigMap;
|
||||
use kube::api::ObjectMeta;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct OpenshiftUserWorkloadMonitoring {}
|
||||
|
||||
impl<T: Topology + K8sclient> Score<T> for OpenshiftUserWorkloadMonitoring {
|
||||
fn name(&self) -> String {
|
||||
"OpenshiftUserWorkloadMonitoringScore".to_string()
|
||||
}
|
||||
|
||||
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
|
||||
Box::new(OpenshiftUserWorkloadMonitoringInterpret {})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct OpenshiftUserWorkloadMonitoringInterpret {}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology + K8sclient> Interpret<T> for OpenshiftUserWorkloadMonitoringInterpret {
|
||||
async fn execute(
|
||||
&self,
|
||||
_inventory: &Inventory,
|
||||
topology: &T,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let client = topology.k8s_client().await.unwrap();
|
||||
self.update_cluster_monitoring_config_cm(&client).await?;
|
||||
self.update_user_workload_monitoring_config_cm(&client)
|
||||
.await?;
|
||||
self.verify_user_workload(&client).await?;
|
||||
Ok(Outcome::success(
|
||||
"successfully enabled user-workload-monitoring".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
fn get_name(&self) -> InterpretName {
|
||||
InterpretName::Custom("OpenshiftUserWorkloadMonitoring")
|
||||
}
|
||||
|
||||
fn get_version(&self) -> Version {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_status(&self) -> InterpretStatus {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_children(&self) -> Vec<Id> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenshiftUserWorkloadMonitoringInterpret {
|
||||
pub async fn update_cluster_monitoring_config_cm(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let mut data = BTreeMap::new();
|
||||
data.insert(
|
||||
"config.yaml".to_string(),
|
||||
r#"
|
||||
enableUserWorkload: true
|
||||
alertmanagerMain:
|
||||
enableUserAlertmanagerConfig: true
|
||||
"#
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
let cm = ConfigMap {
|
||||
metadata: ObjectMeta {
|
||||
name: Some("cluster-monitoring-config".to_string()),
|
||||
namespace: Some("openshift-monitoring".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
data: Some(data),
|
||||
..Default::default()
|
||||
};
|
||||
client.apply(&cm, Some("openshift-monitoring")).await?;
|
||||
|
||||
Ok(Outcome::success(
|
||||
"updated cluster-monitoring-config-map".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn update_user_workload_monitoring_config_cm(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let mut data = BTreeMap::new();
|
||||
data.insert(
|
||||
"config.yaml".to_string(),
|
||||
r#"
|
||||
alertmanager:
|
||||
enabled: true
|
||||
enableAlertmanagerConfig: true
|
||||
"#
|
||||
.to_string(),
|
||||
);
|
||||
let cm = ConfigMap {
|
||||
metadata: ObjectMeta {
|
||||
name: Some("user-workload-monitoring-config".to_string()),
|
||||
namespace: Some("openshift-user-workload-monitoring".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
data: Some(data),
|
||||
..Default::default()
|
||||
};
|
||||
client
|
||||
.apply(&cm, Some("openshift-user-workload-monitoring"))
|
||||
.await?;
|
||||
|
||||
Ok(Outcome::success(
|
||||
"updated openshift-user-monitoring-config-map".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn verify_user_workload(
|
||||
&self,
|
||||
client: &Arc<K8sClient>,
|
||||
) -> Result<Outcome, InterpretError> {
|
||||
let namespace = "openshift-user-workload-monitoring";
|
||||
let alertmanager_name = "alertmanager-user-workload-0";
|
||||
let prometheus_name = "prometheus-user-workload-0";
|
||||
client
|
||||
.wait_for_pod_ready(alertmanager_name, Some(namespace))
|
||||
.await?;
|
||||
client
|
||||
.wait_for_pod_ready(prometheus_name, Some(namespace))
|
||||
.await?;
|
||||
|
||||
Ok(Outcome::success(format!(
|
||||
"pods: {}, {} ready in ns: {}",
|
||||
alertmanager_name, prometheus_name, namespace
|
||||
)))
|
||||
}
|
||||
}
|
||||
1
harmony/src/modules/monitoring/okd/mod.rs
Normal file
1
harmony/src/modules/monitoring/okd/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod enable_user_workload;
|
||||
Reference in New Issue
Block a user