-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ members = [ | |
"kiss/assets", | ||
"kiss/controller", | ||
"kiss/gateway", | ||
"kiss/manager", | ||
"kiss/monitor", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use ipis::core::anyhow::{anyhow, Result}; | ||
use kiss_api::{ | ||
k8s_openapi::{api::core::v1::ConfigMap, Resource}, | ||
kube::{ | ||
api::{Patch, PatchParams, PostParams}, | ||
core::ObjectMeta, | ||
Api, Client, | ||
}, | ||
serde_json::json, | ||
}; | ||
use semver::Version; | ||
|
||
pub struct Handler { | ||
api: Api<ConfigMap>, | ||
} | ||
|
||
impl Handler { | ||
pub async fn try_default() -> Result<Self> { | ||
// create a kubernetes client | ||
let ns = "kiss"; | ||
let client = Client::try_default().await?; | ||
|
||
Ok(Self { | ||
api: Api::<ConfigMap>::namespaced(client, ns), | ||
}) | ||
} | ||
} | ||
|
||
impl Handler { | ||
pub async fn create(&self, version: &Version) -> Result<()> { | ||
let config = ConfigMap { | ||
metadata: ObjectMeta { | ||
name: Some("manager".into()), | ||
..Default::default() | ||
}, | ||
immutable: Some(false), | ||
data: Some({ | ||
let mut map = BTreeMap::default(); | ||
map.insert("version".into(), version.to_string()); | ||
map | ||
}), | ||
..Default::default() | ||
}; | ||
let pp = PostParams { | ||
field_manager: Some("kiss-manager".into()), | ||
..Default::default() | ||
}; | ||
self.api.create(&pp, &config).await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn get(&self, latest: &Version) -> Result<Version> { | ||
let config = match self.api.get("manager").await { | ||
Ok(config) => config, | ||
Err(_) => { | ||
self.create(latest).await?; | ||
return Ok(latest.clone()); | ||
} | ||
}; | ||
|
||
let version = config | ||
.data | ||
.as_ref() | ||
.and_then(|map| map.get("version")) | ||
.ok_or_else(|| anyhow!("failed to find version field in configmap"))?; | ||
version.parse().map_err(Into::into) | ||
} | ||
|
||
pub async fn patch(&self, version: Version) -> Result<()> { | ||
let patch = Patch::Apply(json!({ | ||
"apiVersion": ConfigMap::API_VERSION, | ||
"kind": ConfigMap::KIND, | ||
"spec": { | ||
"version": version.to_string(), | ||
}, | ||
})); | ||
let pp = PatchParams::apply("kiss-manager").force(); | ||
self.api.patch("manager", &pp, &patch).await?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::sync::Arc; | ||
|
||
use ipis::{ | ||
core::anyhow::{bail, Result}, | ||
env::infer, | ||
}; | ||
use octocrab::Octocrab; | ||
use semver::Version; | ||
|
||
pub struct Handler { | ||
instance: Arc<Octocrab>, | ||
repo_name: String, | ||
repo_owner: String, | ||
} | ||
|
||
impl Default for Handler { | ||
fn default() -> Self { | ||
Self { | ||
instance: Default::default(), | ||
repo_name: infer("REPO_NAME").unwrap_or_else(|_| Self::REPOSITORY_NAME.into()), | ||
repo_owner: infer("REPO_OWNER").unwrap_or_else(|_| Self::REPOSITORY_OWNER.into()), | ||
} | ||
} | ||
} | ||
|
||
impl Handler { | ||
const REPOSITORY_NAME: &str = "netai-cloud"; | ||
const REPOSITORY_OWNER: &str = "ulagbulag-village"; | ||
|
||
pub async fn get(&self) -> Result<Version> { | ||
// request the latest release info | ||
let release = self | ||
.instance | ||
.repos(&self.repo_owner, &self.repo_name) | ||
.releases() | ||
.get_latest() | ||
.await?; | ||
|
||
// compare with the current release tag | ||
if !release.tag_name.starts_with("v") { | ||
bail!("Received unexpected version tag: {:?}", &release.tag_name); | ||
} | ||
Version::parse(&release.tag_name[1..]).map_err(Into::into) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters