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
Fixes 4108; remove hidden files in kustomize edit command to correctly mimic shell globbing behaviour #4170
Fixes 4108; remove hidden files in kustomize edit command to correctly mimic shell globbing behaviour #4170
Conversation
Welcome @m-Bilal! |
Hi @m-Bilal. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
563ddc4
to
5bf053a
Compare
/ok-to-test |
} | ||
var result []string | ||
for _, path := range paths { | ||
if strings.Contains(path, "/.") { |
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.
A few questions:
-
Will this still work on windows?
-
What about hidden files in the current directory, e.g. if someone does
kustomize edit add resource *
and there is a hidden file in my current directory, e.g..github
, in which case "/." doesn't appear?
Suggestion: Change this check to strings.HasPrefix(filepath.Base(path), ".")
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.
@natasha41575
I'm unable to understand one thing. If I have a directory which has the structure as follows:
dir/some-file-1
dir/some-file-2
dir/dir2/some-file3
GlobPatterns
returns only some-file-1
, some-file-2
for the pattern dir/*
. It does not go into nested directories, but the command ls dir/*
does return the file in dir2
on bash if I replicate the same directories on storage. Is the behavior of GlobPatterns
correct? Should it not reference the nested directories (and hidden nested directories)?
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.
Deep down in the implementation of GlobPatterns
, kustomize does the following:
kustomize/kyaml/filesys/fsondisk.go
Lines 89 to 92 in 2b8a393
// Glob returns the list of matching files | |
func (fsOnDisk) Glob(pattern string) ([]string, error) { | |
return filepath.Glob(pattern) | |
} |
IMO I would think that filepath.Glob
does the "correct" globbing behavior, because it is a builtin Go package. In any case, it makes sense to me for kustomize to follow whatever filepath.Glob
does rather than attempting to define its own correct behavior. WDYT?
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.
Yes that makes sense
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.
On a second thought @natasha41575, I think correcting the globbing behavior of filepath.Glob
is exactly what we're trying to do here (by explicitly removing files that filepath.Glob
returns). I guess it really comes down to what would you expect if you had the above mentioned directory structure and used dir/*
as the glob pattern (or, even just *
). If you expect the file in dir2
to be included and it isn't, it will be reported as a bug in the future, even though that is the standard behaviour of filepath.Glob
(like #4108). I don't use globbing enough to justify if that is correct or incorrect, but maybe someone who knows better (like yourself, or @KnVerey or @khrisrichardson) could decide if this should be a separate issue that needs to be fixed.
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.
On a second thought @natasha41575, I think correcting the globbing behavior of filepath.Glob is exactly what we're trying to do here (by explicitly removing files that filepath.Glob returns). I guess it really comes down to what would you expect if you had the above mentioned directory structure and used dir/* as the glob pattern (or, even just *).
That's a fair point, and worth some more investigation. I also don't use globbing enough to be able to confidently state what is and what isn't correct behavior, but FWIW some google search results (https://stackoverflow.com/questions/1690809/what-expands-to-all-files-in-current-directory-recursively and https://unix.stackexchange.com/questions/49913/recursive-glob) seem to suggest that you need to do something special (**
) for recursive globbing.
I could be mistaken, but it does not appear to me that this implementation takes into account intentional globbing of hidden files (e.g. - |
5bf053a
to
2a7ffc6
Compare
2a7ffc6
to
48cc45d
Compare
@natasha41575 is this PR complete or should recursive globbing be a part of this PR? (I tested a little, seems like recursive globbing is not currently supported) |
I've just pulled and built your branch, and with the following directory structure:
And I run It seems that this PR fixes some of the kustomize edit add commands, but not all of them go through this path. For example, kustomize/kustomize/commands/internal/util/util.go Lines 38 to 42 in 53577a5
I would suggest moving this code that you've written:
directly into kustomize/kyaml/filesys/fsondisk.go Lines 89 to 92 in 53577a5
and kustomize/kyaml/filesys/fsnode.go Lines 608 to 613 in 53577a5
My suggestion is to filter out the hidden files at the end of these functions, as a last step. You can also move your helper functions and tests into the files I would also appreciate some feedback from @monopole, @KnVerey, or @yuwenma regarding this approach. |
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.
is this PR complete or should recursive globbing be a part of this PR? (I tested a little, seems like recursive globbing is not currently supported)
Let's stick to the dotfile fix. IMO we want to exclude hidden files because it's not likely users running kustomize edit
actually want them, not just to mimic shell behavior on principle. I'm not sure at this point we want to enable recursive globbing, and if someone were to convince me, I'd still say it should go in its own PR.
paths := []string{ | ||
"dir/fa1", | ||
"dir/fa2", | ||
"dir/.fa3", |
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.
Suggestion: for completeness include top-level files (hidden & not) and files with extensions (hidden & not) to show those cases are handled correctly too.
@@ -95,6 +95,47 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { | |||
} | |||
} | |||
|
|||
func TestIsHiddenFilePath(t *testing.T) { | |||
hiddenFilePaths := []string{ |
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.
suggestion: in addition to the glob expressions, include test data that looks like full file paths, since we use this function with both types of input. It might make sense to use a table test instead of multiple loops, e.g.:
tests := map[string]struct {
paths []string
expectHidden bool
}{
"hidden globs": {
expectHidden: true,
paths: []string{
".*",
"/.*",
"dir/.*"},
},
// more categories of examples
}
for name, test := range tests {
test := test
t.Run(name, func(tt *testing.T) {
for _, path := range test.paths {
require.Equal(tt, test.expectHidden, IsHiddenFilePath(path), "path %s not correctly identified", path)
}
})
}
}
func TestExpandFileSourceWithHiddenFiles(t *testing.T) { | ||
fSys := filesys.MakeEmptyDirInMemory() | ||
_, err := fSys.Create("dir/file-1.xtn") | ||
require.NoError(t, err) |
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.
Suggestion: instead of repeating the create calls and error checking, use a loop over a list of files you want to create:
files := []string{
"dir/file-1.xtn",
"dir/file-2",
"dir/.file-3",
"dir/dir-2/.file-4",
"dir/dir-2/file-5.xtn",
"dir/dir-2/.file-6.xtn",
"root-1.xtn",
".root-2.xtn",
}
for _, file := range files {
_, err := fSys.Create(file)
require.NoError(t, err)
}
@m-Bilal: This PR has multiple commits, and the default merge method is: merge. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Sorry for updating this so late, I had gotten stuck on this issue.
now results in kustomize printing
I'm pushing this code so that it can be reviewed as I'm not sure about the things that I've listed above, I'll squash and update the commits once code has been reviewed. |
97291f0
to
04964b3
Compare
Makes sense to me.
This also seems correct to me, especially if this is what's happening on the master branch.
This is also what currently happens with the master branch, and I don't see a reason to change it in this PR.
I think it's fine if we don't touch /lgtm I will wait on letting this merge for a bit to see if there are any comments from others. Thanks for working on this! |
/lgtm |
/label tide/merge-method-squash |
Re: /approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: KnVerey, m-Bilal 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 |
Fixes #4108 which pointed out that
kustomize edit
was including hidden files in directory.APPROACH:
Once the expanded paths are available, I loop through all the paths and check if any hidden files or directories have been included (by checking for
/.
substring in the strings). If a path has that substring, it is ignored.cc: @KnVerey