-
Notifications
You must be signed in to change notification settings - Fork 157
[shiftstack] Bound soft-transient oc exec retries during pod polling #4192
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
Open
tusharjadhav3302
wants to merge
1
commit into
openstack-k8s-operators:main
Choose a base branch
from
tusharjadhav3302:fix/shiftstack-exec-soft-transient-budget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−21
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| #!/bin/bash | ||
| # Copyright Red Hat, Inc. | ||
| # All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # Poll for a detached command running in a shiftstackclient pod. | ||
| # | ||
| # Soft-transient oc exec errors (connection refused / i/o timeout) use a | ||
| # consecutive failure budget so brief API VIP blips do not fail the job, while | ||
| # a dead API does not burn the full poll window. Hard-transient patterns retry | ||
| # for the full max-retries window. | ||
| # | ||
| # Required environment: | ||
| # CIFMW_SHIFTSTACK_POLL_NAMESPACE | ||
| # CIFMW_SHIFTSTACK_POLL_POD | ||
| # CIFMW_SHIFTSTACK_POLL_MAX_RETRIES | ||
| # CIFMW_SHIFTSTACK_POLL_DELAY | ||
| # CIFMW_SHIFTSTACK_POLL_SOFT_MAX | ||
| # CIFMW_SHIFTSTACK_POLL_HARD_RE (grep -E pattern, may be empty) | ||
| # CIFMW_SHIFTSTACK_POLL_SOFT_RE (grep -E pattern, may be empty) | ||
| # | ||
| # Exit codes: | ||
| # 0 - background command finished (stdout holds /tmp/cifmw_cmd_rc) | ||
| # 2 - background process gone without writing a marker | ||
| # 3 - soft-transient budget exhausted | ||
| # 10 - still running after max retries | ||
| # other - non-retryable oc exec failure | ||
| # | ||
| # On completion, prints CIFMW_SHIFTSTACK_POLL_ATTEMPTS=<n> on stderr. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| : "${CIFMW_SHIFTSTACK_POLL_NAMESPACE:?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_POD:?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_MAX_RETRIES:?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_DELAY:?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_SOFT_MAX:?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_HARD_RE?}" | ||
| : "${CIFMW_SHIFTSTACK_POLL_SOFT_RE?}" | ||
|
|
||
| soft=0 | ||
| attempts=0 | ||
| max_retries="${CIFMW_SHIFTSTACK_POLL_MAX_RETRIES}" | ||
| delay="${CIFMW_SHIFTSTACK_POLL_DELAY}" | ||
| soft_max="${CIFMW_SHIFTSTACK_POLL_SOFT_MAX}" | ||
| hard_re="${CIFMW_SHIFTSTACK_POLL_HARD_RE}" | ||
| soft_re="${CIFMW_SHIFTSTACK_POLL_SOFT_RE}" | ||
| ns="${CIFMW_SHIFTSTACK_POLL_NAMESPACE}" | ||
| pod="${CIFMW_SHIFTSTACK_POLL_POD}" | ||
| poll_cmd='if cat /tmp/cifmw_cmd_rc 2>/dev/null; then exit 0; elif [ -f /tmp/cifmw_cmd_pid ] && kill -0 $(cat /tmp/cifmw_cmd_pid) 2>/dev/null; then exit 10; else exit 2; fi' | ||
|
|
||
| emit_attempts() { | ||
| echo "CIFMW_SHIFTSTACK_POLL_ATTEMPTS=${attempts}" >&2 | ||
| } | ||
|
|
||
| while [ "${attempts}" -lt "${max_retries}" ]; do | ||
| attempts=$((attempts + 1)) | ||
| stderr_file="$(mktemp)" | ||
| set +e | ||
| stdout="$(oc exec -n "${ns}" "${pod}" -- bash -c "${poll_cmd}" 2>"${stderr_file}")" | ||
| rc=$? | ||
| set -e | ||
| stderr="$(cat "${stderr_file}" 2>/dev/null || true)" | ||
| rm -f "${stderr_file}" | ||
|
|
||
| if [ "${rc}" -eq 0 ] || [ "${rc}" -eq 2 ]; then | ||
| printf '%s\n' "${stdout}" | ||
| printf '%s\n' "${stderr}" >&2 | ||
| emit_attempts | ||
| exit "${rc}" | ||
| fi | ||
|
|
||
| if [ "${rc}" -eq 10 ]; then | ||
| soft=0 | ||
| sleep "${delay}" | ||
| continue | ||
| fi | ||
|
|
||
| # oc exec itself failed (typically rc=1). Classify stderr. | ||
| if [ -n "${hard_re}" ] && printf '%s\n' "${stderr}" | grep -Eiq -- "${hard_re}"; then | ||
| soft=0 | ||
| sleep "${delay}" | ||
| continue | ||
| fi | ||
|
|
||
| if [ -n "${soft_re}" ] && printf '%s\n' "${stderr}" | grep -Eiq -- "${soft_re}"; then | ||
| soft=$((soft + 1)) | ||
| if [ "${soft}" -ge "${soft_max}" ]; then | ||
| printf '%s\n' "${stderr}" >&2 | ||
| emit_attempts | ||
| exit 3 | ||
| fi | ||
| sleep "${delay}" | ||
| continue | ||
| fi | ||
|
|
||
| printf '%s\n' "${stderr}" >&2 | ||
| emit_attempts | ||
| exit "${rc}" | ||
| done | ||
|
|
||
| emit_attempts | ||
| exit 10 |
This file contains hidden or 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.
Just one slight concern over using
ansible.builtin.commandandrole_pathhere.Have you tested this and does this work as expected? Situations like this can be a little tricky, as
role_pathrefers to the role path on the controlling host, but the command executes on the remote host, so the file might not be exactly where you expect.Maybe
ansible.builtin.scriptwould be safer?