Skip to content

Commit 08339fd

Browse files
committed
fix(chart): preserve enclosing hooks and policy type defaults
Signed-off-by: Tom Brow <[email protected]>
1 parent 528fea0 commit 08339fd

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎deploy/charts/buzz-push-gateway/tests/check-external-policy.rb‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@
1616
end.compact
1717
# Match Kubernetes Unstructured.IsList: an items array, including typed lists.
1818
# https://github.com/kubernetes/apimachinery/blob/v0.34.1/pkg/apis/meta/v1/unstructured/unstructured.go
19-
def flatten_resources(resource)
20-
if resource.is_a?(Hash) && resource['items'].is_a?(Array)
21-
resource.fetch('items').flat_map { |item| flatten_resources(item) }
19+
def flatten_resources(resource, enclosing_hooks = {})
20+
annotations = resource.dig('metadata', 'annotations') || {}
21+
hooks = enclosing_hooks.merge(annotations.select { |k, _| %w[helm.sh/hook argocd.argoproj.io/hook].include?(k) })
22+
if resource['items'].is_a?(Array)
23+
resource.fetch('items').flat_map { |item| flatten_resources(item, hooks) }
2224
else
25+
unless hooks.empty?
26+
resource = Marshal.load(Marshal.dump(resource))
27+
resource['metadata'] ||= {}
28+
resource['metadata']['annotations'] = annotations.merge(hooks)
29+
end
2330
[resource]
2431
end
2532
end
@@ -89,7 +96,12 @@ def label_key?(key)
8996
end
9097
abort "external policy #{name}: expression does not match runtime" unless matches
9198
end
92-
abort "external policy #{name}: must isolate ingress and egress" unless %w[Ingress Egress].all? { |t| spec.fetch('policyTypes', []).include?(t) }
99+
types = spec['policyTypes']
100+
if types.nil? || types.empty?
101+
types = ['Ingress']
102+
types << 'Egress' unless spec.fetch('egress', []).empty?
103+
end
104+
abort "external policy #{name}: must isolate ingress and egress" unless %w[Ingress Egress].all? { |t| types.include?(t) }
93105
end
94106
# Preserve Helm output byte-for-byte only after every deployment passes.
95107
STDOUT.write(input)

‎deploy/charts/buzz-push-gateway/tests/runtime-options.sh‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ expression_policy = Marshal.load(Marshal.dump(policy))
110110
identity = expression_policy['spec']['podSelector'].delete('matchLabels')
111111
expression_policy['spec']['podSelector']['matchExpressions'] = identity.map { |k, v| {'key' => k, 'operator' => 'In', 'values' => [v]} }
112112
File.write("#{ARGV[0]}/expression-identity.yaml", (resources + [expression_policy]).map(&:to_yaml).join)
113+
%w[helm.sh/hook argocd.argoproj.io/hook].each_with_index do |key, i|
114+
wrapped = Marshal.load(Marshal.dump(typed_list))
115+
wrapped['metadata'] = {'annotations' => {key => i == 0 ? 'pre-upgrade' : 'PreSync'}}
116+
File.write("#{ARGV[0]}/wrapped-hook-#{i}.yaml", (resources + [wrapped]).map(&:to_yaml).join)
117+
end
118+
defaulted = Marshal.load(Marshal.dump(policy))
119+
defaulted['spec'].delete('policyTypes')
120+
defaulted['spec']['egress'] = [{'ports' => [{'protocol' => 'TCP', 'port' => 443}]}]
121+
File.write("#{ARGV[0]}/defaulted-types.yaml", (resources + [defaulted]).map(&:to_yaml).join)
113122
variants = {
114123
'invalid-key' => ->(p) { p['spec']['podSelector']['matchExpressions'] = [{'key' => 'bad key', 'operator' => 'DoesNotExist'}] },
115124
'invalid-value' => ->(p) { p['spec']['podSelector']['matchExpressions'] = [{'key' => 'absent', 'operator' => 'NotIn', 'values' => ['bad value']}] },
@@ -140,11 +149,11 @@ gate <"$out/combined.yaml" >"$out/gated.yaml"
140149
cmp "$out/combined.yaml" "$out/gated.yaml"
141150
gate <"$out/list.yaml" >"$out/gated.yaml"
142151
cmp "$out/list.yaml" "$out/gated.yaml"
143-
for valid in typed-list expression-identity; do
152+
for valid in defaulted-types typed-list expression-identity; do
144153
gate <"$out/$valid.yaml" >"$out/gated.yaml"
145154
cmp "$out/$valid.yaml" "$out/gated.yaml"
146155
done
147-
for mutation in duplicate-typed-list argo-skip argo-presync invalid-key invalid-value numeric-value unknown-operator empty-notin valued-exists duplicate-list external hook-policy misspelled wrong-selector empty-selector wrong-namespace ingress-only wrong-expression; do
156+
for mutation in wrapped-hook-0 wrapped-hook-1 duplicate-typed-list argo-skip argo-presync invalid-key invalid-value numeric-value unknown-operator empty-notin valued-exists duplicate-list external hook-policy misspelled wrong-selector empty-selector wrong-namespace ingress-only wrong-expression; do
148157
if gate <"$out/$mutation.yaml" >"$out/gated.yaml" 2>"$out/gate-error"; then
149158
echo "expected combined-render gate to reject $mutation" >&2
150159
exit 1

0 commit comments

Comments
 (0)