-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcapacity-checker-config.yaml
74 lines (65 loc) · 2.53 KB
/
capacity-checker-config.yaml
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
apiVersion: v1
kind: ConfigMap
metadata:
name: capacity-checker-config
data:
script.py: |
import boto3
import time
from datetime import datetime, timedelta, timezone
# Set up CloudWatch Logs client
cloudwatch_logs = boto3.client('logs', region_name='us-west-2')
# CloudWatch Log Group where Karpenter logs are stored
log_group_name = "/aws/containerinsights/kub316/application"
print("Checking insufficient capacity events", flush=True)
def get_nodeclaim_id_for_insufficient_capacity():
try:
start_time = datetime.now(timezone.utc) - timedelta(minutes=5)
end_time = datetime.now(timezone.utc)
query = r"""
fields @timestamp, @message
| filter @message like /insufficient capacity/
| parse @message /"NodeClaim":\{"name":"(?<NodeClaimID>[^"]+)"/
| sort @timestamp desc
| limit 1
"""
response = cloudwatch_logs.start_query(
logGroupName=log_group_name,
startTime=int(start_time.timestamp()),
endTime=int(end_time.timestamp()),
queryString=query
)
query_id = response['queryId']
result = None
while result is None or result['status'] in ['Running','Scheduled']:
print("Waiting for query results...",flush=True)
time.sleep(5)
result = cloudwatch_logs.get_query_results(queryId=query_id)
#print(f"result:{result}",flush=True)
node_claim_id = None
if result['results']:
for entry in result['results']:
#print("Entry:", entry, flush=True)
for field in entry:
#print("Field:", field, flush=True)
if field['field'] == 'NodeClaimID':
node_claim_id = field['value']
break
if node_claim_id:
break
else:
print("No results found for the query.", flush=True)
return node_claim_id
except Exception as e:
print("An error occurred:", str(e), flush=True)
return None
print("Running capacity check...")
node_claim_id = get_nodeclaim_id_for_insufficient_capacity()
current_time = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S %Z')
if node_claim_id:
with open(node_claim_id, "w") as file:
file.write(node_claim_id)
file.flush()
print(f"{current_time}: Insufficient capacity detected for NodeClaimID: {node_claim_id}", flush=True)
else:
print("No recent insufficient capacity errors found.", flush=True)