Skip to content

Commit a03ada1

Browse files
authored
Merge pull request #35381 from dims/account-for-deprecated-methods-in-swagger-json
Account for deprecated methods in swagger.json
2 parents 726e8d2 + 57cc82b commit a03ada1

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

experiment/audit/audit_log_parser.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(self, swagger_url=None):
8383
self.swagger_url = swagger_url or "https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/api/openapi-spec/swagger.json"
8484
self.swagger_spec = None
8585
self.path_to_operation = {}
86+
self.deprecated_operations = set()
8687
self.known_resource_types = set()
8788
self.load_swagger_spec()
8889

@@ -140,12 +141,19 @@ def _build_path_mapping(self):
140141
if method.lower() in ['get', 'post', 'put', 'patch', 'delete'] and 'operationId' in operation:
141142
operation_id = operation['operationId']
142143

144+
# Check if operation is deprecated
145+
description = operation.get('description', '').lower()
146+
if 'deprecated' in description:
147+
self.deprecated_operations.add(operation_id)
148+
143149
# Normalize the path for matching
144150
normalized_path = self._normalize_swagger_path(path)
145151
key = f"{method.lower()}:{normalized_path}"
146152
self.path_to_operation[key] = operation_id
147153

148154
print(f"Loaded {len(self.path_to_operation)} API operations from Swagger spec")
155+
if self.deprecated_operations:
156+
print(f"Found {len(self.deprecated_operations)} deprecated operations")
149157

150158
def _extract_resource_types(self):
151159
"""Extract resource types from Swagger paths to avoid hardcoding."""
@@ -663,21 +671,28 @@ def write_results(endpoint_counts, stats, swagger_mapper=None, output_file=None,
663671
if ineligible_endpoints is None:
664672
ineligible_endpoints = set()
665673

666-
# Filter out ineligible endpoints from results
674+
# Filter out ineligible endpoints and deprecated operations from results
667675
filtered_endpoint_counts = Counter()
668676
ineligible_found_count = 0
677+
deprecated_found_count = 0
678+
deprecated_operations = swagger_mapper.deprecated_operations if swagger_mapper else set()
679+
669680
for endpoint, count in endpoint_counts.items():
670-
if endpoint not in ineligible_endpoints:
671-
filtered_endpoint_counts[endpoint] = count
672-
else:
681+
if endpoint in ineligible_endpoints:
673682
ineligible_found_count += count
683+
elif endpoint in deprecated_operations:
684+
deprecated_found_count += count
685+
else:
686+
filtered_endpoint_counts[endpoint] = count
674687

675688
# Update stats to reflect filtering
676689
filtered_stats = stats.copy()
677690
filtered_stats['unique_endpoints'] = len(filtered_endpoint_counts)
678691
filtered_stats['total_api_calls'] = sum(filtered_endpoint_counts.values())
679-
filtered_stats['ineligible_endpoints_filtered'] = len(endpoint_counts) - len(filtered_endpoint_counts)
692+
filtered_stats['ineligible_endpoints_filtered'] = len([ep for ep in endpoint_counts if ep in ineligible_endpoints])
680693
filtered_stats['ineligible_api_calls_filtered'] = ineligible_found_count
694+
filtered_stats['deprecated_endpoints_filtered'] = len([ep for ep in endpoint_counts if ep in deprecated_operations])
695+
filtered_stats['deprecated_api_calls_filtered'] = deprecated_found_count
681696
if sort_by == 'count':
682697
sorted_endpoints = filtered_endpoint_counts.most_common()
683698
sort_desc = "sorted by count (descending)"
@@ -699,6 +714,9 @@ def write_results(endpoint_counts, stats, swagger_mapper=None, output_file=None,
699714
if ineligible_endpoints:
700715
output.append(f"Ineligible endpoints filtered: {filtered_stats['ineligible_endpoints_filtered']}")
701716
output.append(f"Ineligible API calls filtered: {filtered_stats['ineligible_api_calls_filtered']}")
717+
if deprecated_operations:
718+
output.append(f"Deprecated endpoints filtered: {filtered_stats['deprecated_endpoints_filtered']}")
719+
output.append(f"Deprecated API calls filtered: {filtered_stats['deprecated_api_calls_filtered']}")
702720
output.append(f"Results {sort_desc}")
703721
output.append("")
704722
output.append("Endpoint Name (OpenAPI Operation ID) | Count")
@@ -726,6 +744,10 @@ def write_results(endpoint_counts, stats, swagger_mapper=None, output_file=None,
726744
if ineligible_endpoints:
727745
stable_missing_operations = stable_missing_operations - ineligible_endpoints
728746

747+
# Filter out deprecated operations from missing operations
748+
if deprecated_operations:
749+
stable_missing_operations = stable_missing_operations - deprecated_operations
750+
729751
if stable_missing_operations:
730752
filtered_count = len(missing_operations) - len(stable_missing_operations)
731753

@@ -735,8 +757,8 @@ def write_results(endpoint_counts, stats, swagger_mapper=None, output_file=None,
735757
output.append("=" * 70)
736758
output.append(f"Total missing stable endpoints: {len(stable_missing_operations)}")
737759
if filtered_count > 0:
738-
output.append(f"(Filtered out {filtered_count} alpha/beta endpoints)")
739-
output.append(f"These are stable API endpoints defined in the Swagger spec but not exercised in this audit log:")
760+
output.append(f"(Filtered out {filtered_count} alpha/beta/deprecated endpoints)")
761+
output.append(f"These are stable, non-deprecated API endpoints defined in the Swagger spec but not exercised in this audit log:")
740762
output.append("")
741763

742764
# Sort missing operations alphabetically

0 commit comments

Comments
 (0)