@@ -557,9 +557,10 @@ def parse_audit_logs(file_paths, swagger_mapper=None): # pylint: disable=too-ma
557
557
swagger_mapper (SwaggerEndpointMapper): Mapper for converting to operation IDs
558
558
559
559
Returns:
560
- tuple: (Counter of endpoint counts, stats dict)
560
+ tuple: (Counter of endpoint counts, dict of operation samples, stats dict)
561
561
"""
562
562
endpoint_counts = Counter ()
563
+ operation_samples = {} # Store up to 5 audit entries per operation
563
564
total_entries = 0
564
565
skipped_entries = 0
565
566
swagger_matches = 0
@@ -609,12 +610,24 @@ def parse_audit_logs(file_paths, swagger_mapper=None): # pylint: disable=too-ma
609
610
if operation_id :
610
611
endpoint_counts [operation_id ] += 1
611
612
swagger_matches += 1
613
+
614
+ # Store up to 5 audit samples for this operation
615
+ if operation_id not in operation_samples :
616
+ operation_samples [operation_id ] = []
617
+ if len (operation_samples [operation_id ]) < 5 :
618
+ operation_samples [operation_id ].append (entry )
612
619
else :
613
620
# Try fallback parsing for edge cases
614
621
fallback_endpoint = convert_to_k8s_endpoint_fallback (effective_verb , request_uri )
615
622
if fallback_endpoint :
616
623
endpoint_counts [fallback_endpoint ] += 1
617
624
fallback_matches += 1
625
+
626
+ # Store up to 5 audit samples for this fallback operation
627
+ if fallback_endpoint not in operation_samples :
628
+ operation_samples [fallback_endpoint ] = []
629
+ if len (operation_samples [fallback_endpoint ]) < 5 :
630
+ operation_samples [fallback_endpoint ].append (entry )
618
631
else :
619
632
skipped_entries += 1
620
633
else :
@@ -653,20 +666,22 @@ def parse_audit_logs(file_paths, swagger_mapper=None): # pylint: disable=too-ma
653
666
print (f" Total API calls: { sum (endpoint_counts .values ())} " )
654
667
print (f" Skipped entries: { skipped_entries } " )
655
668
656
- return endpoint_counts , stats
669
+ return endpoint_counts , operation_samples , stats
657
670
658
671
659
- def write_results (endpoint_counts , stats , swagger_mapper = None , output_file = None , sort_by = 'count' , ineligible_endpoints = None ): # pylint: disable=too-many-statements
672
+ def write_results (endpoint_counts , operation_samples , stats , swagger_mapper = None , output_file = None , sort_by = 'count' , ineligible_endpoints = None , audit_operations_json = 'audit-operations.json' ): # pylint: disable=too-many-statements
660
673
"""
661
674
Write results to file or stdout.
662
675
663
676
Args:
664
677
endpoint_counts (Counter): Endpoint counts
678
+ operation_samples (dict): Sample audit entries for each operation
665
679
stats (dict): Parsing statistics
666
680
swagger_mapper (SwaggerEndpointMapper): Mapper for finding missing endpoints
667
681
output_file (str, optional): Output file path
668
682
sort_by (str): Sort method - 'count' (descending) or 'name' (alphabetical)
669
683
ineligible_endpoints (set, optional): Set of ineligible endpoints to filter out
684
+ audit_operations_json (str): Output path for audit operations JSON file
670
685
"""
671
686
if ineligible_endpoints is None :
672
687
ineligible_endpoints = set ()
@@ -780,6 +795,43 @@ def write_results(endpoint_counts, stats, swagger_mapper=None, output_file=None,
780
795
print ("\n Results:" )
781
796
print (result_text )
782
797
798
+ # Generate audit-operations.json JSON file with sample audit entries
799
+ _write_audit_operations_json (filtered_endpoint_counts , operation_samples , ineligible_endpoints , deprecated_operations , audit_operations_json )
800
+
801
+
802
+ def _write_audit_operations_json (filtered_endpoint_counts , operation_samples , ineligible_endpoints , deprecated_operations , json_output_path = 'audit-operations.json' ):
803
+ """
804
+ Write audit-operations.json JSON file with sample audit entries for each operation.
805
+
806
+ Args:
807
+ filtered_endpoint_counts (Counter): Filtered endpoint counts
808
+ operation_samples (dict): Sample audit entries for each operation
809
+ ineligible_endpoints (set): Set of ineligible endpoints
810
+ deprecated_operations (set): Set of deprecated operations
811
+ json_output_path (str): Output path for JSON file
812
+ """
813
+ # Build final JSON with samples for operations that passed filtering
814
+ audit_operations_json = {}
815
+
816
+ for operation_id in filtered_endpoint_counts :
817
+ # Skip operations that are ineligible or deprecated
818
+ if operation_id in ineligible_endpoints or operation_id in deprecated_operations :
819
+ continue
820
+
821
+ # Get sample audit entries for this operation (up to 5)
822
+ samples = operation_samples .get (operation_id , [])
823
+ audit_operations_json [operation_id ] = samples
824
+
825
+ # Write to JSON file
826
+ try :
827
+ with open (json_output_path , 'w' , encoding = 'utf-8' ) as f :
828
+ json .dump (audit_operations_json , f , indent = 2 , ensure_ascii = False )
829
+
830
+ total_samples = sum (len (samples ) for samples in audit_operations_json .values ())
831
+ print (f"Generated { json_output_path } with { len (audit_operations_json )} operations and { total_samples } sample audit entries" )
832
+ except IOError as e :
833
+ print (f"Error writing { json_output_path } : { e } " )
834
+
783
835
784
836
def main ():
785
837
"""Main function to parse command line arguments and run the parser."""
@@ -804,6 +856,9 @@ def main():
804
856
parser .add_argument ('--ineligible-endpoints-url' ,
805
857
help = 'URL or local path to ineligible endpoints YAML file '
806
858
'(default: https://raw.githubusercontent.com/kubernetes/kubernetes/master/test/conformance/testdata/ineligible_endpoints.yaml)' )
859
+ parser .add_argument ('--audit-operations-json' ,
860
+ default = 'audit-operations.json' ,
861
+ help = 'Output path for audit operations JSON file (default: %(default)s)' )
807
862
808
863
args = parser .parse_args ()
809
864
@@ -819,14 +874,14 @@ def main():
819
874
sys .exit (1 )
820
875
821
876
# Parse the audit log(s)
822
- endpoint_counts , stats = parse_audit_logs (args .audit_logs , swagger_mapper )
877
+ endpoint_counts , operation_samples , stats = parse_audit_logs (args .audit_logs , swagger_mapper )
823
878
824
879
if not endpoint_counts :
825
880
print ("No endpoints found or error parsing file" )
826
881
sys .exit (1 )
827
882
828
883
# Write results
829
- write_results (endpoint_counts , stats , swagger_mapper , args .output , args .sort , ineligible_endpoints )
884
+ write_results (endpoint_counts , operation_samples , stats , swagger_mapper , args .output , args .sort , ineligible_endpoints , args . audit_operations_json )
830
885
831
886
832
887
if __name__ == '__main__' :
0 commit comments