Skip to content

Commit

Permalink
Extending rhai<->k8s capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
sebt3 committed May 8, 2024
1 parent b3bc924 commit 164a5e0
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 23 deletions.
8 changes: 6 additions & 2 deletions agent/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct Parameters {
/// Install Name
#[arg(short='i', long, env = "NAME", value_name = "NAME")]
name: String,
/// Templating target (plan|install|destroy)
#[arg(short, long, env = "TARGET", value_name = "TARGET", default_value = "install")]
target: String,
}

pub async fn template(src: PathBuf, dest: PathBuf, client: kube::Client,
Expand Down Expand Up @@ -115,13 +118,14 @@ pub async fn run(args:&Parameters) -> Result<()> {
bail!("{e}");
}};
// Start the script engine
let mut script = script::Script::from_dir(&src.clone(), &"template".to_string(), script::new_context(
let mut script = script::Script::from_dir(&src.clone(), &"template".to_string(), script::new_context_template(
yaml.category.clone(),
yaml.metadata.name.clone(),
args.name.clone(),
src.clone().into_os_string().into_string().unwrap(),
dest.clone().into_os_string().into_string().unwrap(),
&yaml.get_values(&inst.options())
&yaml.get_values(&inst.options()),
args.target.clone()
));
match template(src, dest, client.clone(), &inst, &yaml.clone(), &yaml.get_values(&inst.options()), &mut script).await {Ok(_) => {Ok(())}, Err(e) => {
inst.update_status_errors(client.clone(), AGENT, vec!(format!("{e}"))).await.map_err(|e| anyhow!("{e}"))?;
Expand Down
142 changes: 139 additions & 3 deletions k8s/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use kube::{
api::{Api, ListParams},
api::{Api, ListParams, ObjectList},
Client,
};

Expand All @@ -27,6 +27,10 @@ impl InstallHandler {
pub async fn get(&mut self, name: &str) -> Result<Install, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Install>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub struct DistribHandler {
Expand All @@ -51,6 +55,10 @@ impl DistribHandler {
pub async fn get(&mut self, name: &str) -> Result<Distrib, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Distrib>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::networking::v1::Ingress;
Expand All @@ -76,6 +84,10 @@ impl IngressHandler {
pub async fn get(&mut self, name: &str) -> Result<Ingress, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Ingress>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::core::v1::Secret;
Expand All @@ -101,6 +113,10 @@ impl SecretHandler {
pub async fn get(&mut self, name: &str) -> Result<Secret, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Secret>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition;
Expand All @@ -116,8 +132,8 @@ impl CustomResourceDefinitionHandler {
pub async fn have(&mut self, name: &str) -> bool {
let lp = ListParams::default();
let list = self.api.list(&lp).await.unwrap();
for secret in list {
if secret.metadata.name.clone().unwrap() == name {
for crd in list {
if crd.metadata.name.clone().unwrap() == name {
return true;
}
}
Expand All @@ -126,4 +142,124 @@ impl CustomResourceDefinitionHandler {
pub async fn get(&mut self, name: &str) -> Result<CustomResourceDefinition, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<CustomResourceDefinition>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::core::v1::Service;
pub struct ServiceHandler {
api: Api<Service>,
}
impl ServiceHandler {
#[must_use] pub fn new(cl: &Client, ns: &str) -> ServiceHandler {
ServiceHandler {
api: Api::namespaced(cl.clone(), ns),
}
}
pub async fn have(&mut self, name: &str) -> bool {
let lp = ListParams::default();
let list = self.api.list(&lp).await.unwrap();
for svc in list {
if svc.metadata.name.clone().unwrap() == name {
return true;
}
}
false
}
pub async fn get(&mut self, name: &str) -> Result<Service, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Service>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::core::v1::Namespace;
pub struct NamespaceHandler {
api: Api<Namespace>,
}
impl NamespaceHandler {
#[must_use] pub fn new(cl: &Client) -> NamespaceHandler {
NamespaceHandler {
api: Api::all(cl.clone()),
}
}
pub async fn have(&mut self, name: &str) -> bool {
let lp = ListParams::default();
let list = self.api.list(&lp).await.unwrap();
for ns in list {
if ns.metadata.name.clone().unwrap() == name {
return true;
}
}
false
}
pub async fn get(&mut self, name: &str) -> Result<Namespace, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<Namespace>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::storage::v1::StorageClass;
pub struct StorageClassHandler {
api: Api<StorageClass>,
}
impl StorageClassHandler {
#[must_use] pub fn new(cl: &Client) -> StorageClassHandler {
StorageClassHandler {
api: Api::all(cl.clone()),
}
}
pub async fn have(&mut self, name: &str) -> bool {
let lp = ListParams::default();
let list = self.api.list(&lp).await.unwrap();
for sc in list {
if sc.metadata.name.clone().unwrap() == name {
return true;
}
}
false
}
pub async fn get(&mut self, name: &str) -> Result<StorageClass, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<StorageClass>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}

pub use k8s_openapi::api::storage::v1::CSIDriver;
pub struct CSIDriverHandler {
api: Api<CSIDriver>,
}
impl CSIDriverHandler {
#[must_use] pub fn new(cl: &Client) -> CSIDriverHandler {
CSIDriverHandler {
api: Api::all(cl.clone()),
}
}
pub async fn have(&mut self, name: &str) -> bool {
let lp = ListParams::default();
let list = self.api.list(&lp).await.unwrap();
for sc in list {
if sc.metadata.name.clone().unwrap() == name {
return true;
}
}
false
}
pub async fn get(&mut self, name: &str) -> Result<CSIDriver, kube::Error> {
self.api.get(name).await
}
pub async fn list(&mut self) -> Result<ObjectList<CSIDriver>, kube::Error> {
let lp = ListParams::default();
self.api.list(&lp).await
}
}
12 changes: 7 additions & 5 deletions operator/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ fn get_action(hself: &HashedSelf, act: &str) -> serde_json::Value {
action
}

fn get_templater(hself: &HashedSelf, category: &str, component: &str) -> serde_json::Value {
fn get_templater(hself: &HashedSelf, category: &str, component: &str, target: &str) -> serde_json::Value {
let mut templater = install_container(hself);
templater["name"] = serde_json::Value::String("template".to_string());
templater["args"] = serde_json::Value::Array([
templater["name"].clone(),
serde_json::Value::String("-s".to_string()),
serde_json::Value::String(format!("/src/{}/{}/", category, component))
serde_json::Value::String(format!("/src/{}/{}/", category, component)),
serde_json::Value::String("-t".to_string()),
serde_json::Value::String(target.to_string())
].into());
templater["volumeMounts"] = serde_json::Value::Array([serde_json::json!({
"name": "dist",
Expand Down Expand Up @@ -240,21 +242,21 @@ impl JobHandler {

pub fn get_installs_plan(&self, hashedself: &HashedSelf, category: &str, component: &str) -> serde_json::Value {
self.get_installs_spec(hashedself,
&vec!(get_templater(hashedself, category, component)),
&vec!(get_templater(hashedself, category, component, "plan")),
&vec!(get_action(hashedself, "plan"))
)
}

pub fn get_installs_destroy(&self, hashedself: &HashedSelf, category: &str, component: &str) -> serde_json::Value {
self.get_installs_spec(hashedself,
&vec!(get_templater(hashedself, category, component)),
&vec!(get_templater(hashedself, category, component, "destroy")),
&vec!(get_action(hashedself, "destroy"))
)
}

pub fn get_installs_install(&self, hashedself: &HashedSelf, category: &str, component: &str) -> serde_json::Value {
self.get_installs_spec(hashedself,
&vec![get_templater(hashedself, category, component),get_action(hashedself, "plan")],
&vec![get_templater(hashedself, category, component, "install"),get_action(hashedself, "plan")],
&vec!(get_action(hashedself, "install"))
)
}
Expand Down
Loading

0 comments on commit 164a5e0

Please sign in to comment.