Skip to content

Commit

Permalink
Implement switching clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed Sep 6, 2022
1 parent 1acd68d commit 06ebce1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
21 changes: 12 additions & 9 deletions kiss/api/src/ansible/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use kube::{
Api, Client, Error,
};

use crate::r#box::{BoxPowerSpec, BoxSpec, BoxState};
use crate::r#box::{BoxPowerSpec, BoxSpec, BoxState, BoxStatus};

pub struct AnsibleClient {
ansible_image: String,
Expand All @@ -23,6 +23,7 @@ impl AnsibleClient {
pub const LABEL_BOX_ACCESS_ADDRESS: &'static str = "kiss.netai-cloud/box_access_address";
pub const LABEL_BOX_MACHINE_UUID: &'static str = "kiss.netai-cloud/box_machine_uuid";
pub const LABEL_COMPLETED_STATE: &'static str = "kiss.netai-cloud/completed_state";
pub const LABEL_TARGET_CLUSTER: &'static str = "kiss.netai-cloud/target_cluster";

pub const ANSIBLE_IMAGE: &'static str = "quay.io/kubespray/kubespray:v2.19.1";

Expand All @@ -36,6 +37,13 @@ impl AnsibleClient {
let ns = "kiss";
let box_name = job.spec.machine.uuid.to_string();
let name = format!("box-{}-{}", &job.task, &box_name);
let cluster = job
.status
.as_ref()
.and_then(|status| status.bind_cluster.as_ref())
.or_else(|| job.spec.cluster.as_ref())
.map(String::as_str)
.unwrap_or("default");

// delete all previous cronjobs
{
Expand Down Expand Up @@ -78,6 +86,7 @@ impl AnsibleClient {
.as_ref()
.map(ToString::to_string)
.map(|state| (Self::LABEL_COMPLETED_STATE.into(), state)),
Some((Self::LABEL_TARGET_CLUSTER.into(), cluster.to_string())),
]
.into_iter()
.flatten()
Expand Down Expand Up @@ -190,14 +199,7 @@ impl AnsibleClient {
Volume {
name: "ansible".into(),
config_map: Some(ConfigMapVolumeSource {
name: Some(format!(
"ansible-control-planes-{}",
job.spec
.cluster
.as_ref()
.map(String::as_str)
.unwrap_or("default")
)),
name: Some(format!("ansible-control-planes-{cluster}")),
default_mode: Some(0o400),
..Default::default()
}),
Expand Down Expand Up @@ -284,5 +286,6 @@ pub struct AnsibleJob<'a> {
pub cron: Option<&'static str>,
pub task: &'static str,
pub spec: &'a BoxSpec,
pub status: Option<&'a BoxStatus>,
pub completed_state: Option<BoxState>,
}
1 change: 1 addition & 0 deletions kiss/api/src/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct BoxSpec {
#[serde(rename_all = "camelCase")]
pub struct BoxStatus {
pub state: BoxState,
pub bind_cluster: Option<String>,
pub last_updated: DateTime<Utc>,
}

Expand Down
2 changes: 2 additions & 0 deletions kiss/controller/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl ::kiss_api::manager::Ctx for Ctx {
cron: new_state.cron(),
task,
spec: &data.spec,
status: data.status.as_ref(),
completed_state: new_state.complete(),
},
)
Expand All @@ -76,6 +77,7 @@ impl ::kiss_api::manager::Ctx for Ctx {
"kind": crd.kind,
"status": BoxStatus {
state: new_state,
bind_cluster: status.as_ref().and_then(|status| status.bind_cluster.as_ref()).cloned(),
last_updated: if old_state == new_state {
status
.as_ref()
Expand Down
8 changes: 6 additions & 2 deletions kiss/gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ async fn get_new(client: Data<Arc<Client>>, Query(query): Query<BoxNewQuery>) ->
"kind": crd.kind,
"status": BoxStatus {
state: BoxState::New,
bind_cluster: None,
last_updated: Utc::now(),
},
}));
let pp = PatchParams::apply("kiss-controller").force();
let pp = PatchParams::apply("kiss-gateway").force();
api.patch_status(&name, &pp, &patch).await?;
}
Err(_) => {
Expand All @@ -74,6 +75,7 @@ async fn get_new(client: Data<Arc<Client>>, Query(query): Query<BoxNewQuery>) ->
},
status: Some(BoxStatus {
state: BoxState::New,
bind_cluster: None,
last_updated: Utc::now(),
}),
};
Expand Down Expand Up @@ -112,10 +114,11 @@ async fn get_commission(client: Data<Arc<Client>>, Json(spec): Json<BoxSpec>) ->
"spec": spec,
"status": BoxStatus {
state: BoxState::Ready,
bind_cluster: None,
last_updated: Utc::now(),
},
}));
let pp = PatchParams::apply("kiss-controller").force();
let pp = PatchParams::apply("kiss-gateway").force();
api.patch(&name, &pp, &patch).await?;
api.patch_status(&name, &pp, &patch).await?;
}
Expand All @@ -128,6 +131,7 @@ async fn get_commission(client: Data<Arc<Client>>, Json(spec): Json<BoxSpec>) ->
spec,
status: Some(BoxStatus {
state: BoxState::Ready,
bind_cluster: None,
last_updated: Utc::now(),
}),
};
Expand Down
12 changes: 10 additions & 2 deletions kiss/monitor/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl ::kiss_api::manager::Ctx for Ctx {
.labels()
.get(AnsibleClient::LABEL_COMPLETED_STATE)
.and_then(|state| state.parse().ok());
let cluster = data
.labels()
.get(AnsibleClient::LABEL_TARGET_CLUSTER)
.cloned();

let has_completed = status.and_then(|e| e.succeeded).unwrap_or_default() > 0;
let has_failed = status.and_then(|e| e.failed).unwrap_or_default() > 0;
Expand All @@ -45,7 +49,7 @@ impl ::kiss_api::manager::Ctx for Ctx {
if has_completed {
// update the state
if let Some(completed_state) = completed_state {
Self::update_box_state(manager, data, completed_state).await
Self::update_box_state(manager, data, completed_state, cluster).await
}
// keep the state, scheduled by the controller
else {
Expand All @@ -58,7 +62,9 @@ impl ::kiss_api::manager::Ctx for Ctx {
else if has_failed {
let fallback_state = completed_state.unwrap_or(BoxState::Failed).fail();
match fallback_state {
BoxState::Failed => Self::update_box_state(manager, data, fallback_state).await,
BoxState::Failed => {
Self::update_box_state(manager, data, fallback_state, cluster).await
}
// do nothing when the job has no fallback state
_ => Ok(Action::requeue(
<Self as ::kiss_api::manager::Ctx>::FALLBACK,
Expand All @@ -79,6 +85,7 @@ impl Ctx {
manager: Arc<Manager<Self>>,
data: Arc<<Self as ::kiss_api::manager::Ctx>::Data>,
state: BoxState,
cluster: Option<String>,
) -> Result<Action, Error>
where
Self: Sized,
Expand All @@ -102,6 +109,7 @@ impl Ctx {
"kind": crd.kind,
"status": BoxStatus {
state,
bind_cluster: cluster,
last_updated: Utc::now(),
},
}));
Expand Down

0 comments on commit 06ebce1

Please sign in to comment.