-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for mutating webhooks with side effects when batch dry-run se…
…rver-side apply (#798)
- Loading branch information
1 parent
10d55c0
commit 5f745df
Showing
9 changed files
with
332 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
lib/krane/kubernetes_resource/mutating_webhook_configuration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
module Krane | ||
class MutatingWebhookConfiguration < KubernetesResource | ||
GLOBAL = true | ||
|
||
class Webhook | ||
EQUIVALENT = 'Equivalent' | ||
EXACT = 'Exact' | ||
|
||
class Rule | ||
def initialize(definition) | ||
@definition = definition | ||
end | ||
|
||
def matches_resource?(resource, accept_equivalent:) | ||
groups.each do |group| | ||
versions.each do |version| | ||
resources.each do |kind| | ||
return true if (resource.group == group || group == '*' || accept_equivalent) && | ||
(resource.version == version || version == '*' || accept_equivalent) && | ||
(resource.type.downcase == kind.downcase.singularize || kind == "*") | ||
end | ||
end | ||
end | ||
false | ||
end | ||
|
||
def groups | ||
@definition.dig('apiGroups') | ||
end | ||
|
||
def versions | ||
@definition.dig('apiVersions') | ||
end | ||
|
||
def resources | ||
@definition.dig('resources') | ||
end | ||
end | ||
|
||
def initialize(definition) | ||
@definition = definition | ||
end | ||
|
||
def side_effects | ||
@definition.dig('sideEffects') | ||
end | ||
|
||
def has_side_effects? | ||
!%w(None NoneOnDryRun).include?(side_effects) | ||
end | ||
|
||
def match_policy | ||
@definition.dig('matchPolicy') | ||
end | ||
|
||
def matches_resource?(resource, skip_rule_if_side_effect_none: true) | ||
return false if skip_rule_if_side_effect_none && !has_side_effects? | ||
rules.any? do |rule| | ||
rule.matches_resource?(resource, accept_equivalent: match_policy == EQUIVALENT) | ||
end | ||
end | ||
|
||
def rules | ||
@definition.fetch('rules', []).map { |rule| Rule.new(rule) } | ||
end | ||
end | ||
|
||
def initialize(namespace:, context:, definition:, logger:, statsd_tags:) | ||
@webhooks = (definition.dig('webhooks') || []).map { |hook| Webhook.new(hook) } | ||
super(namespace: namespace, context: context, definition: definition, | ||
logger: logger, statsd_tags: statsd_tags) | ||
end | ||
|
||
TIMEOUT = 30.seconds | ||
|
||
def deploy_succeeded? | ||
exists? | ||
end | ||
|
||
def webhooks | ||
@definition.fetch('webhooks', []).map { |webhook| Webhook.new(webhook) } | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
test/fixtures/mutating_webhook_configurations/ingress_hook.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1beta1 | ||
kind: MutatingWebhookConfiguration | ||
metadata: | ||
name: ingress-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1beta1 | ||
clientConfig: | ||
service: | ||
name: ingress-hook | ||
namespace: test | ||
path: "/ingress-hook" | ||
port: 443 | ||
failurePolicy: Ignore | ||
matchPolicy: Exact | ||
name: ingress-hook.hooks.admission.krane.com | ||
reinvocationPolicy: Never | ||
rules: | ||
- apiGroups: | ||
- extensions | ||
apiVersions: | ||
- v1beta1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- ingresses | ||
scope: "*" | ||
sideEffects: Unknown | ||
timeoutSeconds: 1 |
31 changes: 31 additions & 0 deletions
31
test/fixtures/mutating_webhook_configurations/secret_hook.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1beta1 | ||
kind: MutatingWebhookConfiguration | ||
metadata: | ||
name: secret-hook-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1beta1 | ||
clientConfig: | ||
service: | ||
name: secret-hook | ||
namespace: test | ||
path: "/secret-hook" | ||
port: 443 | ||
failurePolicy: Ignore | ||
matchPolicy: Equivalent | ||
name: secret-hook.hooks.admission.krane.com | ||
reinvocationPolicy: Never | ||
rules: | ||
- apiGroups: | ||
- core | ||
apiVersions: | ||
- v1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- secrets | ||
scope: "*" | ||
sideEffects: Unknown | ||
timeoutSeconds: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
test/unit/krane/kubernetes_resource/mutating_webhook_configuration_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class MutatingWebhookConfigurationTest < Krane::TestCase | ||
def test_load_from_json | ||
definition = YAML.load_file(File.join(fixture_path("mutating_webhook_configurations"), "secret_hook.yaml")) | ||
webhook_configuration = Krane::MutatingWebhookConfiguration.new( | ||
namespace: 'test', context: 'nope', definition: definition, | ||
logger: @logger, statsd_tags: nil | ||
) | ||
assert_equal(webhook_configuration.webhooks.length, 1) | ||
|
||
raw_webhook = definition.dig('webhooks', 0) | ||
webhook = webhook_configuration.webhooks.first | ||
assert_equal(raw_webhook.dig('matchPolicy'), webhook.match_policy) | ||
assert_equal(raw_webhook.dig('sideEffects'), webhook.side_effects) | ||
|
||
assert_equal(webhook.rules.length, 1) | ||
raw_rule = definition.dig('webhooks', 0, 'rules', 0) | ||
rule = webhook.rules.first | ||
assert_equal(raw_rule.dig('apiGroups'), ['core']) | ||
assert_equal(rule.groups, ['core']) | ||
|
||
assert_equal(raw_rule.dig('apiVersions'), ['v1']) | ||
assert_equal(rule.versions, ['v1']) | ||
|
||
assert_equal(raw_rule.dig('resources'), ['secrets']) | ||
assert_equal(rule.resources, ['secrets']) | ||
end | ||
|
||
def test_webhook_configuration_matches_when_side_effects | ||
secret_def = YAML.load_file(File.join(fixture_path('hello-cloud'), 'secret.yml')) | ||
secret = Krane::Secret.new(namespace: 'test', context: 'nope', definition: secret_def, | ||
logger: @logger, statsd_tags: nil) | ||
|
||
definition = YAML.load_file(File.join(fixture_path("mutating_webhook_configurations"), "secret_hook.yaml")) | ||
webhook_configuration = Krane::MutatingWebhookConfiguration.new( | ||
namespace: 'test', context: 'nope', definition: definition, | ||
logger: @logger, statsd_tags: nil | ||
) | ||
webhook = webhook_configuration.webhooks.first | ||
assert(webhook.has_side_effects?) | ||
assert(webhook.matches_resource?(secret)) | ||
assert(webhook.matches_resource?(secret, skip_rule_if_side_effect_none: true)) | ||
assert(webhook.matches_resource?(secret, skip_rule_if_side_effect_none: false)) | ||
end | ||
|
||
def test_matches_webhook_configuration_doesnt_match_when_no_side_effects_and_flag | ||
secret_def = YAML.load_file(File.join(fixture_path('hello-cloud'), 'secret.yml')) | ||
secret = Krane::Secret.new(namespace: 'test', context: 'nope', definition: secret_def, | ||
logger: @logger, statsd_tags: nil) | ||
|
||
definition = YAML.load_file(File.join(fixture_path("mutating_webhook_configurations"), "secret_hook.yaml")) | ||
webhook_configuration = Krane::MutatingWebhookConfiguration.new( | ||
namespace: 'test', context: 'nope', definition: definition, | ||
logger: @logger, statsd_tags: nil | ||
) | ||
webhook = webhook_configuration.webhooks.first | ||
webhook.stubs(:has_side_effects?).returns(false).at_least_once | ||
refute(webhook.matches_resource?(secret)) | ||
refute(webhook.matches_resource?(secret, skip_rule_if_side_effect_none: true)) | ||
assert(webhook.matches_resource?(secret, skip_rule_if_side_effect_none: false)) | ||
end | ||
|
||
def test_no_match_when_policy_is_exact_and_resource_doesnt_match | ||
secret_def = YAML.load_file(File.join(fixture_path('hello-cloud'), 'secret.yml')) | ||
secret = Krane::Secret.new(namespace: 'test', context: 'nope', definition: secret_def, | ||
logger: @logger, statsd_tags: nil) | ||
|
||
definition = YAML.load_file(File.join(fixture_path("mutating_webhook_configurations"), "secret_hook.yaml")) | ||
webhook_configuration = Krane::MutatingWebhookConfiguration.new( | ||
namespace: 'test', context: 'nope', definition: definition, | ||
logger: @logger, statsd_tags: nil | ||
) | ||
|
||
webhook = webhook_configuration.webhooks.first | ||
assert(webhook.matches_resource?(secret)) | ||
webhook.expects(:match_policy).returns(Krane::MutatingWebhookConfiguration::Webhook::EXACT).at_least(1) | ||
assert(webhook.matches_resource?(secret)) | ||
secret.expects(:group).returns('fake').once | ||
refute(webhook.matches_resource?(secret)) | ||
secret.unstub(:group) | ||
secret.expects(:type).returns('fake').once | ||
refute(webhook.matches_resource?(secret)) | ||
end | ||
end |