Skip to content

Commit

Permalink
fix(shulker-operator): keep extra annotations when reconciling a reso…
Browse files Browse the repository at this point in the history
…urce
  • Loading branch information
jeremylvln committed Oct 25, 2023
1 parent 279511d commit 229b50c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

54 changes: 33 additions & 21 deletions packages/shulker-operator/src/reconcilers/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ pub trait ResourceBuilder {
) -> Result<(), anyhow::Error>;
}

async fn does_exists<
async fn get_existing<
R: kube::Resource<DynamicType = ()> + Clone + Serialize + DeserializeOwned + Debug,
>(
api: &Api<R>,
name: &str,
) -> Result<bool, anyhow::Error> {
api.get(name).await.map(|_| true).or_else(|e| match e {
) -> Result<Option<R>, anyhow::Error> {
api.get(name).await.map(|r| Some(r)).or_else(|e| match e {
kube::Error::Api(e) => match e.code {
404 => Ok(false),
404 => Ok(None),
_ => Err(e.into()),
},
e => Err(e.into()),
Expand Down Expand Up @@ -81,12 +81,12 @@ pub async fn reconcile_builder<
"reconciling builder",
);

let does_exists = does_exists(&api, &name)
let existing_resource = get_existing(&api, &name)
.await
.map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e))?;

if !builder.is_needed(owner) {
if does_exists {
if existing_resource.is_some() {
api.delete(&name, &DeleteParams::default())
.await
.map_err(|e| {
Expand All @@ -97,37 +97,49 @@ pub async fn reconcile_builder<
return Ok(None);
}

let mut resource = builder
let mut new_resource = builder
.create(owner, &name)
.await
.map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e))?;

builder
.update(owner, &mut resource)
.update(owner, &mut new_resource)
.await
.map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e))?;

let updated_resource = if RB::is_updatable() && does_exists {
api.patch(
&name,
&PatchParams::apply("shulker-operator").force(),
&Patch::Apply(&resource),
)
.await
.map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e.into()))?
} else if !does_exists {
set_controller_reference(owner, &mut resource);
let updated_resource = if let Some(existing_resource) = existing_resource {
if RB::is_updatable() {
for (key, value) in existing_resource.annotations().iter() {
if !new_resource.annotations().contains_key(key) {
new_resource
.annotations_mut()
.insert(key.clone(), value.clone());
}
}

api.patch(
&name,
&PatchParams::apply("shulker-operator").force(),
&Patch::Apply(&existing_resource),
)
.await
.map_err(|e| {
super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e.into())
})?
} else {
existing_resource
}
} else {
set_controller_reference(owner, &mut new_resource);
api.create(
&PostParams {
dry_run: false,
field_manager: Some("shulker-operator".to_string()),
},
&resource,
&new_resource,
)
.await
.map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::<RB>(), e.into()))?
} else {
resource
};

Ok(Some(updated_resource))
Expand Down

0 comments on commit 229b50c

Please sign in to comment.