-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-worker.sh
More file actions
executable file
·208 lines (171 loc) · 6.98 KB
/
setup-worker.sh
File metadata and controls
executable file
·208 lines (171 loc) · 6.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# ConfigHub Worker Setup for DevOps Examples
# This script sets up a ConfigHub worker to bridge between ConfigHub and Kubernetes
set -e
echo "🚀 ConfigHub Worker Setup for DevOps Examples"
echo "============================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check prerequisites
check_prerequisites() {
echo -e "\n${YELLOW}Checking prerequisites...${NC}"
# Check if cub is installed
if ! command -v cub &> /dev/null; then
echo -e "${RED}❌ 'cub' CLI not found. Please install ConfigHub CLI first.${NC}"
echo "Visit: https://confighub.com/docs/getting-started"
exit 1
fi
# Check if authenticated
if ! cub auth get-token &> /dev/null; then
echo -e "${RED}❌ Not authenticated to ConfigHub. Running 'cub auth login'...${NC}"
cub auth login
fi
# Check if kubectl is configured
if ! kubectl cluster-info &> /dev/null; then
echo -e "${RED}❌ kubectl not configured or cluster not accessible.${NC}"
echo "Please ensure you have a Kubernetes cluster running (e.g., kind, minikube)"
exit 1
fi
echo -e "${GREEN}✅ All prerequisites met${NC}"
}
# Create worker
create_worker() {
WORKER_NAME="${1:-devops-worker-$(date +%s)}"
SPACE="${2:-default}"
echo -e "\n${YELLOW}Creating ConfigHub worker...${NC}"
echo "Worker name: $WORKER_NAME"
echo "Space: $SPACE"
# Check if worker already exists
if cub worker list --space "$SPACE" | grep -q "$WORKER_NAME"; then
echo -e "${YELLOW}⚠️ Worker '$WORKER_NAME' already exists${NC}"
else
# Create the worker
cub worker create "$WORKER_NAME" --space "$SPACE" || {
echo -e "${RED}❌ Failed to create worker${NC}"
exit 1
}
echo -e "${GREEN}✅ Worker created successfully${NC}"
fi
}
# Create target for Kind cluster
create_target() {
CONTEXT="${1:-kind-kind}"
NAMESPACE="${2:-default}"
TARGET_NAME="${3:-kind-target-$NAMESPACE}"
SPACE="${4:-default}"
WORKER_NAME="${5:-devops-worker}"
echo -e "\n${YELLOW}Creating Kubernetes target...${NC}"
echo "Target: $TARGET_NAME"
echo "Context: $CONTEXT"
echo "Namespace: $NAMESPACE"
# Check if target already exists
if cub target list --space "$SPACE" | grep -q "$TARGET_NAME"; then
echo -e "${YELLOW}⚠️ Target '$TARGET_NAME' already exists${NC}"
else
# Create the target with the worker
PARAMS='{"KubeContext":"'$CONTEXT'","KubeNamespace":"'$NAMESPACE'","WaitTimeout":"2m0s"}'
cub target create "$TARGET_NAME" "$PARAMS" "$WORKER_NAME" \
--space "$SPACE" \
--provider Kubernetes \
--toolchain "Kubernetes/YAML" || {
echo -e "${RED}❌ Failed to create target${NC}"
echo "Note: Worker needs to be running for target creation to succeed"
}
echo -e "${GREEN}✅ Target configured${NC}"
fi
}
# Update units with target
assign_target_to_units() {
SPACE="${1:-drift-test-demo}"
TARGET_NAME="${2:-kind-target-drift-test}"
echo -e "\n${YELLOW}Assigning target to units in space '$SPACE'...${NC}"
# Get target ID first
TARGET_ID=$(cub target get "$TARGET_NAME" --space "$SPACE" --json 2>/dev/null | jq -r '.TargetID' || echo "")
if [ -z "$TARGET_ID" ]; then
echo -e "${YELLOW}⚠️ Target not found in space. Skipping unit assignment.${NC}"
return
fi
# List units and update their target
cub unit list --space "$SPACE" --quiet | while read -r unit_name _; do
if [ -n "$unit_name" ]; then
echo " Updating unit: $unit_name"
cub unit update "$unit_name" --space "$SPACE" \
--patch --data '{"TargetID":"'$TARGET_ID'"}' &>/dev/null || {
echo -e "${YELLOW} ⚠️ Could not update $unit_name${NC}"
}
fi
done
echo -e "${GREEN}✅ Units updated with target${NC}"
}
# Clean up old workers and targets
cleanup_old_resources() {
echo -e "\n${YELLOW}🧹 Cleaning up old workers and targets...${NC}"
# Clean up old workers matching common patterns
PATTERNS=("devops-worker" "drift-worker" "cost-worker")
for pattern in "${PATTERNS[@]}"; do
workers=$(cub worker list --quiet 2>/dev/null | grep "$pattern" | awk '{print $1}' || true)
if [ -n "$workers" ]; then
for worker in $workers; do
echo " Deleting worker: $worker"
cub worker delete "$worker" 2>/dev/null || true
done
fi
done
# Clean up old targets in common spaces
SPACES=("default" "drift-test-demo" "cost-optimizer-demo")
for space in "${SPACES[@]}"; do
if cub space get "$space" &>/dev/null; then
targets=$(cub target list --space "$space" --quiet 2>/dev/null | awk '{print $1}' || true)
if [ -n "$targets" ]; then
for target in $targets; do
echo " Deleting target: $target in space $space"
cub target delete "$target" --space "$space" 2>/dev/null || true
done
fi
fi
done
echo -e "${GREEN}✅ Cleanup complete${NC}"
}
# Main setup
main() {
echo -e "\n${GREEN}Starting ConfigHub Worker Setup${NC}"
# Parse arguments
WORKER_NAME="${1:-devops-worker}"
KUBE_CONTEXT="${2:-$(kubectl config current-context)}"
# Check prerequisites
check_prerequisites
# CRITICAL: Clean up old resources first (cleanup-first principle)
cleanup_old_resources
# Get current Kubernetes context
echo -e "\n${YELLOW}Kubernetes Configuration:${NC}"
echo "Current context: $KUBE_CONTEXT"
kubectl get nodes
# Create worker
create_worker "$WORKER_NAME" "default"
# Create targets for common namespaces
echo -e "\n${YELLOW}Setting up targets for DevOps examples...${NC}"
# For drift-detector
create_target "$KUBE_CONTEXT" "drift-test" "kind-drift-test" "drift-test-demo" "$WORKER_NAME"
assign_target_to_units "drift-test-demo" "kind-drift-test"
# For cost-optimizer (uses cluster-wide view)
create_target "$KUBE_CONTEXT" "default" "kind-default" "default" "$WORKER_NAME"
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}✅ ConfigHub Worker Setup Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "\n${YELLOW}Next steps:${NC}"
echo "1. Run the worker (in a separate terminal):"
echo -e " ${GREEN}cub worker run $WORKER_NAME${NC}"
echo ""
echo "2. Apply units to Kubernetes:"
echo -e " ${GREEN}cub unit apply --all --space drift-test-demo${NC}"
echo ""
echo "3. Run the DevOps examples:"
echo -e " ${GREEN}cd drift-detector && ./drift-detector${NC}"
echo -e " ${GREEN}cd cost-optimizer && ./cost-optimizer${NC}"
echo -e "\n${YELLOW}Note:${NC} The worker needs to stay running for ConfigHub ↔ Kubernetes sync"
}
# Run main function
main "$@"