-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-infrastructure.sh
More file actions
executable file
·154 lines (123 loc) · 5.15 KB
/
01-infrastructure.sh
File metadata and controls
executable file
·154 lines (123 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# Exit on error
set -e
# Source common functions
source ./common.sh
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print section headers
print_header() {
echo -e "\n${BLUE}=== $1 ===${NC}\n"
}
# Function to print success messages
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
# Function to print error messages
print_error() {
echo -e "${RED}✗ $1${NC}"
}
# Function to print info messages
print_info() {
echo -e "${YELLOW}ℹ $1${NC}"
}
# Function to get vCPU count for a VM size
get_vcpu_count() {
local vm_size=$1
local location=$2
local vcpu_count=$(az vm list-sizes --location "$location" --query "[?name=='$vm_size'].numberOfCores | [0]" -o tsv)
echo "$vcpu_count"
}
# Function to verify node readiness
verify_node_readiness() {
print_info "Waiting for all nodes to be ready..."
wait_with_progress "nodes" "Ready" 300 || return 1
print_success "All nodes are ready"
return 0
}
# Function to verify zone labels
verify_zone_labels() {
print_info "Verifying zone labels..."
local zone1_nodes=$(kubectl get nodes -l node-type=postgres -o jsonpath='{.items[?(@.metadata.labels.topology\.kubernetes\.io/zone=="westus3-1")].metadata.name}' | wc -w)
local zone2_nodes=$(kubectl get nodes -l node-type=postgres -o jsonpath='{.items[?(@.metadata.labels.topology\.kubernetes\.io/zone=="westus3-2")].metadata.name}' | wc -w)
if [ "$zone1_nodes" -gt 0 ] && [ "$zone2_nodes" -gt 0 ]; then
print_success "Zone labels verified"
return 0
else
print_error "Missing zone labels"
return 1
fi
}
# Function to verify node distribution
verify_node_distribution() {
print_info "Verifying node distribution..."
local zone1_count=$(kubectl get nodes -l node-type=postgres -o jsonpath='{.items[?(@.metadata.labels.topology\.kubernetes\.io/zone=="westus3-1")].metadata.name}' | wc -w)
local zone2_count=$(kubectl get nodes -l node-type=postgres -o jsonpath='{.items[?(@.metadata.labels.topology\.kubernetes\.io/zone=="westus3-2")].metadata.name}' | wc -w)
echo "Current node distribution:"
echo "Zone 1 (pgzone1): $zone1_count nodes"
echo "Zone 2 (pgzone2): $zone2_count nodes"
}
# Function to get cluster credentials
get_cluster_credentials() {
print_info "Getting cluster credentials..."
local resource_group=$(terraform output -raw resource_group_name)
local cluster_name=$(terraform output -raw kubernetes_cluster_name)
az aks get-credentials --resource-group "$resource_group" --name "$cluster_name" --overwrite-existing
}
# Function to verify infrastructure requirements
verify_infrastructure_requirements() {
print_header "Verifying Infrastructure Requirements"
# Get location from terraform.tfvars
local location=$(grep "^[[:space:]]*location[[:space:]]*=" terraform.tfvars | cut -d"=" -f2 | cut -d"#" -f1 | tr -d " \"")
print_info "Location: $location"
# Get VM sizes and check vCPUs
local system_vm_size=$(grep "^[[:space:]]*vm_size[[:space:]]*=" terraform.tfvars | cut -d"=" -f2 | cut -d"#" -f1 | tr -d " \"")
local postgres_vm_size=$(grep "^[[:space:]]*postgres_vm_size[[:space:]]*=" terraform.tfvars | cut -d"=" -f2 | cut -d"#" -f1 | tr -d " \"")
print_info "Checking VM sizes..."
local system_vcpu_count=$(get_vcpu_count "$system_vm_size" "$location")
local postgres_vcpu_count=$(get_vcpu_count "$postgres_vm_size" "$location")
echo "System nodes: $system_vm_size ($system_vcpu_count vCPUs)"
echo "PostgreSQL nodes: $postgres_vm_size ($postgres_vcpu_count vCPUs)"
if [ "$system_vcpu_count" -ge 4 ] && [ "$postgres_vcpu_count" -ge 4 ]; then
print_success "VM sizes meet requirements (minimum 4 vCPUs)"
else
print_error "VM sizes do not meet requirements. Need minimum 4 vCPUs"
return 1
fi
# Check total eligible node count using Terraform outputs
local pgzone1_count=$(terraform output -raw postgres_zone1_node_count)
local pgzone2_count=$(terraform output -raw postgres_zone2_node_count)
local total_nodes=$((pgzone1_count + pgzone2_count))
echo "Total PostgreSQL nodes: $total_nodes"
if [ "$total_nodes" -ge 3 ]; then
print_success "Node count meets requirements (minimum 3 nodes)"
else
print_error "Node count does not meet requirements. Need minimum 3 nodes"
return 1
fi
}
# Main script execution
main() {
print_header "Starting Infrastructure Deployment and Verification"
# Step 1: Deploy infrastructure with Terraform
print_info "Deploying infrastructure with Terraform..."
terraform init
terraform apply -auto-approve
# Step 2: Get cluster credentials
get_cluster_credentials
# Step 3: Wait for nodes to be ready
verify_node_readiness || exit 1
# Step 4: Verify zone labels
verify_zone_labels || exit 1
# Step 5: Verify node distribution
verify_node_distribution
# Step 6: Verify infrastructure requirements
verify_infrastructure_requirements || exit 1
print_success "Infrastructure deployment and verification completed successfully"
}
# Run main function
main