From dc8141e6d5e814f8e50c6d0b7b5405b7f1e35e31 Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Sun, 6 Jul 2025 09:26:00 -0700 Subject: [PATCH] Better cluster-node lifecycle. --- ...erate => wild-cluster-node-patch-generate} | 68 +++++------------- bin/wild-cluster-node-up | 71 ++++++++++++++++--- 2 files changed, 79 insertions(+), 60 deletions(-) rename bin/{wild-cluster-node-machine-config-generate => wild-cluster-node-patch-generate} (64%) diff --git a/bin/wild-cluster-node-machine-config-generate b/bin/wild-cluster-node-patch-generate similarity index 64% rename from bin/wild-cluster-node-machine-config-generate rename to bin/wild-cluster-node-patch-generate index 3725cc8..69ce7c2 100755 --- a/bin/wild-cluster-node-machine-config-generate +++ b/bin/wild-cluster-node-patch-generate @@ -1,13 +1,13 @@ -#\!/bin/bash +#!/bin/bash set -e set -o pipefail # Usage function usage() { - echo "Usage: wild-cluster-node-machine-config-generate " + echo "Usage: wild-cluster-node-patch-generate " echo "" - echo "Generate Talos machine configuration for a specific registered node." + echo "Generate Talos machine configuration patches for a specific registered node." echo "" echo "Arguments:" echo " node-ip IP address of the registered node" @@ -16,15 +16,13 @@ usage() { echo " -h, --help Show this help message" echo "" echo "Examples:" - echo " wild-cluster-node-machine-config-generate 192.168.1.91" - echo " wild-cluster-node-machine-config-generate 192.168.1.100" + echo " wild-cluster-node-patch-generate 192.168.1.91" + echo " wild-cluster-node-patch-generate 192.168.1.100" echo "" echo "This script will:" - echo " - Generate initial cluster secrets if not present" - echo " - Use patch templates from the wild-cloud repository" - echo " - Create machine configuration for the specified node" - echo " - Generate patched config with node-specific hardware settings" - echo " - Update talosctl context with the node" + echo " - Compile patch templates for the specified node" + echo " - Generate node-specific patch files in WC_HOME/setup/cluster-nodes/patch/" + echo " - Use hardware details from the node registration" echo "" echo "Requirements:" echo " - Must be run from a wild-cloud directory" @@ -79,19 +77,15 @@ prompt_if_unset_config "cluster.name" "Cluster name" "local.example.com" # Function to ensure required directories exist in WC_HOME ensure_required_directories() { - # Create output directories in WC_HOME for patch and final configs + # Create output directories in WC_HOME for patch configs mkdir -p "${WC_HOME}/setup/cluster-nodes/patch" - mkdir -p "${WC_HOME}/setup/cluster-nodes/final" - - # Ensure the generated directory exists (for cluster secrets) - mkdir -p "${WC_HOME}/setup/cluster-nodes/generated" } # ============================================================================= -# MACHINE CONFIG GENERATION +# PATCH GENERATION # ============================================================================= -print_header "Talos Machine Config Generation" +print_header "Talos Machine Config Patch Generation" # Ensure required directories exist in WC_HOME ensure_required_directories @@ -109,9 +103,8 @@ fi # Get cluster configuration from config.yaml CLUSTER_NAME=$(wild-config cluster.name) -VIP=$(wild-config cluster.nodes.control.vip) -print_info "Generating machine configuration for node: $NODE_IP" +print_info "Generating patch for node: $NODE_IP" print_info "Cluster: $CLUSTER_NAME" # Check if the specified node is registered @@ -146,15 +139,12 @@ print_info "Compiling patch template for $NODE_TYPE node $NODE_IP..." if [ "$NODE_TYPE" = "control" ]; then TEMPLATE_FILE="${TEMPLATE_SOURCE_DIR}/patch.templates/controlplane.yaml" - BASE_CONFIG="${NODE_SETUP_DIR}/generated/controlplane.yaml" else TEMPLATE_FILE="${TEMPLATE_SOURCE_DIR}/patch.templates/worker.yaml" - BASE_CONFIG="${NODE_SETUP_DIR}/generated/worker.yaml" fi -# Use IP as the patch name and output config name +# Use IP as the patch name PATCH_FILE="${NODE_SETUP_DIR}/patch/${NODE_IP}.yaml" -OUTPUT_CONFIG="${NODE_SETUP_DIR}/final/${NODE_IP}.yaml" # Create a temporary template with the node IP for gomplate processing TEMP_TEMPLATE="/tmp/${NODE_IP//\//_}-$(date +%s).yaml" @@ -162,35 +152,11 @@ sed "s/{{NODE_IP}}/${NODE_IP}/g" "$TEMPLATE_FILE" > "$TEMP_TEMPLATE" cat "$TEMP_TEMPLATE" | wild-compile-template > "$PATCH_FILE" rm -f "$TEMP_TEMPLATE" -# Generate final machine config for the specified node -print_info "Generating final machine configuration..." -talosctl machineconfig patch "$BASE_CONFIG" --patch @"$PATCH_FILE" -o "$OUTPUT_CONFIG" - -# Update talosctl context with this node -print_info "Updating talosctl context..." -talosctl config node "$NODE_IP" - -print_success "Machine configuration generated successfully!" +print_success "Patch generated successfully!" echo "" -print_info "Generated files:" -print_info " - Patch: $PATCH_FILE" -print_info " - Final config: $OUTPUT_CONFIG" +print_info "Generated patch file:" +print_info " - $PATCH_FILE" echo "" print_info "Template used: ${TEMPLATE_FILE}" -echo "" -print_info "Next steps:" -echo " 1. Apply configuration to the node:" -echo " talosctl apply-config -i -n $NODE_IP -f $OUTPUT_CONFIG" -echo "" -if [ "$NODE_TYPE" = "control" ]; then - echo " 2. If this is your first control plane node, bootstrap it:" - echo " talosctl bootstrap -n $NODE_IP" - echo "" - echo " 3. Get kubeconfig when cluster is ready:" - echo " talosctl kubeconfig" -else - echo " 2. Node will join the cluster automatically after applying config" -fi - -print_success "Machine config generation completed!" \ No newline at end of file +print_success "Patch generation completed!" \ No newline at end of file diff --git a/bin/wild-cluster-node-up b/bin/wild-cluster-node-up index f0b68cc..72be5f9 100755 --- a/bin/wild-cluster-node-up +++ b/bin/wild-cluster-node-up @@ -14,30 +14,33 @@ usage() { echo "" echo "Options:" echo " -i, --insecure Apply configuration in insecure mode (for maintenance mode nodes)" + echo " --skip-patch Skip automatic patch generation and use existing final config" echo " --dry-run Show the command that would be executed without running it" echo " -h, --help Show this help message" echo "" echo "Examples:" echo " wild-cluster-node-up 192.168.1.91" echo " wild-cluster-node-up 192.168.1.100 --insecure" + echo " wild-cluster-node-up 192.168.1.100 --skip-patch" echo " wild-cluster-node-up 192.168.1.100 --dry-run" echo "" echo "This script will:" echo " - Verify the node is registered in config.yaml" - echo " - Check that a machine configuration exists for the node" + echo " - Generate final machine configuration if needed" echo " - Apply the configuration using talosctl apply-config" echo " - Use insecure mode for nodes in maintenance mode" echo "" echo "Requirements:" echo " - Must be run from a wild-cloud directory" echo " - Node must be registered (hardware detected) first" - echo " - Machine configuration must exist for the node" + echo " - Base cluster configuration and patch file must exist for the node" } # Parse arguments NODE_IP="" INSECURE_MODE=false DRY_RUN=false +SKIP_PATCH=false while [[ $# -gt 0 ]]; do case $1 in @@ -45,6 +48,10 @@ while [[ $# -gt 0 ]]; do INSECURE_MODE=true shift ;; + --skip-patch) + SKIP_PATCH=true + shift + ;; --dry-run) DRY_RUN=true shift @@ -139,19 +146,60 @@ if [ -n "$MAINTENANCE_IP" ] && [ "$MAINTENANCE_IP" != "null" ]; then print_info " - Maintenance IP: $MAINTENANCE_IP" fi -# Check if machine config exists +# Check if machine config exists, generate if needed NODE_SETUP_DIR="${WC_HOME}/setup/cluster-nodes" CONFIG_FILE="${NODE_SETUP_DIR}/final/${NODE_IP}.yaml" +PATCH_FILE="${NODE_SETUP_DIR}/patch/${NODE_IP}.yaml" if [ ! -f "$CONFIG_FILE" ]; then - print_error "Machine configuration not found: $CONFIG_FILE" - print_info "Generate the machine configuration first:" - print_info " wild-cluster-node-machine-config-generate $NODE_IP" - exit 1 + if [ "$SKIP_PATCH" = true ]; then + print_error "Machine configuration not found: $CONFIG_FILE" + print_info "--skip-patch was specified but no existing config found" + print_info "Either generate the configuration first or remove --skip-patch:" + print_info " wild-cluster-node-machine-config-generate $NODE_IP" + exit 1 + fi + + print_info "Machine configuration not found: $CONFIG_FILE" + print_info "Generating final machine configuration..." + + # Check if patch file exists + if [ ! -f "$PATCH_FILE" ]; then + print_error "Patch file not found: $PATCH_FILE" + print_info "Generate the patch file first:" + print_info " wild-cluster-node-patch-generate $NODE_IP" + exit 1 + fi + + # Determine base config file + if [ "$IS_CONTROL" = "true" ]; then + BASE_CONFIG="${NODE_SETUP_DIR}/generated/controlplane.yaml" + else + BASE_CONFIG="${NODE_SETUP_DIR}/generated/worker.yaml" + fi + + # Check if base config exists + if [ ! -f "$BASE_CONFIG" ]; then + print_error "Base configuration not found: $BASE_CONFIG" + print_info "Generate base cluster configuration first:" + print_info " wild-cluster-config-generate" + exit 1 + fi + + # Create final config directory if it doesn't exist + mkdir -p "${NODE_SETUP_DIR}/final" + + # Generate final machine config + print_info "Generating final machine configuration from patch..." + talosctl machineconfig patch "$BASE_CONFIG" --patch @"$PATCH_FILE" -o "$CONFIG_FILE" + print_success "Generated machine configuration: $CONFIG_FILE" +else + print_success "Found existing machine configuration: $CONFIG_FILE" + if [ "$SKIP_PATCH" = true ]; then + print_info "--skip-patch specified: using existing configuration without regeneration" + fi fi -print_success "Found machine configuration: $CONFIG_FILE" - # Build talosctl command TALOSCTL_CMD="talosctl apply-config" @@ -179,6 +227,11 @@ echo "" if eval "$TALOSCTL_CMD"; then print_success "Machine configuration applied successfully!" + + # Update talosctl context to this node + print_info "Updating talosctl context..." + talosctl config node "$NODE_IP" + print_success "Updated talosctl context to node $NODE_IP" echo "" if [ "$IS_CONTROL" = "true" ]; then