Skip to content

Instantly share code, notes, and snippets.

@mdraevich
Created January 26, 2024 01:08
Show Gist options
  • Save mdraevich/c14308d43d6c6067da41ac4fe0817117 to your computer and use it in GitHub Desktop.
Save mdraevich/c14308d43d6c6067da41ac4fe0817117 to your computer and use it in GitHub Desktop.
Convert ArgoCD CLI app diff to HTML-page (Ubuntu-based image might be required)

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
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment