-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
add -k flag in cli-runtime and kubectl to process kustomization directories #74140
Conversation
Hi @Liujingfang1. Thanks for your PR. I'm waiting for a kubernetes 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. |
/sig-cli |
Recursive bool | ||
} | ||
|
||
func (o *FilenameOptions) Validate() error { | ||
if len(o.Filenames) > 0 && len(o.Kustomize) > 0 { |
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.
it's likely many FilenameOptions/Builder users will not call this new method. should this validation go into Builder#FilenameParam
and append to Builder#errs instead?
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.
I added two functions to FilenameOptions
: Validate()
and IsSourceEmpty()
.
Validate()
is to make sure there is no -f
and -k
conflicts. This function is needed wherever FilenameOptions
is used. So it makes sense to move it into Builder#FilenameParam
.
IsSourceEmpty()
is to check both -f
and -k
are not set. This is used to make sure at least one of -f
and -k
is specified. There are subcommands such as apply
. Previously, -f
is required in those subcommands. So we need to change them to require one of -f
and -k
.
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.
validate
now is triggered inside FilenameParam
function. In addition, I renamed IsSourceEmpty()
to RequireFilenameOrKustomize
.
By this change, |
|
||
return &DeleteFlags{ | ||
FileNameFlags: &genericclioptions.FileNameFlags{Usage: usage, Filenames: &filenames, Recursive: &recursive}, | ||
FileNameFlags: &genericclioptions.FileNameFlags{Usage: usage, Filenames: &filenames, Kustomize: &kustomize, Recursive: &recursive}, |
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.
Why doesn't this use the helpers.go? We should at least document it.
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.
DeleteFlags
composes common flag struct used for commands requiring deletion logic, such as kubectl delete
and kubectl apply
. It contains FileNameFlags
, which is then converted to FileNameOptions
.
While in other subcommand options, FileNameOptions
is directly used instead of FileNameFlags
. helpers.go contains functions to add -k
and -f
for FileNameOptions
, but not for FileNameFlags
.
In the future, I think the logic of adding flags should be moved to cli-runtime
. Do we convert all current FileNameOptions
to FileNameFlags
or keep both of them? I'm not sure. @juanvallejo What do you think?
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.
Please add a comment to this code explaining why it doesn't use helpers.go
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.
How about
// Not using helpers.go it since provides function to add '-k' for FileNameOptions, but not FileNameFlags
@@ -103,11 +103,6 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cob | |||
Long: createLong, | |||
Example: createExample, | |||
Run: func(cmd *cobra.Command, args []string) { | |||
if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) { |
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 it possible to keep printing the helper message?
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.
We can keep it. The change would be like
if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
defaultRunFunc(cmd, args)
return
}
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.
SGTM. If we want to change this behavior in the future it should be a separate discussion.
@@ -223,6 +223,21 @@ __EOF__ | |||
# cleanup | |||
kubectl delete -f hack/testdata/service-revision2.yaml "${kube_flags[@]}" | |||
|
|||
## kubectl apply -k somedir | |||
kubectl apply -k hack/testdata/kustomize |
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.
lets add a test for every command that supports this if possible
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.
How about adding tests to the existing test files for apply
, diff
, create
, delete
, get
?
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.
+1
pkg/kubectl/cmd/apply/apply.go
Outdated
@@ -115,6 +115,9 @@ var ( | |||
# Apply the configuration in pod.json to a pod. | |||
kubectl apply -f ./pod.json | |||
|
|||
# Apply resources from a directory containing kustomization.yaml. |
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.
# Apply resources from a directory containing kustomization.yaml. - e.g. dir/kustomization.yaml
kubectl apply -k dir/
/ok-to-test |
/retest |
staging/src/k8s.io/cli-runtime/pkg/genericclioptions/resource/builder_test.go
Outdated
Show resolved
Hide resolved
directory: "../../../artifacts/kustomization/should-not-load.yaml", | ||
expectErr: true, | ||
errMsg: "must be a directory to be a root", | ||
}, |
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.
Add an error for specifying the kustomization file directly
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.
Added one test with
directory: filepath.Join(dir, "kustomization.yaml"),
/test pull-kubernetes-e2e-gce |
/approve |
/lgtm |
/hold Added hold for @apelisse |
/approve |
/assign @dims For staging/publishing |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: apelisse, dims, Liujingfang1, pwittrock 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 |
/hold cancel |
What type of PR is this?
/kind feature
What this PR does / why we need it:
This PR implements the 2nd Customize integration KEP
Which issue(s) this PR fixes:
Fixes kubernetes/enhancements#633
Special notes for your reviewer:
This PR adds
-k
flag intoFilenameFlags
in cli-runtime.Kubectl consumes
FilenameFlags
in different sub commands.Does this PR introduce a user-facing change?:
yes