-
Notifications
You must be signed in to change notification settings - Fork 56
/
check-examples.py
executable file
·47 lines (38 loc) · 1.45 KB
/
check-examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
import yaml
import os
import sys
def load_gvks(path, loader):
types = set()
for root, _, files in os.walk(path):
for f in files:
if f.endswith(".yaml"):
with open(os.path.join(root, f)) as s:
for t in yaml.safe_load_all(s):
for gvk in loader(t):
types.add(gvk)
return types
def load_crd_type(t):
kind = t["spec"]["names"]["kind"]
group = t["spec"]["group"]
for v in t["spec"]["versions"]:
yield f'{kind}.{group}/{v["name"]}'
exception_set = {
'ProviderConfigUsage.tf.upbound.io/v1beta1',
'StoreConfig.tf.upbound.io/v1beta1'
}
# Example usage: check-examples.py <CRD dir> <example manifests dir>
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Example usage: check-examples.py <CRD dir> <example manifests dir>")
sys.exit(1)
known_crd_types = load_gvks(sys.argv[1], load_crd_type)
example_types = load_gvks(sys.argv[2], lambda t: [] if t is None or not {"kind", "apiVersion"}.issubset(t.keys())
else [f'{t["kind"]}.{t["apiVersion"]}'])
diff = known_crd_types.difference(example_types.union(exception_set))
if len(diff) == 0:
print("All CRDs have at least one example...")
print(f'Exceptions allowed for: {exception_set}')
sys.exit(0)
print(f'Please add example manifests for the following types: {diff}')
sys.exit(2)