Cluster nodes by name instead of (mutable) ip.

This commit is contained in:
2025-07-21 17:32:21 -07:00
parent 9d1ad5950b
commit c0b1d60e7b
7 changed files with 197 additions and 158 deletions

View File

@@ -5,19 +5,19 @@ set -o pipefail
# Usage function
usage() {
echo "Usage: wild-cluster-node-patch-generate <node-ip>"
echo "Usage: wild-cluster-node-patch-generate <node-name>"
echo ""
echo "Generate Talos machine configuration patches for a specific registered node."
echo ""
echo "Arguments:"
echo " node-ip IP address of the registered node"
echo " node-name Name of the registered node"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " wild-cluster-node-patch-generate 192.168.1.91"
echo " wild-cluster-node-patch-generate 192.168.1.100"
echo " wild-cluster-node-patch-generate control-1"
echo " wild-cluster-node-patch-generate worker-1"
echo ""
echo "This script will:"
echo " - Compile patch templates for the specified node"
@@ -32,7 +32,7 @@ usage() {
}
# Parse arguments
NODE_IP=""
NODE_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
@@ -45,8 +45,8 @@ while [[ $# -gt 0 ]]; do
exit 1
;;
*)
if [ -z "$NODE_IP" ]; then
NODE_IP="$1"
if [ -z "$NODE_NAME" ]; then
NODE_NAME="$1"
else
echo "Unexpected argument: $1"
usage
@@ -57,9 +57,9 @@ while [[ $# -gt 0 ]]; do
esac
done
# Check if node IP was provided
if [ -z "$NODE_IP" ]; then
echo "Error: Node IP address is required"
# Check if node name was provided
if [ -z "$NODE_NAME" ]; then
echo "Error: Node name is required"
usage
exit 1
fi
@@ -104,38 +104,46 @@ fi
# Get cluster configuration from config.yaml
CLUSTER_NAME=$(wild-config cluster.name)
print_info "Generating patch for node: $NODE_IP"
print_info "Generating patch for node: $NODE_NAME"
print_info "Cluster: $CLUSTER_NAME"
# Check if the specified node is registered
NODE_INTERFACE=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".interface" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_DISK=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".disk" "${WC_HOME}/config.yaml" 2>/dev/null)
IS_CONTROL=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".control" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_INTERFACE=$(yq eval ".cluster.nodes.active.\"${NODE_NAME}\".interface" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_DISK=$(yq eval ".cluster.nodes.active.\"${NODE_NAME}\".disk" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_ROLE=$(yq eval ".cluster.nodes.active.\"${NODE_NAME}\".role" "${WC_HOME}/config.yaml" 2>/dev/null)
NODE_CURRENT_IP=$(yq eval ".cluster.nodes.active.\"${NODE_NAME}\".currentIp" "${WC_HOME}/config.yaml" 2>/dev/null)
if [ -z "$NODE_INTERFACE" ] || [ "$NODE_INTERFACE" = "null" ]; then
print_error "Node $NODE_IP is not registered in config.yaml"
print_info "Please register the node first by running node hardware detection:"
print_info " wild-node-detect $NODE_IP"
print_info "Or run 'wild-setup' to register nodes interactively"
print_error "Node $NODE_NAME is not registered in config.yaml"
print_info "Please register the node first by running node hardware detection"
print_info "Or run 'wild-setup-cluster' to register nodes interactively"
exit 1
fi
# Get current IP for the node
if [ -z "$NODE_CURRENT_IP" ] || [ "$NODE_CURRENT_IP" = "null" ]; then
print_error "Node $NODE_NAME has no current IP address set"
exit 1
fi
# Determine node type
if [ "$IS_CONTROL" = "true" ]; then
if [ "$NODE_ROLE" = "controlplane" ]; then
NODE_TYPE="control"
print_success "Registered control plane node: $NODE_IP"
print_success "Registered control plane node: $NODE_NAME"
else
NODE_TYPE="worker"
print_success "Registered worker node: $NODE_IP"
print_success "Registered worker node: $NODE_NAME"
fi
print_info "Node details:"
print_info " - Name: $NODE_NAME"
print_info " - Current IP: $NODE_CURRENT_IP"
print_info " - Interface: $NODE_INTERFACE"
print_info " - Disk: $NODE_DISK"
print_info " - Type: $NODE_TYPE"
# Compile patch template for the specified node
print_info "Compiling patch template for $NODE_TYPE node $NODE_IP..."
print_info "Compiling patch template for $NODE_TYPE node $NODE_NAME..."
if [ "$NODE_TYPE" = "control" ]; then
TEMPLATE_FILE="${TEMPLATE_SOURCE_DIR}/patch.templates/controlplane.yaml"
@@ -143,12 +151,12 @@ else
TEMPLATE_FILE="${TEMPLATE_SOURCE_DIR}/patch.templates/worker.yaml"
fi
# Use IP as the patch name
PATCH_FILE="${NODE_SETUP_DIR}/patch/${NODE_IP}.yaml"
# Use node name as the patch name
PATCH_FILE="${NODE_SETUP_DIR}/patch/${NODE_NAME}.yaml"
# Create a temporary template with the node IP for gomplate processing
TEMP_TEMPLATE="/tmp/${NODE_IP//\//_}-$(date +%s).yaml"
sed "s/{{NODE_IP}}/${NODE_IP}/g" "$TEMPLATE_FILE" > "$TEMP_TEMPLATE"
# Create a temporary template with the node name and IP for gomplate processing
TEMP_TEMPLATE="/tmp/${NODE_NAME//\//_}-$(date +%s).yaml"
sed -e "s/{{NODE_NAME}}/${NODE_NAME}/g" -e "s/{{NODE_IP}}/${NODE_CURRENT_IP}/g" "$TEMPLATE_FILE" > "$TEMP_TEMPLATE"
cat "$TEMP_TEMPLATE" | wild-compile-template > "$PATCH_FILE"
rm -f "$TEMP_TEMPLATE"