Skip to content

Commit

Permalink
Request flavor details when needed
Browse files Browse the repository at this point in the history
This change handles the case where the server flavor doesn't have the details.
  • Loading branch information
TristanCacqueray committed Dec 5, 2024
1 parent a41da3a commit 9762660
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "zuul-capacity"
version = "0.3.1"
version = "0.4.0"
description = "Zuul capacity exporter"
readme = "README.md"
requires-python = ">=3.12"
Expand Down
30 changes: 22 additions & 8 deletions zuul-capacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ class Resource:

def from_server(server):
flavor = server["flavor"]
if flavor["ram"] == 0:
return None
return Resource(flavor["ram"], flavor["vcpus"])

def get_resources(cloud):
def from_flavor(flavors, cloud, server):
flavor_id = server["flavor"].id
if flavor_id not in flavors:
log.info("Requesting flavor %s", flavor_id)
flavors[flavor_id] = cloud.get_flavor(flavor_id)
flavor = flavors[flavor_id]
return Resource(flavor["ram"], flavor["vcpus"])

def get_resources(flavors, cloud):
"Get the cloud resources."
resources = []
for server in cloud.compute.servers():
try:
resources.append(Resource.from_server(server))
if resource := Resource.from_server(server):
resources.append(resource)
else:
resources.append(Resource.from_flavor(flavors, cloud, server))
except Exception as e:
log.exception("Couldn't get server resource %s: %s", server, e)
return resources
Expand All @@ -49,8 +62,8 @@ def get_providers(nodepool_yaml):
providers[provider.get("name", "unknown")] = Provider.from_nodepool(provider)
return providers

def update_provider_metric(metrics, name, provider):
resources = get_resources(provider.cloud)
def update_provider_metric(metrics, flavors, name, provider):
resources = get_resources(flavors, provider.cloud)
metrics["instances"].labels(cloud=name).set(len(resources))
cpu, mem = 0, 0
for resource in resources:
Expand All @@ -59,10 +72,10 @@ def update_provider_metric(metrics, name, provider):
metrics["cpu"].labels(cloud=name).set(cpu)
metrics["mem"].labels(cloud=name).set(mem)

def update_providers_metric(metrics, providers):
def update_providers_metric(metrics, flavors, providers):
for (name, provider) in providers.items():
try:
update_provider_metric(metrics, name, provider)
update_provider_metric(metrics, flavors, name, provider)
except Exception as e:
log.exception("Couldn't get provider %s: %s", name, e)
metrics["error"].labels(cloud=name).inc()
Expand All @@ -86,16 +99,17 @@ def main():
)

providers = get_providers(args.nodepool)
flavors = dict()

update_providers_metric(metrics, providers)
update_providers_metric(metrics, flavors, providers)

# Initialize connection
log.info("Starting exporter at :%d for %d provider", args.port, len(providers))
start_http_server(args.port)

while True:
time.sleep(300)
update_providers_metric(metrics, providers)
update_providers_metric(metrics, flavors, providers)



Expand Down

0 comments on commit 9762660

Please sign in to comment.