Skip to content

Commit

Permalink
Changes from Code Review
Browse files Browse the repository at this point in the history
(cherry picked from commit 990a0b7)
  • Loading branch information
lieberlois authored and swoehrl-mw committed Jan 20, 2023
1 parent 37321df commit ee5ac65
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 71 deletions.
3 changes: 1 addition & 2 deletions src/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::events::init_event_watcher;
use crate::manager::Manager;
use crate::policy::{load_policies_from_file, PolicyInfo, PolicyStore, PolicyStoreRef};
use crate::util::error::{kube_err, load_err, BridgekeeperError, Result};
use crate::util::k8s_client::{list_with_retry, patch_status_with_retry};
use crate::util::k8s::{list_with_retry, patch_status_with_retry, namespaces, find_k8s_resource_matches, gen_target_identifier};
use crate::util::defaults::api_group_or_default;
use crate::util::k8s_util::{namespaces, find_k8s_resource_matches, gen_target_identifier};
use argh::FromArgs;
use k8s_openapi::chrono::{DateTime, Utc};
use kube::{
Expand Down
2 changes: 1 addition & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
crd::{Policy, PolicySpec},
events::{EventSender, PolicyEvent, PolicyEventData},
policy::{PolicyInfo, PolicyStoreRef}, util::k8s_util::find_k8s_resource_matches,
policy::{PolicyInfo, PolicyStoreRef}, util::k8s::find_k8s_resource_matches,
};
use kube::{core::{
admission::{self, Operation},
Expand Down
67 changes: 63 additions & 4 deletions src/util/k8s_util.rs → src/util/k8s.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
use crate::util::error::{kube_err, Result};
use k8s_openapi::{api::core::v1::Namespace, apimachinery::pkg::apis::meta::v1::{APIGroup, APIResource}};
use exponential_backoff::Backoff;
use kube::{
api::{Api, ListParams, Patch, PatchParams},
core::ObjectList,
Resource, Client,
core::{ApiResource as KubeApiResource, DynamicObject, GroupVersionKind},
Client, api::ListParams, Api, Resource
};
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Serialize};
use std::time::Duration;

use crate::util::error::{kube_err, Result};
use k8s_openapi::{api::core::v1::Namespace, apimachinery::pkg::apis::meta::v1::{APIGroup, APIResource}};

lazy_static! {
static ref BACKOFF: Backoff =
Backoff::new(4, Duration::from_millis(100), Duration::from_secs(2));
}

pub async fn list_with_retry<T>(api: &Api<T>, params: ListParams) -> kube::Result<ObjectList<T>>
where
T: DeserializeOwned + Clone + std::fmt::Debug,
{
for duration in BACKOFF.iter() {
match api.list(&params).await {
Ok(result) => return Ok(result),
Err(_err) => tokio::time::sleep(duration).await,
}
}
api.list(&params).await
}

pub async fn patch_status_with_retry<
T: DeserializeOwned + Clone + std::fmt::Debug,
P: serde::Serialize + std::fmt::Debug,
>(
api: &Api<T>,
name: &str,
pp: &PatchParams,
patch: &Patch<P>,
) -> kube::Result<T> {
for duration in BACKOFF.iter() {
match api.patch_status(name, pp, patch).await {
Ok(result) => return Ok(result),
Err(_err) => tokio::time::sleep(duration).await,
}
}
api.patch_status(name, pp, patch).await
}

pub async fn apply<T: Resource>(api: &Api<T>, name: &str, mut object: T) -> kube::Result<T>
where
<T as Resource>::DynamicType: Default,
T: Clone,
T: Serialize,
T: DeserializeOwned,
T: std::fmt::Debug,
{
if let Ok(res) = api.get(name).await {
object.meta_mut().resource_version = res.meta().resource_version.clone();
api.replace(name, &Default::default(), &object).await
} else {
api.create(&Default::default(), &object).await
}
}

pub async fn find_k8s_resource_matches(
api_group: &str,
Expand Down Expand Up @@ -130,4 +189,4 @@ pub async fn namespaces(k8s_client: Client) -> Result<Vec<String>> {
}
}
Ok(namespaces)
}
}
61 changes: 0 additions & 61 deletions src/util/k8s_client.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod cert;
pub mod error;
pub mod k8s_client;
pub mod k8s_util;
pub mod k8s;
pub mod webhook;
pub mod defaults;
2 changes: 1 addition & 1 deletion src/util/webhook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::util::error::{kube_err, Result};
use crate::{constants::*, util::cert::CertKeyPair, util::k8s_client::apply};
use crate::{constants::*, util::cert::CertKeyPair, util::k8s::apply};
use k8s_openapi::api::admissionregistration::v1::{
MutatingWebhookConfiguration, ValidatingWebhookConfiguration,
};
Expand Down

0 comments on commit ee5ac65

Please sign in to comment.