-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelm-deploy.sh
More file actions
executable file
·323 lines (279 loc) · 9.07 KB
/
helm-deploy.sh
File metadata and controls
executable file
·323 lines (279 loc) · 9.07 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
CHART_NAME="slaking"
RELEASE_NAME="slaking"
NAMESPACE="slaking"
IMAGE_NAME="slaking"
IMAGE_TAG="latest"
HELM_CHART_PATH="./charts/slaking"
echo -e "${BLUE}🚀 Slaking Helm Deployment Script${NC}"
echo "=========================================="
# Check if required tools are available
check_requirements() {
echo -e "${BLUE}🔍 Checking requirements...${NC}"
if ! command -v helm &> /dev/null; then
echo -e "${RED}❌ Helm is not installed or not in PATH${NC}"
exit 1
fi
if ! command -v kubectl &> /dev/null; then
echo -e "${RED}❌ kubectl is not installed or not in PATH${NC}"
exit 1
fi
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker is not installed or not in PATH${NC}"
exit 1
fi
echo -e "${GREEN}✅ All requirements met${NC}"
}
# Build Docker image
build_image() {
echo -e "${BLUE}🔨 Building Docker image...${NC}"
if [ ! -f "Dockerfile" ]; then
echo -e "${RED}❌ Dockerfile not found in current directory${NC}"
exit 1
fi
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Docker image built successfully${NC}"
else
echo -e "${RED}❌ Failed to build Docker image${NC}"
exit 1
fi
}
# Create namespace
create_namespace() {
echo -e "${BLUE}📦 Creating namespace...${NC}"
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
echo -e "${GREEN}✅ Namespace created/updated${NC}"
}
# Create values file from environment
create_values_file() {
echo -e "${BLUE}⚙️ Creating values file...${NC}"
if [ ! -f ".env" ]; then
echo -e "${YELLOW}⚠️ .env file not found. Creating from template...${NC}"
cp env.example .env
echo -e "${YELLOW}⚠️ Please edit .env file with your Slack token and other settings${NC}"
exit 1
fi
# Load environment variables
source .env
# Check if SLACK_TOKEN is set
if [ -z "$SLACK_TOKEN" ] || [ "$SLACK_TOKEN" = "xoxb-your-slack-bot-token-here" ]; then
echo -e "${RED}❌ Please set your Slack token in the .env file${NC}"
exit 1
fi
# Create values file
cat > charts/slaking/values-production.yaml << EOF
# Production values for Slaking
# Generated from .env file
image:
repository: ${IMAGE_NAME}
tag: "${IMAGE_TAG}"
pullPolicy: IfNotPresent
env:
SLACK_TOKEN: "${SLACK_TOKEN}"
SLACK_DEFAULT_CHANNEL: "${SLACK_DEFAULT_CHANNEL:-#general}"
SLACK_RATE_LIMIT: "${SLACK_RATE_LIMIT:-1000}"
K8S_NAMESPACES: "${K8S_NAMESPACES:-}"
K8S_WATCH_ALL_NAMESPACES: "${K8S_WATCH_ALL_NAMESPACES:-true}"
K8S_WATCH_INTERVAL: "${K8S_WATCH_INTERVAL:-30000}"
LOG_LEVEL: "${LOG_LEVEL:-info}"
METRICS_PORT: "${METRICS_PORT:-9090}"
PORT: "3000"
NODE_ENV: "production"
CONFIG_PATH: "/app/config/config.json"
secret:
create: true
data:
slack-token: "${SLACK_TOKEN}"
config:
slack:
defaultChannel: "${SLACK_DEFAULT_CHANNEL:-#general}"
rateLimit: ${SLACK_RATE_LIMIT:-1000}
kubernetes:
namespaces: ${K8S_NAMESPACES:-[]}
watchAllNamespaces: ${K8S_WATCH_ALL_NAMESPACES:-true}
watchInterval: ${K8S_WATCH_INTERVAL:-30000}
logging:
level: "${LOG_LEVEL:-info}"
# Enable monitoring features
serviceMonitor:
enabled: true
interval: 30s
scrapeTimeout: 10s
# Enable horizontal pod autoscaler
hpa:
enabled: true
minReplicas: 1
maxReplicas: 3
targetCPUUtilizationPercentage: 80
targetMemoryUtilizationPercentage: 80
# Resource limits
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
# Security settings
podSecurityContext:
fsGroup: 1000
runAsNonRoot: true
runAsUser: 1000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
containerSecurityContext:
runAsNonRoot: true
runAsUser: 1000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
EOF
echo -e "${GREEN}✅ Values file created: charts/slaking/values-production.yaml${NC}"
}
# Deploy with Helm
deploy_helm() {
echo -e "${BLUE}📋 Deploying with Helm...${NC}"
# Check if release exists
if helm list -n ${NAMESPACE} | grep -q ${RELEASE_NAME}; then
echo -e "${YELLOW}⚠️ Release ${RELEASE_NAME} already exists. Upgrading...${NC}"
helm upgrade ${RELEASE_NAME} ${HELM_CHART_PATH} \
--namespace ${NAMESPACE} \
--values charts/slaking/values-production.yaml \
--wait \
--timeout 10m
else
echo -e "${BLUE}📦 Installing new release...${NC}"
helm install ${RELEASE_NAME} ${HELM_CHART_PATH} \
--namespace ${NAMESPACE} \
--values charts/slaking/values-production.yaml \
--wait \
--timeout 10m
fi
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Helm deployment completed successfully${NC}"
else
echo -e "${RED}❌ Helm deployment failed${NC}"
exit 1
fi
}
# Verify deployment
verify_deployment() {
echo -e "${BLUE}🔍 Verifying deployment...${NC}"
# Check pods
echo -e "${BLUE}📦 Checking pods...${NC}"
kubectl get pods -n ${NAMESPACE} -l "app.kubernetes.io/name=${CHART_NAME}"
# Check services
echo -e "${BLUE}🔌 Checking services...${NC}"
kubectl get svc -n ${NAMESPACE}
# Check secrets
echo -e "${BLUE}🔐 Checking secrets...${NC}"
kubectl get secrets -n ${NAMESPACE} | grep ${CHART_NAME}
# Check configmaps
echo -e "${BLUE}📋 Checking configmaps...${NC}"
kubectl get configmaps -n ${NAMESPACE} | grep ${CHART_NAME}
# Check RBAC
echo -e "${BLUE}🛡️ Checking RBAC...${NC}"
kubectl get clusterrole,clusterrolebinding | grep ${CHART_NAME}
echo -e "${GREEN}✅ Deployment verification completed${NC}"
}
# Show useful commands
show_commands() {
echo -e "${BLUE}📋 Useful commands:${NC}"
echo ""
echo -e "${GREEN}Check status:${NC}"
echo " helm status ${RELEASE_NAME} -n ${NAMESPACE}"
echo " kubectl get pods -n ${NAMESPACE}"
echo ""
echo -e "${GREEN}View logs:${NC}"
echo " kubectl logs -n ${NAMESPACE} -l app.kubernetes.io/name=${CHART_NAME}"
echo ""
echo -e "${GREEN}Test health:${NC}"
echo " kubectl port-forward -n ${NAMESPACE} svc/${RELEASE_NAME} 3000:3000"
echo " curl http://localhost:3000/health"
echo ""
echo -e "${GREEN}View metrics:${NC}"
echo " kubectl port-forward -n ${NAMESPACE} svc/${RELEASE_NAME}-metrics 9090:9090"
echo " curl http://localhost:9090/metrics"
echo ""
echo -e "${GREEN}Update configuration:${NC}"
echo " helm upgrade ${RELEASE_NAME} ${HELM_CHART_PATH} --namespace ${NAMESPACE} --values charts/slaking/values-production.yaml"
echo ""
echo -e "${GREEN}Uninstall:${NC}"
echo " helm uninstall ${RELEASE_NAME} -n ${NAMESPACE}"
echo ""
echo -e "${YELLOW}⚠️ Remember to configure your workloads with slaking annotations!${NC}"
}
# Main execution
main() {
check_requirements
build_image
create_namespace
create_values_file
deploy_helm
verify_deployment
show_commands
echo -e "${GREEN}🎉 Slaking Helm deployment completed successfully!${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo "1. Configure your workloads with slaking annotations"
echo "2. Test the service endpoints"
echo "3. Monitor the logs and metrics"
echo "4. Set up Prometheus/Grafana dashboards if needed"
}
# Handle command line arguments
case "${1:-deploy}" in
"deploy")
main
;;
"upgrade")
echo -e "${BLUE}🔄 Upgrading existing deployment...${NC}"
create_values_file
deploy_helm
verify_deployment
;;
"uninstall")
echo -e "${YELLOW}🗑️ Uninstalling Slaking...${NC}"
helm uninstall ${RELEASE_NAME} -n ${NAMESPACE}
echo -e "${GREEN}✅ Uninstallation completed${NC}"
;;
"status")
echo -e "${BLUE}📊 Checking deployment status...${NC}"
helm status ${RELEASE_NAME} -n ${NAMESPACE}
kubectl get pods -n ${NAMESPACE}
;;
"logs")
echo -e "${BLUE}📋 Showing logs...${NC}"
kubectl logs -n ${NAMESPACE} -l app.kubernetes.io/name=${CHART_NAME} -f
;;
"test")
echo -e "${BLUE}🧪 Testing deployment...${NC}"
kubectl port-forward -n ${NAMESPACE} svc/${RELEASE_NAME} 3000:3000 &
sleep 5
curl -f http://localhost:3000/health
kill %1
;;
*)
echo "Usage: $0 {deploy|upgrade|uninstall|status|logs|test}"
echo " deploy - Deploy Slaking (default)"
echo " upgrade - Upgrade existing deployment"
echo " uninstall- Remove Slaking"
echo " status - Check deployment status"
echo " logs - Show application logs"
echo " test - Test deployment health"
exit 1
;;
esac