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 shellcheck failure in gce/gci/flexvolume_node_setup.sh #81061

Merged
merged 1 commit into from Sep 16, 2019
Merged

Fix shellcheck failure in gce/gci/flexvolume_node_setup.sh #81061

merged 1 commit into from Sep 16, 2019

Conversation

k-toyoda-pi
Copy link
Contributor

What type of PR is this?
/kind cleanup

What this PR does / why we need it:
Fix shellcheck failure of cluster/gce/gci/flexvolume_node_setup.sh.

Almost all of detected failures are SC2086: Double quote to prevent globbing and word splitting.
Also, the below failures are detected in the file. See changed lines.

SC2012: Use find instead of ls to better handle non-alphanumeric filenames.
SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

Which issue(s) this PR fixes:
Ref #72956

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

/sig testing
/priority backlog

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. sig/testing Categorizes an issue or PR as relevant to SIG Testing. priority/backlog Higher priority than priority/awaiting-more-evidence. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 7, 2019
@@ -175,8 +175,7 @@ echo "Restarting Kubelet..."
echo

systemctl restart kubelet.service
kubelet_wait
if [ $? -eq 0 ]; then
if kubelet_wait; then
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if [ $? -eq 0 ]; then
     ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

@k8s-ci-robot k8s-ci-robot added the sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. label Aug 7, 2019
if [ -d "$driver_dir" ]; then

filecount=$(ls -1 $driver_dir | wc -l)
if [ $filecount -gt 1 ]; then
filecount=$(find "$driver_dir" -mindepth 1 -maxdepth 1 -printf "%P\n" | grep -c -v "^\.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

filecount=$(ls -1 $driver_dir | wc -l)
            ^---------------^ SC2012: Use find instead of ls to better handle non-alphanumeric filenames.
                  ^---------^ SC2086: Double quote to prevent globbing and word splitting.

It is not same result with original code even if ls is replaced with find simply.
It uses -printf "%P\n" to output only file name, and use grep -v "^\." to except files starting with ..

After changing, shellsheck says SC2126: Consider using grep -c instead of grep|wc -l.
Therefore, it removes wc -l and adds grep -c.

Copy link
Member

Choose a reason for hiding this comment

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

darwin's version of find doesn't have -printf

the original implementation is way simpler, and since this is used for counting and not the specific paths I think it's acceptable to ignore SC2012 here

Copy link
Member

Choose a reason for hiding this comment

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

if we did actually want the values and not the count, we have an established pattern elsewhere:

  • use a subshell
    • cd to the target search directory
    • use find against .
    • trim ./ from the beginning by piping to cut or similar

To get filename, use print0 and basename.

+1 on not needing it here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comments.
I modified it a little as follows. As far as I run, the result is equivalent to the original way. But, more complicated and not readable.

filecount=$(cd "$driver_dir"; find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -n1 basename | grep -c -v "^\.")

Can we ignore SC2012?

Copy link
Member

Choose a reason for hiding this comment

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

Specifically for getting a count, that's probably fine.
But generally speaking SC2012 exists because ls output is NOT portable or specified. We should prefer find.

Longer term we should move elaborate logic out of bash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. I updated the commit using -print0 and basename. Could you check it?

echo "ERROR: Expected 1 file in the FlexVolume directory but found $filecount."
exit 1
fi

driver_file=$( ls $driver_dir | head -n 1 )
driver_file=$(find "$driver_dir" -mindepth 1 -maxdepth 1 -printf "%P\n" | grep -v "^\." | head -n 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

driver_file=$( ls $driver_dir | head -n 1 )
               ^------------^ SC2012: Use find instead of ls to better handle non-alphanumeric filenames.
                  ^---------^ SC2086: Double quote to prevent globbing and word splitting.

Same with line 105.

Copy link
Member

Choose a reason for hiding this comment

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

same here, I think it's acceptable to ignore SC2012

Copy link
Member

Choose a reason for hiding this comment

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

instead of printf, use normal print, and use basename as the last step in the chain

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have done.

@k-toyoda-pi
Copy link
Contributor Author

/test pull-kubernetes-integration

Copy link
Member

@spiffxp spiffxp left a comment

Choose a reason for hiding this comment

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

thanks for the explanations

if [ -d "$driver_dir" ]; then

filecount=$(ls -1 $driver_dir | wc -l)
if [ $filecount -gt 1 ]; then
filecount=$(find "$driver_dir" -mindepth 1 -maxdepth 1 -printf "%P\n" | grep -c -v "^\.")
Copy link
Member

Choose a reason for hiding this comment

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

darwin's version of find doesn't have -printf

the original implementation is way simpler, and since this is used for counting and not the specific paths I think it's acceptable to ignore SC2012 here

echo "ERROR: Expected 1 file in the FlexVolume directory but found $filecount."
exit 1
fi

driver_file=$( ls $driver_dir | head -n 1 )
driver_file=$(find "$driver_dir" -mindepth 1 -maxdepth 1 -printf "%P\n" | grep -v "^\." | head -n 1)
Copy link
Member

Choose a reason for hiding this comment

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

same here, I think it's acceptable to ignore SC2012

if [ -d "$driver_dir" ]; then

filecount=$(ls -1 $driver_dir | wc -l)
if [ $filecount -gt 1 ]; then
filecount=$(cd "$driver_dir"; find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -n1 basename | grep -c -v "^\.")
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't need to do grep after the basename?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line counts files under $driver_dir. Therefore, I think need grep -c to count.

Copy link
Member

Choose a reason for hiding this comment

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

we can count with something like wc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered using wc -l, but shellcheck said SC2126.

... xargs -0 -n1 basename | grep -v "^\." | wc -l)
                            ^-----------^ SC2126: Consider using grep -c instead of grep|wc -l.

Copy link
Member

Choose a reason for hiding this comment

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

like I said up top for both of these now-find-based-implementations, I feel like the original ls-based implementations are fine; it's not clear to me what value we're getting here by adding all of this extra complexity to work around corner cases that haven't crept up thus far

Copy link
Member

Choose a reason for hiding this comment

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

@k-toyoda-pi er right, see discussion below for if we even need to ignore files starting with . (I don't think so, however we probably should ignore directories)
@spiffxp discussing below. #81061 (comment)

echo "ERROR: Expected 1 file in the FlexVolume directory but found $filecount."
exit 1
fi

driver_file=$( ls $driver_dir | head -n 1 )
driver_file=$(cd "$driver_dir"; find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -n1 basename | grep -v "^\." | head -n 1)
Copy link
Member

Choose a reason for hiding this comment

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

we should prefer cut to grep -v since we are removing a known fixed length prefix from all entries.

kubernetes/hack/lib/util.sh

Lines 541 to 544 in 8c6c94b

function kube::util::list_staging_repos() {
(
cd "${KUBE_ROOT}/staging/src/k8s.io" && \
find . -mindepth 1 -maxdepth 1 -type d | cut -c 3- | sort

Copy link
Contributor Author

Choose a reason for hiding this comment

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

find reveals files starting with .(ex. .aaa). But ls without -a does not. So, grep -v in this line excepts the files.

Copy link
Member

Choose a reason for hiding this comment

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

I see now, we already pipe through basename which drops the ./foo/, but do we actually need to ignore these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If find outputs the following result, I think grep -v is needed to exclude .ddd.

$ cd find_test; find . -mindepth 1 -print0 | xargs -0 -n1 basename
aaa
.ddd
bbb
ccc

Should we not consider these case?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, I guess it depends on the data. Let's handle it for now but maybe leave a TODO about it.

also cc @msau42 ... does it make sense to ignore drivers starting with . ?

Copy link
Member

Choose a reason for hiding this comment

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

directories that start with . are reserved for the driver installation mechanism (to make sure driver installs are atomic). See here for details

directories sure, files though?

Copy link
Member

Choose a reason for hiding this comment

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

like I said up top for both of these now-find-based-implementations, I feel like the original ls-based implementations are fine; it's not clear to me what value we're getting here by adding all of this extra complexity to work around corner cases that haven't crept up thus far

the ls implementation was already not-correct I suspect. in neither of these are we currently only finding files, despite the variable name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for many comments.
I've checked flexvolume behavior.

When I placed kubelet-plugins/volume/exec/test~.aaa/.aaa, which is dummy driver, .aaa can work.
From the result, I think it should not except files begin with ".".

I will modify as the below.

  1. (line:105) filecount=$(cd "$driver_dir"; find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -n1 basename | wc -l)
  2. (line:111) driver_file=$(cd "$driver_dir"; find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -n1 basename | head -n 1)

Copy link
Member

Choose a reason for hiding this comment

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

SGTM, thanks for being thorough!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'm done.

@BenTheElder
Copy link
Member

/retest

@BenTheElder
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 4, 2019
@k-toyoda-pi
Copy link
Contributor Author

/test pull-kubernetes-integration

@BenTheElder
Copy link
Member

/assign @filbranden
(feel free to unassign and or update OWNERS 😉)

@filbranden
Copy link
Contributor

/unassign

Good point @BenTheElder ! Will send a PR to update OWNERS...

Thanks!
Filipe

@k-toyoda-pi
Copy link
Contributor Author

/assing @yguo0905
for approval

@BenTheElder
Copy link
Member

/assign @yguo0905
(corrected the command)

@yguo0905
Copy link
Contributor

cc @verult
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BenTheElder, k-toyoda-pi, yguo0905

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 16, 2019
@k8s-ci-robot k8s-ci-robot merged commit 1bebaea into kubernetes:master Sep 16, 2019
@k8s-ci-robot k8s-ci-robot added this to the v1.17 milestone Sep 16, 2019
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/backlog Higher priority than priority/awaiting-more-evidence. release-note-none Denotes a PR that doesn't merit a release note. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants