Skip to content

Commit 57b85a7

Browse files
authored
chore(ci): more generic CRD validation filtering (#23432)
1 parent 76128b4 commit 57b85a7

3 files changed

Lines changed: 81 additions & 59 deletions

File tree

‎.github/workflows/check-crd-compatibility.yaml‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,17 @@ jobs:
8888
8989
- name: Run crdify for platform operator
9090
run: |
91-
set -e
91+
set -euo pipefail
9292
old_path="$PWD/.old-stackrox/operator/config/crd/bases"
9393
new_path="$PWD/operator/config/crd/bases"
94-
crdify="go run -C operator/tools/crdify sigs.k8s.io/crdify --config=config.yaml"
95-
set -x
96-
$crdify "file://${old_path}/platform.stackrox.io_securedclusters.yaml" "file://${new_path}/platform.stackrox.io_securedclusters.yaml"
97-
$crdify "file://${old_path}/platform.stackrox.io_centrals.yaml" "file://${new_path}/platform.stackrox.io_centrals.yaml"
94+
crdify=".github/workflows/scripts/crdify.sh"
95+
$crdify "${old_path}/platform.stackrox.io_securedclusters.yaml" "${new_path}/platform.stackrox.io_securedclusters.yaml"
96+
$crdify "${old_path}/platform.stackrox.io_centrals.yaml" "${new_path}/platform.stackrox.io_centrals.yaml"
9897
9998
- name: Run crdify for config operator
10099
run: |
101100
set -euo pipefail
102101
old_path="$PWD/.old-stackrox/config-controller/config/crd/bases"
103102
new_path="$PWD/config-controller/config/crd/bases"
104-
policy_file=config.stackrox.io_securitypolicies.yaml
105-
# The installed CRD keeps the exclusion CEL rule. crdify 0.6.0 treats
106-
# x-kubernetes-validations as an unhandled error, so compare a copy
107-
# with that one validation removed.
108-
filtered="$(mktemp -d)"
109-
.github/workflows/scripts/strip-securitypolicy-atmostoneof.sh \
110-
"${old_path}/${policy_file}" "${filtered}/old-${policy_file}"
111-
.github/workflows/scripts/strip-securitypolicy-atmostoneof.sh \
112-
"${new_path}/${policy_file}" "${filtered}/new-${policy_file}"
113-
crdify="go run -C operator/tools/crdify sigs.k8s.io/crdify --config=config.yaml"
114-
set -x
115-
$crdify "file://${filtered}/old-${policy_file}" "file://${filtered}/new-${policy_file}"
103+
crdify=".github/workflows/scripts/crdify.sh"
104+
$crdify "${old_path}/config.stackrox.io_securitypolicies.yaml" "${new_path}/config.stackrox.io_securitypolicies.yaml"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
# Run crdify on copies of CRDs after stripping out known false positives.
3+
# Usage:
4+
# crdify.sh <old-crd> <new-crd> [crdify flags]
5+
6+
set -euo pipefail
7+
8+
ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")/../../.."
9+
readonly ROOT_DIR
10+
11+
if [[ $# -lt 2 ]]; then
12+
echo "usage: $0 <old-crd> <new-crd> [crdify flags]" >&2
13+
exit 2
14+
fi
15+
16+
old_crd="${1:?missing required argument 'old-crd'}"
17+
new_crd="${2:?missing required argument 'new-crd'}"
18+
shift
19+
shift
20+
21+
if [[ ! -f "$old_crd" ]]; then
22+
echo "::error::CRD file not found: $old_crd" >&2
23+
exit 1
24+
fi
25+
26+
if [[ ! -f "$new_crd" ]]; then
27+
echo "::error::CRD file not found: $new_crd" >&2
28+
exit 1
29+
fi
30+
31+
32+
# The addition of this particular validation rule, even though considered a breaking change by definition
33+
# (https://github.com/kubernetes/community/blob/main/contributors/devel/sig-architecture/api_changes.md#backward-compatibility-gotchas)
34+
# is fine in practice, since the `excludeByKind` branch is being added in the same release cycle as the rule itself,
35+
# meaning no production CRS could have existed that violated the validation.
36+
filter_spec_exclusions_one_of() {
37+
local input="$1"
38+
local output="$2"
39+
40+
local message='at most one of the fields in [deployment excludeByKind] may be set'
41+
local rule='(has(self.deployment)?1:0)+(has(self.excludeByKind)?1:0) <= 1'
42+
43+
# Remove matching rule, and the validations list if empty afterwards.
44+
message="${message}" rule="${rule}" \
45+
go run -C "${ROOT_DIR}/operator/tools/yq" github.com/mikefarah/yq/v4 eval '
46+
del(
47+
.spec.versions[].schema.openAPIV3Schema.properties.spec.properties.exclusions.items."x-kubernetes-validations"[]
48+
| select(.message == strenv(message) and .rule == strenv(rule))
49+
)
50+
| del(
51+
.spec.versions[].schema.openAPIV3Schema.properties.spec.properties.exclusions.items."x-kubernetes-validations"
52+
| select(length == 0)
53+
)
54+
' "$input" > "$output"
55+
}
56+
57+
58+
crd_basename="$(basename "${old_crd}")"
59+
case "$crd_basename" in
60+
config.stackrox.io_securitypolicies.yaml)
61+
filtered_dir="$(mktemp -d)"
62+
echo >&2 "Filtering known false positives from ${old_crd} and ${new_crd}"
63+
filter_spec_exclusions_one_of "${old_crd}" "${filtered_dir}/old-${crd_basename}"
64+
filter_spec_exclusions_one_of "${new_crd}" "${filtered_dir}/new-${crd_basename}"
65+
old_crd="${filtered_dir}/old-${crd_basename}"
66+
new_crd="${filtered_dir}/new-${crd_basename}"
67+
;;
68+
*)
69+
echo >&2 "No filtering rules for ${old_crd}, running crdify on original CRDs."
70+
;;
71+
esac
72+
73+
set -x
74+
go run -C "${ROOT_DIR}/operator/tools/crdify" sigs.k8s.io/crdify --config=config.yaml "$@" \
75+
"file://${old_crd}" "file://${new_crd}"

‎.github/workflows/scripts/strip-securitypolicy-atmostoneof.sh‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)