Skip to content

Commit aeba35c

Browse files
committed
ci: add check for policy samples
This adds a GitHub Action workflow to check that the policy samples have been updated with each PR. We'll add it as a required check to catch policy errors earlier in the dev process. I also tweaked the update script to have better error handling, and I simplified the PR template to make it more readable.
1 parent 6181fd8 commit aeba35c

File tree

3 files changed

+83
-34
lines changed

3 files changed

+83
-34
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
<!--
2-
COMMENT BLOCKS WILL NOT BE INCLUDED IN THE PR.
3-
Feel free to delete sections of the template which do not apply to your PR, or add additional details
4-
-->
5-
61
###### Merge Checklist <!-- REQUIRED -->
7-
<!-- You can set them now ([x]) or set them later using the Github UI -->
8-
<!-- **All** boxes should be checked before merging the PR *(just tick any boxes which don't apply to this PR)* -->
92
- [ ] Followed patch format from upstream recommendation: https://github.com/kata-containers/community/blob/main/CONTRIBUTING.md#patch-format
103
- [ ] Included a single commit in a given PR - at least unless there are related commits and each makes sense as a change on its own.
114
- [ ] Aware about the PR to be merged using "create a merge commit" rather than "squash and merge" (or similar)
125
- [ ] genPolicy only: Ensured the tool still builds on Windows
13-
- [ ] genPolicy only: Updated sample YAMLs' policy annotations, if applicable
14-
- [ ] The `upstream-missing` label (or `upstream-not-needed`) has been set on the PR.
6+
- [ ] The `upstream/missing` label (or `upstream/not-needed`) has been set on the PR.
157

168
###### Summary <!-- REQUIRED -->
179
<!-- Quick explanation of WHAT changed and WHY. -->
1810

19-
###### Associated issues <!-- optional -->
20-
<!-- Link to Github issues if possible. -->
21-
22-
###### Links to CVEs <!-- optional -->
23-
<!-- https://nvd.nist.gov/vuln/detail/CVE-YYYY-XXXX -->
24-
2511
###### Test Methodology
2612
<!-- How was this test validated? i.e. local build, pipeline build etc. -->
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) Microsoft Corporation.
2+
3+
name: Check policy samples
4+
5+
on:
6+
pull_request:
7+
8+
jobs:
9+
check-policy-samples:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
14+
- name: Check out code
15+
uses: actions/checkout@v4
16+
17+
- name: Install yq
18+
env:
19+
INSTALL_IN_GOPATH: false
20+
run: |
21+
./ci/install_yq.sh
22+
23+
- name: Install Rust
24+
run: |
25+
./tests/install_rust.sh
26+
echo "${HOME}/.cargo/bin" >> $GITHUB_PATH
27+
28+
- name: Install protobuf-compiler
29+
run: |
30+
sudo apt-get -y install protobuf-compiler
31+
32+
- name: Configure containerd
33+
run: |
34+
sudo containerd config default | sudo dd of=/etc/containerd/config.toml
35+
sudo systemctl restart containerd
36+
sudo systemctl is-active containerd
37+
38+
- name: Update policy samples
39+
working-directory: ./src/tools/genpolicy
40+
run: |
41+
python3 update_policy_samples.py
42+
43+
- name: Show diff
44+
run: |
45+
git diff
46+
47+
- name: Check policy samples
48+
run: |
49+
git diff-files --exit-code
Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from concurrent.futures import ThreadPoolExecutor
1+
import concurrent.futures
22
import os
33
import subprocess
44
import sys
@@ -19,42 +19,56 @@
1919
file_base_path = "../../agent/samples/policy/yaml"
2020

2121
def runCmd(arg):
22-
return subprocess.run([arg], stdout=sys.stdout, stderr=sys.stderr, universal_newlines=True, input="", shell=True)
22+
return subprocess.run([arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, input="", shell=True, check=True)
2323

2424
def timeRunCmd(arg):
25+
log = [f"========== COMMAND: {arg}"]
2526
start = time.time()
26-
proc = runCmd(arg)
27-
end = time.time()
2827

29-
log = f"COMMAND: {arg}\n"
30-
if proc.returncode != 0:
31-
log += f"`{arg}` failed with exit code {proc.returncode}. Stderr: {proc.stderr}, Stdout: {proc.stdout}\n"
32-
log += f"Time taken: {round(end - start, 2)} seconds"
33-
print(log)
28+
try:
29+
p = runCmd(arg)
30+
except subprocess.CalledProcessError as e:
31+
log.append(e.stdout)
32+
log.append(f"+++++ Failed with exit code {e.returncode}")
33+
raise
34+
else:
35+
if p.stdout:
36+
log.append(p.stdout)
37+
finally:
38+
end = time.time()
39+
log.append(f"Time taken: {round(end - start, 2)} seconds")
40+
print("\n".join(log))
3441

3542
# check we can access all files we are about to update
3643
for file in default_yamls + silently_ignored + no_policy:
3744
filepath = os.path.join(file_base_path, file)
3845
if not os.path.exists(filepath):
39-
print(f"filepath does not exists: {filepath}")
46+
sys.exit(f"filepath does not exists: {filepath}")
4047

4148
# build tool
42-
print("COMMAND: cargo build")
43-
runCmd("cargo build")
49+
print("========== COMMAND: LIBC=gnu BUILD_TYPE= make")
50+
runCmd("LIBC=gnu BUILD_TYPE= make")
4451

4552
# update files
46-
genpolicy_path = "target/debug/genpolicy"
53+
genpolicy_path = "./target/x86_64-unknown-linux-gnu/debug/genpolicy"
4754

4855
total_start = time.time()
49-
executor = ThreadPoolExecutor(max_workers=os.cpu_count())
5056

51-
for file in default_yamls + no_policy + needs_containerd_pull:
52-
executor.submit(timeRunCmd, f"sudo {genpolicy_path} -d -y {os.path.join(file_base_path, file)}")
57+
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
58+
futures = []
5359

54-
for file in silently_ignored:
55-
executor.submit(timeRunCmd, f"sudo {genpolicy_path} -d -s -y {os.path.join(file_base_path, file)}")
60+
for file in default_yamls + no_policy + needs_containerd_pull:
61+
cmd = f"sudo {genpolicy_path} -d -y {os.path.join(file_base_path, file)}"
62+
futures.append(executor.submit(timeRunCmd, cmd))
63+
64+
for file in silently_ignored:
65+
cmd = f"sudo {genpolicy_path} -d -s -y {os.path.join(file_base_path, file)}"
66+
futures.append(executor.submit(timeRunCmd, cmd))
67+
68+
for future in concurrent.futures.as_completed(futures):
69+
# Surface any potential exception thrown by the future.
70+
future.result()
5671

57-
executor.shutdown()
5872
total_end = time.time()
5973

6074
print(f"Total time taken: {total_end - total_start} seconds")

0 commit comments

Comments
 (0)