From 312c8430a80b158e51698ff9d9e05717472d6785 Mon Sep 17 00:00:00 2001 From: Eguo Wang Date: Thu, 12 Oct 2023 15:42:49 +0800 Subject: [PATCH] Adjusted to patch `source` and `image` fields only when actor is live --- Cargo.lock | 10 +++++----- Cargo.toml | 2 +- controllers/src/playbook_controller.rs | 14 +++++++------- resolver/src/lib.rs | 23 ++++++++++++++--------- resolver/src/preface.rs | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ffcdafb..cc57c50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,7 +61,7 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" [[package]] name = "amp-apiserver" -version = "0.5.7" +version = "0.5.8" dependencies = [ "amp-common", "amp-resources", @@ -122,7 +122,7 @@ dependencies = [ [[package]] name = "amp-controllers" -version = "0.5.7" +version = "0.5.8" dependencies = [ "amp-common", "amp-resolver", @@ -159,7 +159,7 @@ dependencies = [ [[package]] name = "amp-resolver" -version = "0.5.7" +version = "0.5.8" dependencies = [ "amp-common", "amp-resources", @@ -173,7 +173,7 @@ dependencies = [ [[package]] name = "amp-resources" -version = "0.5.7" +version = "0.5.8" dependencies = [ "amp-common", "k8s-metrics", @@ -192,7 +192,7 @@ dependencies = [ [[package]] name = "amp-syncer" -version = "0.5.7" +version = "0.5.8" dependencies = [ "amp-common", "async-nats", diff --git a/Cargo.toml b/Cargo.toml index d6c7147..78ebd5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace.package] -version = "0.5.7" +version = "0.5.8" edition = "2021" license = "Apache-2.0" repository = "https://github.com/amphitheatre-app/amphitheatre" diff --git a/controllers/src/playbook_controller.rs b/controllers/src/playbook_controller.rs index 2051809..0d44c49 100644 --- a/controllers/src/playbook_controller.rs +++ b/controllers/src/playbook_controller.rs @@ -134,14 +134,14 @@ async fn resolve(playbook: &Playbook, ctx: &Arc, recorder: &Recorder) - tracing::debug!("The repositories to be fetched are: {fetches:?}"); for (name, partner) in fetches.iter() { - let actor = resolver::partner::load(&ctx.k8s, &credentials, name, partner) + let character = resolver::partner::load(&ctx.k8s, &credentials, name, partner) .await .map_err(Error::ResolveError)?; let message = "Fetch and add the actor to this playbook"; trace(recorder, message).await.map_err(Error::ResourceError)?; - playbook::add(&ctx.k8s, playbook, actor) + playbook::add(&ctx.k8s, playbook, character) .await .map_err(Error::ResourceError)?; } @@ -191,17 +191,17 @@ async fn run(playbook: &Playbook, ctx: &Arc, recorder: &Recorder) -> Re let message = format!("Try to refresh an existing Actor {}", name); trace(recorder, message).await.map_err(Error::ResourceError)?; - let spec = resolver::to_actor(&credentials, character).map_err(Error::ResolveError)?; + let spec = resolver::to_actor(character, &credentials).map_err(Error::ResolveError)?; actor::update(&ctx.k8s, playbook, &spec) .await .map_err(Error::ResourceError)?; } false => { // Create a new actor - trace(recorder, format!("Create new Actor: {}", name)) - .await - .map_err(Error::ResourceError)?; - let spec = resolver::to_actor(&credentials, character).map_err(Error::ResolveError)?; + let message = format!("Create new Actor: {}", name); + trace(recorder, message).await.map_err(Error::ResourceError)?; + + let spec = resolver::to_actor(character, &credentials).map_err(Error::ResolveError)?; actor::create(&ctx.k8s, playbook, &spec) .await .map_err(Error::ResourceError)?; diff --git a/resolver/src/lib.rs b/resolver/src/lib.rs index 3ee19b7..8e6b6cc 100644 --- a/resolver/src/lib.rs +++ b/resolver/src/lib.rs @@ -29,7 +29,7 @@ pub mod utils; const CATALOG_REPO_URL: &str = "https://github.com/amphitheatre-app/catalog.git"; -/// Load mainfest from catalog and return the actor spec. +/// Load manifest from catalog and return the actor spec. pub fn load_from_catalog(credentials: &Credentials, name: &str, version: &str) -> Result { let reference = GitReference { repo: CATALOG_REPO_URL.to_string(), @@ -40,7 +40,7 @@ pub fn load_from_catalog(credentials: &Credentials, name: &str, version: &str) - load_from_source(credentials, &reference) } -/// Load mainfest from remote VCS (like github) and return the actor spec. +/// Load manifest from remote VCS (like github) and return the actor spec. pub fn load_from_source(credentials: &Credentials, reference: &GitReference) -> Result { let client = ScmClient::init(credentials, &reference.repo).map_err(ResolveError::SCMError)?; @@ -59,7 +59,7 @@ pub fn load_from_source(credentials: &Credentials, reference: &GitReference) -> Ok(CharacterSpec::from(manifest)) } -/// Load mainfest from Kubernetes cluster and return the actor spec. +/// Load manifest from Kubernetes cluster and return the actor spec. pub async fn load_from_cluster(client: &KubeClient, name: &str) -> Result { let character = character::get(client, name) .await @@ -68,12 +68,17 @@ pub async fn load_from_cluster(client: &KubeClient, name: &str) -> Result Result { - let client = ScmClient::init(credentials, &manifest.meta.repository).map_err(ResolveError::SCMError)?; +pub fn to_actor(character: &CharacterSpec, credentials: &Credentials) -> Result { + let client = ScmClient::init(credentials, &character.meta.repository).map_err(ResolveError::SCMError)?; - let mut spec = ActorSpec::from(manifest); - spec.source = Some(patches::source(&client, &spec.source.unwrap())?); - spec.image = patches::image(credentials, &spec)?; + let mut actor = ActorSpec::from(character); - Ok(spec) + // Patch the source and image if the actor is not live. + // it will be build with the builders later, so these must be valid. + if !actor.live { + actor.source = Some(patches::source(&client, &actor.source.unwrap())?); + actor.image = patches::image(credentials, &actor)?; + } + + Ok(actor) } diff --git a/resolver/src/preface.rs b/resolver/src/preface.rs index ee2b527..a88058c 100644 --- a/resolver/src/preface.rs +++ b/resolver/src/preface.rs @@ -19,7 +19,7 @@ use crate::{ use amp_common::{config::Credentials, resource::CharacterSpec, resource::Preface}; use kube::Client as KubeClient; -/// Load mainfest from different sources and return the actor spec. +/// Load manifest from different sources and return the actor spec. pub async fn load(client: &KubeClient, credentials: &Credentials, preface: &Preface) -> Result { if let Some(p) = &preface.registry { let registry = p.registry.clone().unwrap_or_else(|| "catalog".to_string());