Skip to content

Instantly share code, notes, and snippets.

@mdraevich
Created January 26, 2024 01:08
Show Gist options
  • Select an option

  • Save mdraevich/c14308d43d6c6067da41ac4fe0817117 to your computer and use it in GitHub Desktop.

Select an option

Save mdraevich/c14308d43d6c6067da41ac4fe0817117 to your computer and use it in GitHub Desktop.

Revisions

  1. mdraevich created this gist Jan 26, 2024.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    This script generates HTML-page from ArgoCD Diff output.
    You could find it useful for CI/CD by generating HTML-page and putting it to artifacts for future analysis.

    To convert the output execute:
    ```
    argocd app diff YOUR-APPLICATION --revision DESIRED-STATE --refresh | python3 format.py
    ```
    116 changes: 116 additions & 0 deletions format.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    import sys
    import json


    def parse_argocd_diff_output(diff_string):
    lines = diff_string.strip().split('\n')

    resource_name = None
    diff_dict = {}

    for line in lines:
    if line.startswith('='):
    resource_name = line.replace('=', '').strip()
    diff_dict[resource_name] = {'live_state': [], 'desired_state': []}
    elif line.startswith('<'):
    diff_dict[resource_name]['live_state'].append(line[2:])
    elif line.startswith('>'):
    diff_dict[resource_name]['desired_state'].append(line[2:])

    return diff_dict


    def generate_html_page(data):
    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
    <title>App State Comparison</title>
    <style>
    body {
    font-family: Arial, sans-serif;
    }
    table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    }
    th, td {
    padding: 10px;
    white-space: pre-wrap;
    border: 1px solid #ccc;
    text-align: left;
    font-family: monospace; /* Use monospace font for code */
    }
    th {
    background-color: #f2f2f2;
    }
    .left-column, .right-column {
    width: 50%;
    }
    .left-column {
    color: red;
    }
    .right-column {
    color: green;
    }
    h2 {
    font-family: monospace; /* Use monospace font for code */
    margin-top: 8%;
    text-align: center; /* Center the text within h2 tags */
    }
    </style>
    </head>
    <body>
    """

    for app_name, states in data.items():
    html_content += f"""
    <h2>{app_name}</h2>
    <table>
    <colgroup>
    <col class="left-column">
    <col class="right-column">
    </colgroup>
    <tr>
    <th>Live State</th>
    <th>Desired State</th>
    </tr>
    """

    live_state = "<br>".join(states['live_state'])
    desired_state = "<br>".join(states['desired_state'])

    html_content += f"""
    <tr>
    <td class="left-column"><code>{live_state}</code></td>
    <td class="right-column"><code>{desired_state}</code></td>
    </tr>
    """

    html_content += """
    </table>
    """

    html_content += """
    </body>
    </html>
    """

    return html_content

    if __name__ == "__main__":
    # Read argocd app diff output from stdin
    diff_output = sys.stdin.read()

    # Parse the diff output
    changes = parse_argocd_diff_output(diff_output)
    print(generate_html_page(changes))
    # print(json.dumps(changes, indent=4))
    # Generate HTML table with two columns (Existing and Desired)
    # table_html = generate_html_table(changes)

    # Print or save the HTML table as needed
    # print(table_html)