Skip to content

Commit

Permalink
fix: ClusterBuilder order group
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Feb 9, 2024
1 parent 9fb4560 commit 9b9f829
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 312 deletions.
18 changes: 10 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.15"
version = "0.8.16"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/amphitheatre-app/amphitheatre"
Expand Down
2 changes: 2 additions & 0 deletions builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ amp-resources.workspace = true

kube.workspace = true
k8s-openapi.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
53 changes: 42 additions & 11 deletions builder/src/kpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ use crate::{errors::Error, Builder, Result};

use amp_common::{config::Credentials, resource::Actor};
use amp_resources::{
kpack::{cluster_builder, cluster_buildpack, cluster_store, image, syncer, BuildExt},
kpack::{
cluster_builder, cluster_buildpack, cluster_store, encode_name, image, syncer,
types::{find_top_level_buildpacks, Buildpack, Group, Order},
BuildExt,
},
volume,
};

use async_trait::async_trait;
use tokio::sync::RwLock;
use tracing::info;
use tracing::{debug, info};

/// Buildpacks builder implementation using Buildpacks kpack.
pub struct KpackBuilder {
Expand All @@ -46,7 +50,6 @@ impl Builder for KpackBuilder {
self.try_init_pvc().await.map_err(Error::ResourceError)?;
self.try_init_syncer().await.map_err(Error::ResourceError)?;
self.try_init_buildpack().await.map_err(Error::ResourceError)?;
self.try_init_store().await.map_err(Error::ResourceError)?;
self.try_init_builder().await.map_err(Error::ResourceError)?;

// Build or update the Image
Expand Down Expand Up @@ -95,16 +98,44 @@ impl KpackBuilder {
Ok(())
}

async fn try_init_store(&self) -> Result<(), amp_resources::error::Error> {
if !cluster_store::exists(&self.k8s, &self.actor).await? {
cluster_store::create(&self.k8s, &self.actor).await?;
}

Ok(())
}
async fn try_init_builder(&self) -> Result<(), amp_resources::error::Error> {
if !cluster_builder::exists(&self.k8s, &self.actor).await? {
cluster_builder::create(&self.k8s, &self.actor, self.credentials.clone()).await?;
if !cluster_store::exists(&self.k8s, &self.actor).await? {
cluster_store::create(&self.k8s, &self.actor).await?;
}

if !cluster_store::ready(&self.k8s, &self.actor).await? {
return Ok(()); // wait for the ClusterStore to be ready
}

let mut order = Vec::new();
let store = cluster_store::get(&self.k8s, &self.actor).await?;
if let Some(buildpacks) = store.data.pointer("/status/buildpacks") {
let buildpacks: Vec<Buildpack> = serde_json::from_value(buildpacks.clone())
.map_err(amp_resources::error::Error::SerializationError)?;
order = find_top_level_buildpacks(&buildpacks);
}

if let Some(buildpacks) = self.actor.spec.character.buildpacks() {
order.append(
&mut buildpacks
.iter()
.map(|item| Order {
group: vec![Group {
name: Some(encode_name(item)),
kind: Some("ClusterBuildpack".to_string()),
..Default::default()
}],
})
.collect(),
);
}

debug!("The ClusterBuilder order: {:?}", order);

let credentials = self.credentials.read().await;
let tag = self.actor.spec.character.builder_tag(&credentials)?;
cluster_builder::create(&self.k8s, &self.actor, &tag, order).await?;
}

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions resources/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub enum Error {

#[error("MissingBuilder")]
MissingBuilder,

#[error("ClusterStoreNotReady")]
ClusterStoreNotReady,
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
Loading

0 comments on commit 9b9f829

Please sign in to comment.