-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add/extend some K8s-related utilities. #26
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a83e45f
Formatting fixes to util.platform_util.
aznashwan c459537
Add retry-related kwargs to k8s_util.wait_for_* functions.
aznashwan a215b17
Add util.k8s_utils.wait_for_stateful_set().
aznashwan a2f6e7b
Fix default delays in k8s_util functions.
aznashwan bb73a1b
Describe relevant resources on wait_for_*() errors.
aznashwan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# See LICENSE file for licensing details | ||
# | ||
|
||
import functools | ||
import itertools | ||
import json | ||
import logging | ||
|
@@ -32,6 +33,27 @@ def purge_k8s_snap(instance: harness.Instance): | |
instance.exec(["sudo", "snap", "remove", "k8s", "--purge"]) | ||
|
||
|
||
def describe_resources_on_error(resource_type: str): | ||
def _decorator(fun): | ||
@functools.wraps(fun) | ||
def _inner(instance: harness.Instance, *args, **kwargs): | ||
try: | ||
return fun(instance, *args, **kwargs) | ||
except Exception: | ||
proc = instance.exec( | ||
["k8s", "kubectl", "describe", resource_type], capture_output=True | ||
) | ||
LOG.info( | ||
f"### All current '{resource_type}' definitions: " | ||
f"{proc.stdout.decode()}" | ||
) | ||
raise | ||
|
||
return _inner | ||
|
||
return _decorator | ||
|
||
|
||
# Validates that the K8s node is in Ready state. | ||
def wait_until_k8s_ready( | ||
control_node: harness.Instance, instances: List[harness.Instance] | ||
|
@@ -129,9 +151,11 @@ def wait_for_resource( | |
name: str, | ||
namespace: str = constants.K8S_NS_DEFAULT, | ||
condition: str = constants.K8S_CONDITION_AVAILABLE, | ||
retry_times: int = 5, | ||
retry_delay_s: int = 60, | ||
): | ||
"""Waits for the given resource to reach the given condition.""" | ||
exec_util.stubbornly(retries=5, delay_s=1).on(instance).exec( | ||
exec_util.stubbornly(retries=retry_times, delay_s=retry_delay_s).on(instance).exec( | ||
[ | ||
"k8s", | ||
"kubectl", | ||
|
@@ -142,26 +166,44 @@ def wait_for_resource( | |
resource_type, | ||
name, | ||
"--timeout", | ||
"60s", | ||
"1s", | ||
] | ||
) | ||
|
||
|
||
@describe_resources_on_error("pods") | ||
@describe_resources_on_error("deployment") | ||
def wait_for_deployment( | ||
instance: harness.Instance, | ||
name: str, | ||
namespace: str = constants.K8S_NS_DEFAULT, | ||
condition: str = constants.K8S_CONDITION_AVAILABLE, | ||
retry_times: int = 5, | ||
retry_delay_s: int = 60, | ||
): | ||
"""Waits for the given deployment to reach the given condition.""" | ||
wait_for_resource(instance, constants.K8S_DEPLOYMENT, name, namespace, condition) | ||
wait_for_resource( | ||
instance, | ||
constants.K8S_DEPLOYMENT, | ||
name, | ||
namespace=namespace, | ||
condition=condition, | ||
retry_times=retry_times, | ||
retry_delay_s=retry_delay_s, | ||
) | ||
|
||
|
||
@describe_resources_on_error("pods") | ||
@describe_resources_on_error("daemonsets") | ||
def wait_for_daemonset( | ||
instance: harness.Instance, name: str, namespace: str = constants.K8S_NS_DEFAULT | ||
instance: harness.Instance, | ||
name: str, | ||
namespace: str = constants.K8S_NS_DEFAULT, | ||
retry_times: int = 5, | ||
retry_delay_s: int = 60, | ||
): | ||
"""Waits for the given daemonset to become available.""" | ||
exec_util.stubbornly(retries=5, delay_s=1).on(instance).exec( | ||
exec_util.stubbornly(retries=retry_times, delay_s=retry_delay_s).on(instance).exec( | ||
[ | ||
"k8s", | ||
"kubectl", | ||
|
@@ -172,7 +214,33 @@ def wait_for_daemonset( | |
constants.K8S_DAEMONSET, | ||
name, | ||
"--timeout", | ||
"60s", | ||
"1s", | ||
] | ||
) | ||
|
||
|
||
@describe_resources_on_error("pods") | ||
@describe_resources_on_error("statefulsets") | ||
def wait_for_statefulset( | ||
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. we should probably just make this a |
||
instance: harness.Instance, | ||
name: str, | ||
namespace: str = constants.K8S_NS_DEFAULT, | ||
retry_times: int = 5, | ||
retry_delay_s: int = 60, | ||
): | ||
"""Waits for the given daemonset to become available.""" | ||
exec_util.stubbornly(retries=retry_times, delay_s=retry_delay_s).on(instance).exec( | ||
[ | ||
"k8s", | ||
"kubectl", | ||
"rollout", | ||
"status", | ||
"--namespace", | ||
namespace, | ||
constants.K8S_STATEFULSET, | ||
name, | ||
"--timeout", | ||
"1s", | ||
] | ||
) | ||
|
||
|
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
Oops, something went wrong.
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.
Actually, this should be
1min
I guess.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 have “shifted” the wait from the ‘kubectl’ call to the retry function itself via the default values for the kwarga, with the total wait time still amounting to more or less 1minute.
The ‘--timeout’ kwarg to ‘kubectl’ is still required though, otherwise we risk it waiting indefinitely…
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.
Actually it just hit me that the original was about 5 minutes, updated the default kwargs to be about 5minutes again.