|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Ctrlplane Local Deployment Script |
| 4 | +# This script deploys the ctrlplane Helm chart to your local Kubernetes cluster |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +# Configuration |
| 9 | +CHART_NAME="ctrlplane" |
| 10 | +RELEASE_NAME="ctrlplane" |
| 11 | +NAMESPACE="default" |
| 12 | +CHART_PATH="./charts/ctrlplane" |
| 13 | +VALUES_FILE="./charts/ctrlplane/local-values.yaml" |
| 14 | +DRY_RUN=false |
| 15 | + |
| 16 | +# Colors for output |
| 17 | +RED='\033[0;31m' |
| 18 | +GREEN='\033[0;32m' |
| 19 | +YELLOW='\033[1;33m' |
| 20 | +BLUE='\033[0;34m' |
| 21 | +NC='\033[0m' # No Color |
| 22 | + |
| 23 | +# Helper functions |
| 24 | +log_info() { |
| 25 | + echo -e "${BLUE}[INFO]${NC} $1" |
| 26 | +} |
| 27 | + |
| 28 | +log_success() { |
| 29 | + echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 30 | +} |
| 31 | + |
| 32 | +log_warning() { |
| 33 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 34 | +} |
| 35 | + |
| 36 | +log_error() { |
| 37 | + echo -e "${RED}[ERROR]${NC} $1" |
| 38 | +} |
| 39 | + |
| 40 | +# Function to check if command exists |
| 41 | +command_exists() { |
| 42 | + command -v "$1" >/dev/null 2>&1 |
| 43 | +} |
| 44 | + |
| 45 | +# Pre-flight checks |
| 46 | +preflight_checks() { |
| 47 | + log_info "Running pre-flight checks..." |
| 48 | + |
| 49 | + # Check if helm is installed |
| 50 | + if ! command_exists helm; then |
| 51 | + log_error "Helm is not installed. Please install Helm first." |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + |
| 55 | + # Check if kubectl is installed |
| 56 | + if ! command_exists kubectl; then |
| 57 | + log_error "kubectl is not installed. Please install kubectl first." |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + |
| 61 | + # Check if we can connect to the cluster |
| 62 | + if ! kubectl cluster-info >/dev/null 2>&1; then |
| 63 | + log_error "Cannot connect to Kubernetes cluster. Please check your kubeconfig." |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + # Check if chart directory exists |
| 68 | + if [ ! -d "$CHART_PATH" ]; then |
| 69 | + log_error "Chart directory '$CHART_PATH' does not exist." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + |
| 73 | + # Check if values file exists |
| 74 | + if [ ! -f "$VALUES_FILE" ]; then |
| 75 | + log_error "Values file '$VALUES_FILE' does not exist." |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + |
| 79 | + log_success "Pre-flight checks passed!" |
| 80 | +} |
| 81 | + |
| 82 | +# Function to update Helm dependencies |
| 83 | +update_dependencies() { |
| 84 | + log_info "Updating Helm dependencies..." |
| 85 | + cd "$CHART_PATH" |
| 86 | + |
| 87 | + if helm dependency update; then |
| 88 | + log_success "Dependencies updated successfully!" |
| 89 | + else |
| 90 | + log_error "Failed to update dependencies" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + cd - >/dev/null |
| 95 | +} |
| 96 | + |
| 97 | +# Function to deploy the chart |
| 98 | +deploy_chart() { |
| 99 | + if [ "$DRY_RUN" = true ]; then |
| 100 | + log_info "Running dry-run deployment for $CHART_NAME to namespace '$NAMESPACE'..." |
| 101 | + else |
| 102 | + log_info "Deploying $CHART_NAME to namespace '$NAMESPACE'..." |
| 103 | + fi |
| 104 | + |
| 105 | + # Prepare common Helm arguments |
| 106 | + HELM_ARGS=( |
| 107 | + --namespace "$NAMESPACE" |
| 108 | + --values "$VALUES_FILE" |
| 109 | + --timeout 10m |
| 110 | + --debug |
| 111 | + ) |
| 112 | + |
| 113 | + # Add dry-run flag if enabled |
| 114 | + if [ "$DRY_RUN" = true ]; then |
| 115 | + HELM_ARGS+=(--dry-run) |
| 116 | + else |
| 117 | + HELM_ARGS+=(--wait) |
| 118 | + fi |
| 119 | + |
| 120 | + # Check if release already exists (skip for dry-run) |
| 121 | + if [ "$DRY_RUN" = true ] || helm list -n "$NAMESPACE" | grep -q "$RELEASE_NAME"; then |
| 122 | + if [ "$DRY_RUN" = true ]; then |
| 123 | + log_info "Simulating upgrade of release '$RELEASE_NAME'..." |
| 124 | + else |
| 125 | + log_info "Release '$RELEASE_NAME' already exists. Upgrading..." |
| 126 | + fi |
| 127 | + |
| 128 | + if helm upgrade "$RELEASE_NAME" "$CHART_PATH" "${HELM_ARGS[@]}"; then |
| 129 | + if [ "$DRY_RUN" = true ]; then |
| 130 | + log_success "Dry-run upgrade completed successfully!" |
| 131 | + else |
| 132 | + log_success "Chart upgraded successfully!" |
| 133 | + fi |
| 134 | + else |
| 135 | + if [ "$DRY_RUN" = true ]; then |
| 136 | + log_error "Dry-run upgrade failed" |
| 137 | + else |
| 138 | + log_error "Failed to upgrade chart" |
| 139 | + fi |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + else |
| 143 | + log_info "Installing new release '$RELEASE_NAME'..." |
| 144 | + |
| 145 | + if helm install "$RELEASE_NAME" "$CHART_PATH" "${HELM_ARGS[@]}"; then |
| 146 | + log_success "Chart installed successfully!" |
| 147 | + else |
| 148 | + log_error "Failed to install chart" |
| 149 | + exit 1 |
| 150 | + fi |
| 151 | + fi |
| 152 | +} |
| 153 | + |
| 154 | +# Function to show deployment status |
| 155 | +show_status() { |
| 156 | + if [ "$DRY_RUN" = true ]; then |
| 157 | + log_info "Dry-run completed. No actual resources were created or modified." |
| 158 | + log_info "To perform the actual deployment, run the script without --dry-run flag." |
| 159 | + return |
| 160 | + fi |
| 161 | + |
| 162 | + log_info "Deployment Status:" |
| 163 | + echo |
| 164 | + |
| 165 | + # Show Helm release status |
| 166 | + helm status "$RELEASE_NAME" -n "$NAMESPACE" |
| 167 | + |
| 168 | + echo |
| 169 | + log_info "Pod Status:" |
| 170 | + kubectl get pods -n "$NAMESPACE" -l "app.kubernetes.io/instance=$RELEASE_NAME" |
| 171 | + |
| 172 | + echo |
| 173 | + log_info "Service Status:" |
| 174 | + kubectl get services -n "$NAMESPACE" -l "app.kubernetes.io/instance=$RELEASE_NAME" |
| 175 | +} |
| 176 | + |
| 177 | +# Main deployment function |
| 178 | +main() { |
| 179 | + log_info "Starting Ctrlplane deployment..." |
| 180 | + echo |
| 181 | + |
| 182 | + preflight_checks |
| 183 | + echo |
| 184 | + |
| 185 | + update_dependencies |
| 186 | + echo |
| 187 | + |
| 188 | + deploy_chart |
| 189 | + echo |
| 190 | + |
| 191 | + show_status |
| 192 | + echo |
| 193 | + |
| 194 | + log_success "Deployment completed successfully!" |
| 195 | + log_info "You can check the status anytime with: helm status $RELEASE_NAME -n $NAMESPACE" |
| 196 | +} |
| 197 | + |
| 198 | +# Parse command line arguments |
| 199 | +while [[ $# -gt 0 ]]; do |
| 200 | + case $1 in |
| 201 | + -n|--namespace) |
| 202 | + NAMESPACE="$2" |
| 203 | + shift 2 |
| 204 | + ;; |
| 205 | + -r|--release-name) |
| 206 | + RELEASE_NAME="$2" |
| 207 | + shift 2 |
| 208 | + ;; |
| 209 | + -v|--values-file) |
| 210 | + VALUES_FILE="$2" |
| 211 | + shift 2 |
| 212 | + ;; |
| 213 | + --dry-run) |
| 214 | + DRY_RUN=true |
| 215 | + shift |
| 216 | + ;; |
| 217 | + -h|--help) |
| 218 | + echo "Usage: $0 [OPTIONS]" |
| 219 | + echo |
| 220 | + echo "Options:" |
| 221 | + echo " -n, --namespace Kubernetes namespace (default: default)" |
| 222 | + echo " -r, --release-name Helm release name (default: ctrlplane)" |
| 223 | + echo " -v, --values-file Values file path (default: ./charts/ctrlplane/local-values.yaml)" |
| 224 | + echo " --dry-run Show what would be deployed without actually deploying" |
| 225 | + echo " -h, --help Show this help message" |
| 226 | + echo |
| 227 | + echo "Examples:" |
| 228 | + echo " $0 # Deploy with defaults" |
| 229 | + echo " $0 --dry-run # Test deployment without applying changes" |
| 230 | + echo " $0 -n ctrlplane # Deploy to 'ctrlplane' namespace" |
| 231 | + echo " $0 -r my-ctrlplane -n my-namespace # Custom release name and namespace" |
| 232 | + exit 0 |
| 233 | + ;; |
| 234 | + *) |
| 235 | + log_error "Unknown option: $1" |
| 236 | + echo "Use -h or --help for usage information" |
| 237 | + exit 1 |
| 238 | + ;; |
| 239 | + esac |
| 240 | +done |
| 241 | + |
| 242 | +# Run main function |
| 243 | +main |
0 commit comments