mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-05 14:42:05 +09:00
test(hack): skip request-timeout calls that name a kubeconfig or context (#5406)
## What this PR does `hack/check-hook-request-timeout.bats` exempts a file only when it names `KUBECONFIG`. A hook script under `files/` that gets `KUBECONFIG` from its template's env has no reason to name it, so a call that goes through that kubeconfig with `--context` is flagged as an in-cluster call. That is what turned `capi-providers-core/files/upgrade-providers.sh` red on main; #5398 fixed that script by naming `KUBECONFIG`, and this keeps the next script written the same way from hitting it again. The check now skips a line that passes a non-empty `--context` or `--kubeconfig`. client-go serves such a call from the loaded kubeconfig, and without one `--context` fails with "context was not found" instead of falling back to localhost:8080. An empty value (`--context ""`, `--context=`, `--kubeconfig=`) does fall back, so it is still reported, and so is a bare call in the same file. The header notes the limits: a variable that is empty at runtime is not visible to the check, and the match is per line. Tested with `bats hack/check-hook-request-timeout.bats`: 11 of 11 pass, the new cases fail without the change, and each part of the filter is pinned by a mutation. The empty-value fallback was reproduced with kubectl v1.37.2. ### Screenshots <!-- REQUIRED for UI changes: attach screenshots or screen recordings demonstrating the visual impact of your changes. PRs with UI changes without screenshots will not be merged. --> ### Downstream repositories <!-- Cozystack is upstream for repositories that are NOT kept in sync with it automatically, and nothing in CI compares the two sides, so a change here can break them silently. Which change forces what, and which file to touch, is the trigger map in docs/agents/contributing.md under "Downstream Repositories". Walk it before you tick anything. IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM TO FILL IN: 1. Walk the trigger map against the actual diff of this PR, file by file. Do not decide from the PR title or from your memory of what you changed. 2. For every repository you tick, open the follow-up PR there and link it on the same line. A ticked box with no link claims work that does not exist, which is worse than an empty box. Search that repository for an open PR or issue covering it first, and link that instead of filing a duplicate. 3. When the follow-up is out of scope, or needs a decision that is not yours to make, open an issue in that repository instead, link that, and say so in the PR body. Do not push a speculative PR into another repository just to fill a line here. 4. Do not tick "No downstream repository is affected" to make the checklist go away. If you are unsure, leave every box empty and say so in the PR body, so a human decides. --> - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note <!-- Write a release note: - Explain what has changed internally and for users. - Start with the same `type(scope):` prefix as in the PR title - Follow the guidelines at https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md. --> ```release-note NONE ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Timeout checks now exclude matching lines that specify a non-empty context or kubeconfig value. Empty option values and bare calls on separate lines are still reported; files naming `KUBECONFIG` remain excluded. * **Documentation** * Clarified how context and kubeconfig options affect timeout reporting. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
1 file changed
+38
-4
@@ -19,8 +19,16 @@
|
||||
# an explicit kubeconfig, so the in-cluster branch is not what serves it and the
|
||||
# flag is harmless there. That is a per-file judgement, not per-command, which
|
||||
# is the coarse part of this check and is why it is written down rather than
|
||||
# inferred. Vendored charts under packages/*/*/charts are upstream and out of
|
||||
# scope.
|
||||
# inferred. A script under files/ that gets KUBECONFIG from its template's env
|
||||
# has no reason to name it, so a line passing a non-empty --context or
|
||||
# --kubeconfig is skipped instead: client-go then serves the call from that
|
||||
# kubeconfig or fails with "context was not found", never from localhost:8080.
|
||||
# A literally empty value does fall back, so it is still reported; a variable
|
||||
# that expands to nothing at runtime is not visible here, so guard it with
|
||||
# ${VAR:?} as the shipped hooks do. The match is per
|
||||
# line, so a flag in a trailing comment or in another command on the same line
|
||||
# also exempts it. Vendored charts under packages/*/*/charts are upstream and
|
||||
# out of scope.
|
||||
#
|
||||
# Bounding the call is still available: --timeout bounds a watch, and
|
||||
# activeDeadlineSeconds bounds the whole Job, which is what
|
||||
@@ -55,7 +63,8 @@ request_timeout_candidates() {
|
||||
}
|
||||
|
||||
# request_timeout_offenders FILE...: the files, with matching lines, that pass
|
||||
# a non-zero --request-timeout without naming KUBECONFIG.
|
||||
# a non-zero --request-timeout without naming KUBECONFIG or a non-empty
|
||||
# --context or --kubeconfig.
|
||||
request_timeout_offenders() {
|
||||
local f hits
|
||||
for f in "$@"; do
|
||||
@@ -63,7 +72,7 @@ request_timeout_offenders() {
|
||||
if grep -q 'KUBECONFIG' "$f"; then
|
||||
continue
|
||||
fi
|
||||
hits="$(grep -nE -- "$REQUEST_TIMEOUT_RE" "$f" || true)"
|
||||
hits="$(grep -nE -- "$REQUEST_TIMEOUT_RE" "$f" | grep -vE -- "--(context|kubeconfig)(=|[[:space:]]+)[\"']?[^\"'[:space:]]" || true)"
|
||||
if [ -n "$hits" ]; then
|
||||
printf '%s\n%s\n' "$f" "$hits"
|
||||
fi
|
||||
@@ -146,3 +155,28 @@ write_tree_file() {
|
||||
write_tree_file packages/x/y/files/manifest.yaml 'command: ["kubectl", "get", "pods", "--request-timeout=30s"]'
|
||||
[ -z "$(scan_tree)" ]
|
||||
}
|
||||
|
||||
@test "a call that names a kubeconfig context or file is left alone" {
|
||||
write_tree_file packages/x/y/files/hook.sh "$(printf '%s\n' \
|
||||
'timeout "$remaining" kubectl --context "$KUBE_CONTEXT" --namespace "$NAMESPACE" --request-timeout=10s "$@"' \
|
||||
'kubectl --kubeconfig=/etc/kubeconfig get pods --request-timeout 30s')"
|
||||
[ -z "$(scan_tree)" ]
|
||||
}
|
||||
|
||||
@test "a bare call is still caught next to a call that names a context" {
|
||||
write_tree_file packages/x/y/files/hook.sh "$(printf '%s\n' \
|
||||
'kubectl --context "$KUBE_CONTEXT" get pods --request-timeout=10s' \
|
||||
'kubectl get pods --request-timeout=10s')"
|
||||
run scan_tree
|
||||
[ "${#lines[@]}" -eq 2 ]
|
||||
[ "${lines[1]}" = '2:kubectl get pods --request-timeout=10s' ]
|
||||
}
|
||||
|
||||
@test "an empty context or kubeconfig value is still caught" {
|
||||
write_tree_file packages/x/y/files/hook.sh "$(printf '%s\n' \
|
||||
'kubectl --context "" get pods --request-timeout=10s' \
|
||||
'kubectl --kubeconfig= get pods --request-timeout=10s' \
|
||||
'kubectl --context= get pods --request-timeout=10s')"
|
||||
run scan_tree
|
||||
[ "${#lines[@]}" -eq 4 ]
|
||||
}
|
||||
Reference in new issue
Block a user