Skip to content

Commit

Permalink
more code and comment cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: catsby <[email protected]>
  • Loading branch information
catsby committed Dec 12, 2024
1 parent 2caccc3 commit f695335
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/pepr/operator/controllers/network/generators/allNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import { UDSConfig } from "../../../../config";
// configure subproject logger
const log = setupLogger(Component.OPERATOR_GENERATORS);

// This is an in-memory cache of the API server CIDR
// nodePolicies is a list of all the nodes in the cluster in egress:to format
let nodePolicies: V1NetworkPolicyPeer[];
// nodeSet is a set of all the nodes in the cluster, to ensure uniqueness and
// easily add/remove nodes
const nodeSet = new Set<string>();

/**
* Initialize the API server CIDR by getting the EndpointSlice and Service for the API server
* Initialize the node CIDRs by getting the initial list of nodes
*/
export async function initAllNodesTarget() {
try {
const nodes = await fetchKubernetesNodes();
// reset the node policies, which shouldn't be necessary but doesn't hurt
nodePolicies = [];
// Iterate through the nodes and extract their IP addresses
nodes.items.forEach((node: kind.Node) => {
const addresses = node.status?.addresses;

// Extract the IP address (typically "InternalIP")
const internalIP = addresses?.find(
(addr: V1NodeAddress) => addr.type === "InternalIP",
)?.address;
Expand Down Expand Up @@ -65,21 +66,6 @@ export function nodeCIDRs(): V1NetworkPolicyPeer[] {
return [anywhere];
}

/**
* When the kubernetes node is deleted, update the node CIDRs
* @param slice The node that was deleted
*/
export async function updateKubeNodesFromDelete(node: kind.Node) {
const addresses = node.status?.addresses;

const internalIP = addresses?.find((addr: V1NodeAddress) => addr.type === "InternalIP")?.address;
if (internalIP) {
nodeSet.delete(internalIP);
}
buildNodePolicies(Array.from(nodeSet));
await updateKubeNodesNetworkPolicies(nodePolicies);
}

/**
* Update the node CIDRs when a node is created or updated
*
Expand All @@ -103,6 +89,22 @@ export async function updateKubeNodesFromCreateUpdate(node: kind.Node) {
await updateKubeNodesNetworkPolicies(nodePolicies);
}

/**
* Update the node CIDRs when a node is deleted
*
* @param slice The node that was deleted
*/
export async function updateKubeNodesFromDelete(node: kind.Node) {
const addresses = node.status?.addresses;

const internalIP = addresses?.find((addr: V1NodeAddress) => addr.type === "InternalIP")?.address;
if (internalIP) {
nodeSet.delete(internalIP);
}
buildNodePolicies(Array.from(nodeSet));
await updateKubeNodesNetworkPolicies(nodePolicies);
}

/**
* Fetches the Kubernetes Nodes. This is used to get the internal IPs of all nodes.
*
Expand Down

0 comments on commit f695335

Please sign in to comment.