-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable per-resource timeout customization via an annotation #232
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -3,14 +3,18 @@ | |
|
||
class KubernetesResourceTest < KubernetesDeploy::TestCase | ||
class DummyResource < KubernetesDeploy::KubernetesResource | ||
def initialize(*) | ||
definition = { "kind" => "DummyResource", "metadata" => { "name" => "test" } } | ||
def initialize(definition_extras: {}) | ||
definition = { "kind" => "DummyResource", "metadata" => { "name" => "test" } }.merge(definition_extras) | ||
super(namespace: 'test', context: 'test', definition: definition, logger: @logger) | ||
end | ||
|
||
def exists? | ||
true | ||
end | ||
|
||
def file_path | ||
"/tmp/foo/bar" | ||
end | ||
end | ||
|
||
def test_service_and_deployment_timeouts_are_equal | ||
|
@@ -53,8 +57,88 @@ def test_fetch_events_returns_empty_hash_when_kubectl_results_empty | |
assert_operator events, :empty? | ||
end | ||
|
||
def test_can_override_hardcoded_timeout_via_an_annotation | ||
basic_resource = DummyResource.new | ||
assert_equal 5.minutes, basic_resource.timeout | ||
|
||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("60")) | ||
assert_equal 60, customized_resource.timeout | ||
|
||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata(" 60 ")) | ||
assert_equal 60, customized_resource.timeout | ||
end | ||
|
||
def test_blank_timeout_annotation_is_ignored | ||
stub_kubectl_response("create", "-f", "/tmp/foo/bar", "--dry-run", "--output=name", anything, resp: "{}") | ||
|
||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("")) | ||
customized_resource.validate_definition | ||
refute customized_resource.validation_failed?, "Blank annotation with was invalid" | ||
assert_equal 5.minutes, customized_resource.timeout | ||
end | ||
|
||
def test_validation_of_timeout_annotation | ||
expected_cmd = ["create", "-f", "/tmp/foo/bar", "--dry-run", "--output=name", anything] | ||
error_msg = "kubernetes-deploy.shopify.io/timeout-override-seconds annotation " \ | ||
"must contain digits only and must be > 0" | ||
|
||
stub_kubectl_response(*expected_cmd, resp: "{}") | ||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("sixty")) | ||
customized_resource.validate_definition | ||
assert customized_resource.validation_failed?, "Annotation with 'sixty' was valid" | ||
assert_equal error_msg, customized_resource.validation_error_msg | ||
|
||
stub_kubectl_response(*expected_cmd, resp: "{}") | ||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("-1")) | ||
customized_resource.validate_definition | ||
assert customized_resource.validation_failed?, "Annotation with '-1' was valid" | ||
assert_equal error_msg, customized_resource.validation_error_msg | ||
|
||
stub_kubectl_response(*expected_cmd, resp: "{}") | ||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("0")) | ||
customized_resource.validate_definition | ||
assert customized_resource.validation_failed?, "Annotation with '0' was valid" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case is not |
||
assert_equal error_msg, customized_resource.validation_error_msg | ||
|
||
stub_kubectl_response(*expected_cmd, resp: "{}") | ||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("10m")) | ||
customized_resource.validate_definition | ||
assert customized_resource.validation_failed?, "Annotation with '10m' was valid" | ||
assert_equal error_msg, customized_resource.validation_error_msg | ||
end | ||
|
||
def test_annotation_and_kubectl_error_messages_are_combined | ||
stub_kubectl_response( | ||
"create", "-f", "/tmp/foo/bar", "--dry-run", "--output=name", anything, | ||
resp: "{}", | ||
err: "Error from kubectl: Something else in this template was not valid", | ||
success: false | ||
) | ||
|
||
customized_resource = DummyResource.new(definition_extras: build_timeout_metadata("bad")) | ||
customized_resource.validate_definition | ||
assert customized_resource.validation_failed?, "Expected resource to be invalid" | ||
|
||
expected = <<~STRING.strip | ||
kubernetes-deploy.shopify.io/timeout-override-seconds annotation must contain digits only and must be > 0 | ||
Error from kubectl: Something else in this template was not valid | ||
STRING | ||
assert_equal expected, customized_resource.validation_error_msg | ||
end | ||
|
||
private | ||
|
||
def build_timeout_metadata(value) | ||
{ | ||
"metadata" => { | ||
"name" => "customized", | ||
"annotations" => { | ||
KubernetesDeploy::KubernetesResource::TIMEOUT_OVERRIDE_ANNOTATION => value | ||
} | ||
} | ||
} | ||
end | ||
|
||
def assert_includes_dummy_events(events, first:, second:) | ||
unless first || second | ||
assert_operator events, :empty? | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to support more ways to specify the duration like
10s
or5m
? Or perhaps just make the annotationkubernetes-deploy.shopify.io/timeout-override
and state that for the time being the only acceptable value is seconds?I am bringing this up because a timeout of
15m
(which may not be unreasonable for Redis) would be900s
which isn't as easy for a human to parse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. I'll update it to accept "s" or "m", and to assume that unqualified values are seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well if you're going to have
m
ands
, you might as well haveh
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend
ISO8601
format (to be used withActiveSupport::Duration::ISO8601Parse
) for time durations. It's what I used in my open PR on resource discovery.