Skip to content

Commit

Permalink
ci(helm): added workflow to see the diff (#14134)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladyslav Diachenko <[email protected]>
  • Loading branch information
vlad-diachenko authored Sep 17, 2024
1 parent 39fa9b4 commit 1e939aa
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 1 deletion.
211 changes: 211 additions & 0 deletions .github/workflows/helm-loki-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
---
name: helm-loki-ci
on:
pull_request:
paths:
- "production/helm/loki/**"

jobs:
publish-diff:
name: Publish Rendered Helm Chart Diff
runs-on: ubuntu-latest
steps:
- name: Setup Helm
uses: azure/setup-helm@v4

- name: Add required Helm repositories
run: |
helm repo add minio https://charts.min.io/
helm repo add grafana https://grafana.github.io/helm-charts
helm repo add grafana-operator https://grafana.github.io/helm-charts
helm repo update
- name: Prepare directories for base and PR branches
run: |
mkdir -p ${{ github.workspace }}/base
mkdir -p ${{ github.workspace }}/pr
mkdir -p ${{ github.workspace }}/output
mkdir -p ${{ github.workspace }}/output/base
mkdir -p ${{ github.workspace }}/output/pr
- name: Checkout base branch to 'base' folder within workspace
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
path: ${{ github.workspace }}/base

- name: Checkout PR branch to 'pr' folder within workspace
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
path: ${{ github.workspace }}/pr

- name: Render Helm chart for each scenario in the base branch
run: |
cd ${{ github.workspace }}/base/production/helm/loki
if [ ! -d "scenarios" ]; then
echo "Directory with the scenarios does not exist in base branch, skipping rendering them."
exit 0
fi
helm dependency build
for file in scenarios/*.yaml; do
cat "$file"
schenario_folder=${{ github.workspace }}/output/base/$(basename $file .yaml)
mkdir $schenario_folder
helm template loki-test-chart-name . -f $file --output-dir $schenario_folder
done
- name: Render Helm chart for each scenario in the PR branch
run: |
cd ${{ github.workspace }}/pr/production/helm/loki
helm dependency build
for file in scenarios/*.yaml; do
cat "$file"
schenario_folder=${{ github.workspace }}/output/pr/$(basename $file .yaml)
mkdir $schenario_folder
helm template loki-test-chart-name . -f $file --output-dir $schenario_folder
done
- name: Calculate the diff between base and PR rendered manifests for each scenario
run: |
cd ${{ github.workspace }}/pr/production/helm/loki
for scenario_file in scenarios/*.yaml; do
added_files='[]'
modified_files='[]'
removed_files='[]'
scenario_name=$(basename $scenario_file .yaml)
base_branch_dir=${{ github.workspace }}/output/base/$scenario_name
pr_branch_dir=${{ github.workspace }}/output/pr/$scenario_name
echo "Comparing directories: $base_branch_dir and $pr_branch_dir"
# Find all files in the left and right directories
base_branch_files=$(if [[ -d "$base_branch_dir" ]]; then find "$base_branch_dir" -type f | sed "s|$base_branch_dir/||"; else echo ""; fi)
pr_branch_files=$(find "$pr_branch_dir" -type f | sed "s|$pr_branch_dir/||")
# Check for modified and removed files
for file in $base_branch_files; do
echo "check if file exists: $file"
if [[ -f "$pr_branch_dir/$file" ]]; then
echo "File exists in both directories, check if it is modified"
if ! diff -q "$base_branch_dir/$file" "$pr_branch_dir/$file" >/dev/null; then
echo "file is modified $file"
file_diff=$(diff -c "$base_branch_dir/$file" "$pr_branch_dir/$file" || true)
diff_obj=$(jq -n --arg file "$file" --arg diff "$file_diff" '{"filename": $file, "diff": $diff}')
# Append the new object to the JSON array using jq
modified_files=$(echo "$modified_files" | jq --argjson diff_obj "$diff_obj" '. += [$diff_obj]')
else
echo "file is not modified"
fi
else
echo "file is removed $file"
# File is missing in the PR directory
file_content=$(cat "$base_branch_dir/$file")
removed_obj=$(jq -n --arg filename "$file" --arg content "$file_content" '{"filename": $filename, "content": $content}')
# Append the new object to the JSON array using jq
removed_files=$(echo "$removed_files" | jq --argjson removed_obj "$removed_obj" '. += [$removed_obj]')
fi
done
# Check for added files in the right directory
for file in $pr_branch_files; do
if [[ ! -f "$base_branch_dir/$file" ]]; then
echo "added file detected"
# File is missing in the PR directory
file_content=$(cat "$pr_branch_dir/$file")
added_obj=$(jq -n --arg file "$file" --arg content "$file_content" '{"filename": $file, "content": $content}')
# Append the new object to the JSON array using jq
added_files=$(echo "$added_files" | jq --argjson added_obj "$added_obj" '. += [$added_obj]')
fi
done
scenario_output_dir="${{ github.workspace }}/output/$scenario_name"
mkdir $scenario_output_dir
echo $added_files > $scenario_output_dir/added_files.json
echo $modified_files > $scenario_output_dir/modified_files.json
echo $removed_files > $scenario_output_dir/removed_files.json
echo $removed_files
done
- name: Generate Markdown Summary
run: |
# Initialize the Markdown output file
output_file="${{ github.workspace }}/output/diff_summary.md"
echo "# Kubernetes Manifest Diff Summary" > $output_file
# Iterate over each scenario file
for file in ${{ github.workspace }}/pr/production/helm/loki/scenarios/*.yaml; do
scenario=$(basename "$file" .yaml)
echo "Processing scenario: $scenario"
# Read JSON data for added, modified, and removed files
added_files=$(cat ${{ github.workspace }}/output/$scenario/added_files.json)
modified_files=$(cat ${{ github.workspace }}/output/$scenario/modified_files.json)
removed_files=$(cat ${{ github.workspace }}/output/$scenario/removed_files.json)
# Count the number of added, modified, and removed files
num_added=$(echo "$added_files" | jq length)
num_modified=$(echo "$modified_files" | jq length)
num_removed=$(echo "$removed_files" | jq length)
# Create a header for the scenario
echo -e "\n<details><summary>Scenario: $scenario (Added: $num_added, Modified: $num_modified, Removed: $num_removed) </summary>\n" >> $output_file
echo -e "<p>\n\n" >> $output_file

# Add summary counts
echo -e "\n**Summary:**" >> $output_file
echo -e "\n- **Added:** $num_added" >> $output_file
echo -e "\n- **Modified:** $num_modified" >> $output_file
echo -e "\n- **Removed:** $num_removed" >> $output_file

# Add details for added files
echo -e "\n### Added Files" >> $output_file
if [[ "$num_added" -gt 0 ]]; then
echo "$added_files" | jq -c '.[]' | while read -r obj; do
filename=$(echo "$obj" | jq -r '.filename')
content=$(echo "$obj" | jq -r '.content')
echo -e "\n<details><summary>$filename</summary>" >> $output_file
echo -e "\n\`\`\`yaml\n$content\n\`\`\`\n</details>" >> $output_file
done
else
echo -e "\n_No added files_\n" >> $output_file
fi

# Add details for modified files
echo -e "\n### Modified Files" >> $output_file
if [[ "$num_modified" -gt 0 ]]; then
echo "$modified_files" | jq -c '.[]' | while read -r obj; do
filename=$(echo "$obj" | jq -r '.filename')
diff=$(echo "$obj" | jq -r '.diff')
echo -e "\n<details><summary>$filename</summary>" >> $output_file
echo -e "\n\`\`\`diff\n$diff\n\`\`\`\n</details>" >> $output_file
done
else
echo -e "\n_No modified files_\n" >> $output_file
fi

# Add details for removed files
echo -e "\n### Removed Files" >> $output_file
if [[ "$num_removed" -gt 0 ]]; then
echo "$removed_files" | jq -c '.[]' | while read -r obj; do
filename=$(echo "$obj" | jq -r '.filename')
content=$(echo "$obj" | jq -r '.content')
echo -e "\n<details><summary>$filename</summary>" >> $output_file
echo -e "\n\`\`\`yaml\n$content\n\`\`\`\n</details>" >> $output_file
done
else
echo -e "\n_No removed files_\n" >> $output_file
fi

# close <p> and <details>
echo -e "\n\n</p>\n</details>" >> $output_file
done

- name: Post diff as PR comment
uses: marocchino/sticky-pull-request-comment@v2
with:
hide_and_recreate: true
hide_classify: "OUTDATED"
path: ${{ github.workspace }}/output/diff_summary.md
2 changes: 1 addition & 1 deletion .github/workflows/metrics-collector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

jobs:
main:
if: github.owner == "grafana"
if: github.owner == 'grafana'
runs-on: ubuntu-latest
steps:
- name: Checkout Actions
Expand Down
19 changes: 19 additions & 0 deletions production/helm/loki/scenarios/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
These scenarios are used by Github Workflow: [Publish Rendered Helm Chart Diff](../../../../.github/workflows/helm-loki-ci.yml).

Each scenario is used as the values file for the Loki Helm chart to render Kubernetes manifests in `base` and `PR's` branch to compare the content and report the diff on Pull Request as a comment([example](https://github.com/grafana/loki/pull/14127#issuecomment-2348360828)). It gives the ability to the reviewer to understand how the changes in the chart modify resulting manifests.

![img.png](images/img.png)

The workflow reports three types of changes for each scenario:

1. Added files - the manifests that are added in the current PR and that did not exist in `base` branch.

![added.png](images/added.png)


2. Modified files - the manifests that exist in both branches but the changes in PRs branch modify them.
![modified.png](images/modified.png)

3. Removed files - the manifests that exist in `base` branch but do not exist in PRs branch.

![removed.png](images/removed.png)
71 changes: 71 additions & 0 deletions production/helm/loki/scenarios/default-single-binary-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
loki:
schemaConfig:
configs:
- from: 2024-04-01
store: tsdb
object_store: s3
schema: v13
index:
prefix: loki_index_
period: 24h
ingester:
chunk_encoding: snappy
tracing:
enabled: true
querier:
# Default is 4, if you have enough memory and CPU you can increase, reduce if OOMing
max_concurrent: 4

#gateway:
# ingress:
# enabled: true
# hosts:
# - host: FIXME
# paths:
# - path: /
# pathType: Prefix

deploymentMode: Distributed

ingester:
replicas: 3
querier:
replicas: 3
maxUnavailable: 2
queryFrontend:
replicas: 2
maxUnavailable: 1
queryScheduler:
replicas: 2
distributor:
replicas: 3
maxUnavailable: 2
compactor:
replicas: 1
indexGateway:
replicas: 2
maxUnavailable: 1

# optional experimental components
bloomPlanner:
replicas: 0
bloomBuilder:
replicas: 0
bloomGateway:
replicas: 0

# Enable minio for storage
minio:
enabled: true

# Zero out replica counts of other deployment modes
backend:
replicas: 0
read:
replicas: 0
write:
replicas: 0

singleBinary:
replicas: 0
16 changes: 16 additions & 0 deletions production/helm/loki/scenarios/default-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
loki:
commonConfig:
replication_factor: 1
useTestSchema: true
storage:
bucketNames:
chunks: chunks
ruler: ruler
admin: admin
read:
replicas: 1
write:
replicas: 1
backend:
replicas: 1
Binary file added production/helm/loki/scenarios/images/added.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added production/helm/loki/scenarios/images/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added production/helm/loki/scenarios/images/removed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions production/helm/loki/scenarios/ingress-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
gateway:
ingress:
enabled: true
annotations: {}
hosts:
- host: gateway.loki.example.com
paths:
- path: /
pathType: Prefix
loki:
commonConfig:
replication_factor: 1
useTestSchema: true
storage:
bucketNames:
chunks: chunks
ruler: ruler
admin: admin
read:
replicas: 1
write:
replicas: 1
backend:
replicas: 1
monitoring:
lokiCanary:
enabled: false
test:
enabled: false
27 changes: 27 additions & 0 deletions production/helm/loki/scenarios/legacy-monitoring-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
loki:
commonConfig:
replication_factor: 1
useTestSchema: true
storage:
bucketNames:
chunks: chunks
ruler: ruler
admin: admin
read:
replicas: 1
write:
replicas: 1
backend:
replicas: 1
monitoring:
enabled: true
selfMonitoring:
enabled: true
grafanaAgent:
installOperator: true
serviceMonitor:
labels:
release: "prometheus"
test:
prometheusAddress: "http://prometheus-kube-prometheus-prometheus.prometheus.svc.cluster.local.:9090"
Loading

0 comments on commit 1e939aa

Please sign in to comment.