Skip to content

Commit

Permalink
fix: init task not match return None error
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Feb 24, 2024
1 parent 81a07d7 commit 823453f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.8.21"
version = "0.8.22"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/amphitheatre-app/amphitheatre"
Expand Down
7 changes: 6 additions & 1 deletion resources/src/kpack/syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ pub async fn ready(client: &Client, actor: &Actor) -> Result<bool> {
let pod = result.unwrap();
let status = pod.status.as_ref().ok_or_else(|| Error::MissingObjectKey(".status"))?;

// Check if the Pod phase is Succeeded
if let Some(phase) = &status.phase {
return Ok(phase == "Succeeded");
}

if let Some(conditions) = &status.conditions {
return Ok(conditions.iter().any(|c| c.type_ == "Ready" && c.status == "True"));
return Ok(conditions.iter().any(|c| c.type_ == "Running" && c.status == "True"));
}

Ok(false)
Expand Down
32 changes: 22 additions & 10 deletions workflow/src/actor/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::time::Duration;

use crate::actor::{BuildingState, DeployingState};
use crate::errors::{Error, Result};
use crate::{Context, Intent, State, Task};

Expand All @@ -20,11 +23,10 @@ use amp_common::resource::{Actor, ActorState};

use amp_resources::actor;
use async_trait::async_trait;
use kube::runtime::controller::Action;
use kube::ResourceExt;
use tracing::{error, info, trace};

use super::{BuildingState, DeployingState};

pub struct InitialState;

#[async_trait]
Expand All @@ -35,16 +37,25 @@ impl State<Actor> for InitialState {

// Check if InitTask should be executed
let task = InitTask::new();
if !task.matches(ctx) {
return None;
if task.matches(ctx) {
match task.execute(ctx).await {
Ok(Some(intent)) => return Some(intent),
Err(err) => error!("Error during DeployTask execution: {}", err),
Ok(None) => {}
}
}

let result = task.execute(ctx).await;
if let Err(err) = &result {
error!("Error during InitTask execution: {}", err);
// Transition to the building state if status of actor is building
if ctx.object.status.as_ref().is_some_and(|status| status.building()) {
return Some(Intent::State(Box::new(BuildingState)));
}

result.ok().and_then(|intent| intent)
// Transition to the deploying state if status of actor is running
if ctx.object.status.as_ref().is_some_and(|status| status.running()) {
return Some(Intent::State(Box::new(DeployingState)));
}

None
}
}

Expand All @@ -68,13 +79,14 @@ impl Task<Actor> for InitTask {
if actor.spec.live || !self.built(ctx).await? {
let condition = ActorState::building();
actor::patch_status(&ctx.k8s, &ctx.object, condition).await.map_err(Error::ResourceError)?;
Ok(Some(Intent::State(Box::new(BuildingState))))
} else {
// patch the status to running
let condition = ActorState::running(true, "AutoRun", None);
actor::patch_status(&ctx.k8s, &ctx.object, condition).await.map_err(Error::ResourceError)?;
Ok(Some(Intent::State(Box::new(DeployingState))))
}

// Requeue immediately
Ok(Some(Intent::Action(Action::requeue(Duration::ZERO))))
}
}

Expand Down

0 comments on commit 823453f

Please sign in to comment.