Created
June 10, 2023 00:36
-
-
Save derrickburns/807ef5acfe7d05a1870188686817da7f to your computer and use it in GitHub Desktop.
Multi-AZ anti-affinity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import yaml | |
import csv | |
from collections import defaultdict | |
def supports_anti_affinity_across_zones(deployment): | |
affinity = deployment.get('spec', {}).get('template', {}).get('spec', {}).get('affinity', {}) | |
pod_anti_affinity = affinity.get('podAntiAffinity', {}) | |
required_during_scheduling_ignored_during_execution = pod_anti_affinity.get('requiredDuringSchedulingIgnoredDuringExecution', []) | |
for item in required_during_scheduling_ignored_during_execution: | |
topology_key = item.get('topologyKey', '') | |
if topology_key == "topology.kubernetes.io/zone": | |
return True | |
return False | |
# Read the YAML input from stdin | |
input_yaml = sys.stdin.read() | |
# Parse the input as YAML | |
deployments_data = yaml.safe_load_all(input_yaml) | |
# Initialize the CSV writer | |
csv_writer = csv.writer(sys.stdout) | |
# Write the header line | |
header = ['Deployment', 'Namespace', 'Supports Anti-Affinity Across Zones'] | |
csv_writer.writerow(header) | |
# Initialize counters | |
total_deployments = 0 | |
deployments_per_namespace = defaultdict(int) | |
deployments_with_anti_affinity = 0 | |
# Process each Deployment | |
for deployments in deployments_data: | |
for deployment_data in deployments['items']: | |
# Extract the name and namespace of the Deployment | |
metadata = deployment_data.get('metadata', {}) | |
name = metadata.get('name', '') | |
namespace = metadata.get('namespace', '') | |
# Check if the Deployment supports anti-affinity across zones | |
supports_anti_affinity_var = supports_anti_affinity_across_zones(deployment_data) | |
# Write the Deployment details to CSV | |
csv_row = [name, namespace, supports_anti_affinity_var] | |
csv_writer.writerow(csv_row) | |
# Update counters | |
total_deployments += 1 | |
deployments_per_namespace[namespace] += 1 | |
if supports_anti_affinity_var: | |
deployments_with_anti_affinity += 1 | |
# Print statistics | |
print("Total deployments:", total_deployments) | |
print("Deployments per namespace:", dict(deployments_per_namespace)) | |
print("Percentage of deployments supporting anti-affinity across zones:", (deployments_with_anti_affinity / total_deployments) * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment