Skip to content

Commit

Permalink
egress improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanharmon committed Sep 13, 2024
1 parent ea2d3cb commit 20f85c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
44 changes: 14 additions & 30 deletions eip_operator/src/egress.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::eip::v2::Eip;
use crate::Error;
use k8s_openapi::api::core::v1::Node;
use kube::api::{Api, ListParams, Patch, PatchParams};
use kube::api::{Api, Patch, PatchParams};
use kube::ResourceExt;
use tracing::{info, instrument};

use crate::EGRESS_GATEWAY_NODE_SELECTOR_LABEL_KEY;
use crate::EGRESS_GATEWAY_NODE_SELECTOR_LABEL_VALUE;

/// Applies label specifying the ready status of the egress gateway node.
#[instrument(skip(api), err)]
pub(crate) async fn add_gateway_status_label(
Expand Down Expand Up @@ -36,37 +33,20 @@ pub(crate) async fn add_gateway_status_label(
api.patch(name, &params, &patch).await
}

/// Retrieve all egress nodes in the cluster.
async fn get_egress_nodes(api: &Api<Node>) -> Result<Vec<Node>, kube::Error> {
let params = ListParams::default().labels(
format!(
"{}={}",
EGRESS_GATEWAY_NODE_SELECTOR_LABEL_KEY, EGRESS_GATEWAY_NODE_SELECTOR_LABEL_VALUE
)
.as_str(),
);

match api.list(&params).await {
Ok(node_list) => Ok(node_list.items),
Err(e) => Err(e),
}
}

/// Update the ready status label on egress nodes.
/// This controls whether traffic is allowed to egress through the node.
/// Note: Egress traffic will be immediately dropped when the ready status label value is changed away from "true".
#[instrument(skip(), err)]
pub(crate) async fn label_egress_nodes(eip: &Eip, node_api: &Api<Node>) -> Result<(), Error> {
let egress_nodes = get_egress_nodes(&Api::all(node_api.clone().into())).await?;
let egress_nodes = node_api.list(&eip.get_resource_list_params()).await?.items;
if egress_nodes.is_empty() {
info!("No egress nodes found. Skipping egress node ready status labeling.");
return Ok(());
}

// Note(Evan): find nodes that match eips we're reconciling
// if eip has a resource id, see if the node with the resoruce is ready
// if no, do nothing
// if yes, mark that node as egress_ready=true, and mark all other nodes as egress_ready=false
// If EIP being reconciled has a resource id, find it's node, and check if it's ready.
// If not ready, return early.
// If ready, mark that node as egress_ready=true, and mark all other nodes as egress_ready=false.
if let Some(resource_id) = eip.status.as_ref().and_then(|s| s.resource_id.as_ref()) {
let node = egress_nodes
.iter()
Expand All @@ -91,12 +71,16 @@ pub(crate) async fn label_egress_nodes(eip: &Eip, node_api: &Api<Node>) -> Resul
// mark node with EIP as ready for egress traffic
add_gateway_status_label(node_api, node.name_unchecked().as_str(), "true").await?;
// mark all other nodes as not ready for egress traffic
for other_node in egress_nodes
.iter()
.filter(|n| n.name_unchecked() != node.name_unchecked())
{
add_gateway_status_label(node_api, other_node.name_unchecked().as_str(), "false")
for other_node in &egress_nodes {
// skip the node we just set to ready
if other_node.name_unchecked() != node.name_unchecked() {
add_gateway_status_label(
node_api,
other_node.name_unchecked().as_str(),
"false",
)
.await?;
}
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions eip_operator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const FIELD_MANAGER: &str = "eip.materialize.cloud";
const AUTOCREATE_EIP_LABEL: &str = "eip.materialize.cloud/autocreate_eip";
const EIP_ALLOCATION_ID_ANNOTATION: &str = "eip.materialize.cloud/allocation_id";
const EXTERNAL_DNS_TARGET_ANNOTATION: &str = "external-dns.alpha.kubernetes.io/target";
const EGRESS_GATEWAY_NODE_SELECTOR_LABEL_KEY: &str = "workload";
const EGRESS_GATEWAY_NODE_SELECTOR_LABEL_VALUE: &str = "materialize-egress";
const EGRESS_NODE_STATUS_LABEL: &str = "egress-gateway.materialize.cloud/ready-status";

// See https://us-east-1.console.aws.amazon.com/servicequotas/home/services/ec2/quotas
Expand Down

0 comments on commit 20f85c3

Please sign in to comment.