Skip to content
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

fix vendor scripts shellcheck failures #79316

Merged
merged 5 commits into from
Jun 25, 2019

Conversation

BenTheElder
Copy link
Member

@BenTheElder BenTheElder commented Jun 23, 2019

What type of PR is this?

Uncomment only one /kind <> line, hit enter to put that in a new line, and remove leading whitespaces from that line:

/kind api-change
/kind bug

/kind cleanup

/kind design
/kind documentation
/kind failing-test
/kind feature
/kind flake

What this PR does / why we need it: fixes shellcheck failures in hack/update-vendor.sh, hack/pin-dependency.sh, and hack/verify-no-vendor-cycles.sh.

Which issue(s) this PR fixes:

Part of #72956

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Jun 23, 2019
@k8s-ci-robot k8s-ci-robot requested review from fejta and sttts June 23, 2019 23:12
@@ -47,6 +47,15 @@ if [ -z "${BASH_XTRACEFD:-}" ]; then
set -x
fi

# list_staging_repos outputs a sorted list of repos in staging/src/k8s.io
# each entry will just be the $repo portion of staging/src/k8s.io/$repo/...
function list_staging_repos() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correctly listing these portably and sorted in one place had a nice side effect of simplifying the script a bit.
I implemented a couple variants of this, but this is probably the most robust version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to kube::util as I found the other vendor scripts need to do the same thing a lot..

@@ -67,17 +76,17 @@ function ensure_require_replace_directives_for_all_dependencies() {
go mod edit -json | jq -r ".Replace // [] | sort | .[] | select(${replace_filter})" > "${replace_json}"

# 1a. Ensure replace directives have an explicit require directive
cat "${replace_json}" | jq -r '"-require \(.Old.Path)@\(.New.Version)"' | xargs -L 100 go mod edit -fmt
jq -r '"-require \(.Old.Path)@\(.New.Version)"' < "${replace_json}" | xargs -L 100 go mod edit -fmt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary cat usage. we can just pipe the file to input. all of the changes on .*cat.* lines are this.

echo "replace (";
cat "${go_mod_replace}";
echo ")";
} > go.mod
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we've used a command group to redirect once, which is simpler (and preferred by shellcheck).

It has a super minor performance benefit of only needing to open the file once since bash know understands the intent to redirect all of these to the same output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between { foo } and ( foo )?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ foo } is a command group but does not involve a subshell. ( foo ) is a command group in a subshell. using { foo; } or:

{
   foo
}

gets the benefits of grouping without involving a subprocess / shell.

technically there are also some slightly different rules for parsing to identify them that don't apply here.

@@ -201,9 +214,15 @@ done
kube::log::status "go.mod: sorting staging modules"
# tidy staging repos in reverse dependency order.
# the content of dependencies' go.mod files affects what `go mod tidy` chooses to record in a go.mod file.
ls staging/src/k8s.io | sort | xargs -I {} echo "k8s.io/{}" > "${TMP_DIR}/tidy_unordered.txt"
tidy_unordered="${TMP_DIR}/tidy_unordered.txt"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not strictly necessary, but we were computing this in at least 3 places, so >WET ==> DRY

# We do read from ${tidy_unordered} in the pipeline and mention it within the
# pipeline (but only ready it again) so we disable the lint to assure shellcheck
# that :this-is-fine:
# shellcheck disable=SC2094
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike needing these disable statements, but in this case it's a warning about potentially reading and writing to the same path in a loop (which wouldn't be safe). Since shellcheck has to just guess based on where the path is being referenced that it might be hitting this, we have to confirm that it is not.

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jun 23, 2019
@BenTheElder
Copy link
Member Author

/sig release
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added sig/release Categorizes an issue or PR as relevant to SIG Release. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 23, 2019
@BenTheElder BenTheElder changed the title fix hack/update-vendor.sh shellcheck failures fix vendor scripts shellcheck failures Jun 23, 2019
@@ -531,6 +531,16 @@ EOF
EOF
}

# list_staging_repos outputs a sorted list of repos in staging/src/k8s.io
# each entry will just be the $repo portion of staging/src/k8s.io/$repo/...
function kube::util::list_staging_repos() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originally I didn't add this to util, but all 3 fixed scripts have a need for this and doing it correctly / portably is tricky

@BenTheElder BenTheElder force-pushed the shellll branch 2 times, most recently from 6976ec1 to 95876cb Compare June 23, 2019 23:45
@BenTheElder
Copy link
Member Author

/retest

(
cd "${KUBE_ROOT}/staging/src/k8s.io" && \
find . -type d -mindepth 1 -maxdepth 1 | cut -c 3- | sort
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explanation:

  • ls output is not portable
  • find is more portable but gives relative paths

-> cd to the search directory in a subshell, then trim off the leading ./ from results.
search only for directories, set min/max depth so we only get results immediately below the search dir.

Copy link
Contributor

@fejta fejta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

echo "replace (";
cat "${go_mod_replace}";
echo ")";
} > go.mod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between { foo } and ( foo )?

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 24, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BenTheElder, fejta

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fejta
Copy link
Contributor

fejta commented Jun 24, 2019

/retest

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@BenTheElder
Copy link
Member Author

#78913 grrr
/retest

@k8s-ci-robot k8s-ci-robot merged commit dac0bd9 into kubernetes:master Jun 25, 2019
@BenTheElder BenTheElder deleted the shellll branch June 25, 2019 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/release Categorizes an issue or PR as relevant to SIG Release. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants