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

kubectl create: enforce namespace during dry run #84343

Closed
wants to merge 16 commits into from
Closed

kubectl create: enforce namespace during dry run #84343

wants to merge 16 commits into from

Conversation

busser
Copy link

@busser busser commented Oct 25, 2019

What type of PR is this?

/kind bug

What this PR does / why we need it:

When creating a role with kubectl create role, any specified namespace is ignored if the command is a dry run. This is unexpected behavior.

For example, the following command yields the following output:

kubectl create role foo --verb=get --resource=pod --namespace=bar --output=yaml --dry-run
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  creationTimestamp: null
  name: foo
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get

This pull-request fixes this issue. The command above now provides the expected output:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  creationTimestamp: null
  name: foo
  namespace: bar
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#705

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

The output of a dry run of creating a resource with kubectl now always takes into account the namespace specified in the command.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot
Copy link
Contributor

Welcome @busser!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. 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 Oct 25, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @busser. 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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. kind/bug Categorizes issue or PR as related to a bug. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 25, 2019
@soltysh
Copy link
Contributor

soltysh commented Oct 25, 2019

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 25, 2019
Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

Although kubernetes/kubectl#705 uses create role as an example this problem applies to majority (if not all) of create commands. Can you please check all of them? Also don't forget to add tests ensuring that when run with --dry-run the namespace is properly set.

@busser
Copy link
Author

busser commented Oct 25, 2019

/hold

Putting this PR on hold while I fix the other kubectl create commands and add tests.

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 25, 2019
@busser busser changed the title kubectl create role: add namespace during dry run kubectl create: enforce namespace during dry run Oct 30, 2019
@busser busser requested a review from soltysh October 30, 2019 19:32
@soltysh
Copy link
Contributor

soltysh commented Nov 7, 2019

/priority backlog

@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Nov 7, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: busser
To complete the pull request process, please assign soltysh
You can assign the PR to them by writing /assign @soltysh in a comment when ready.

The full list of commands accepted by this bot can be found 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

When the TestConfigFlags.WithNamespace method is called, the
ClientConfig is wrapped inside a namespacedClientConfig.
Previously, calling the namespaceClientConfig.Namespace method would
return that the namespace should not be enforced. This made testing
namespace enforcement complicated. From now on, the method will
return that the namespace should be enforced.
A change in the cmdtesting.TestFactory broke the TestSetLocalNamespace
unit test. Calling the TestFactory.WithNamespace method now enforces
the "test" namespace, which conflicts with how this test works.

This unit tests works by loading a resource from a file with its
namespace set to "existing-ns", which was overwritten by "test".

This fix corrects this issue.
The create.RunCreateRole function now enforces namespace during dry runs.
The TestCreateRole unit test has been updated to reflect that.
TestCreateClusterRole now runs as multiple subtests and uses the diff package
to print differences between the expected and actual cluster roles.
If the namespace is to be enforced, make sure to always enforce it, even
during dry runs.
TestCreateCronJob now uses the diff package to print differences
between the expected and actual cron jobs.
The tests were renamed because there could not be a TestComplete
function for both roles and cron jobs, for example. Same for
TestValidate.
If the namespace is to be enforced, make sure to always enforce it, even
during dry runs.
The TestCreateRoleValidation test was replaced by TestValidateCreateRole
to match how other `kubectl create` commands are tested.
@busser
Copy link
Author

busser commented Nov 14, 2019

/test pull-kubernetes-node-e2e-containerd

1 similar comment
@busser
Copy link
Author

busser commented Nov 15, 2019

/test pull-kubernetes-node-e2e-containerd

@busser
Copy link
Author

busser commented Nov 15, 2019

The pull-kubernetes-node-e2e-containerd test keeps failing. I am getting the same error messages as in issue #85040.

@busser
Copy link
Author

busser commented Nov 15, 2019

/unhold

All kubectl create sub-commands have been checked and, when needed, fixed.

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 15, 2019
@busser
Copy link
Author

busser commented Nov 18, 2019

/test pull-kubernetes-node-e2e-containerd

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

Also please squash your changes into a single commit.

@@ -96,7 +96,7 @@ type namespacedClientConfig struct {
}

func (c *namespacedClientConfig) Namespace() (string, bool, error) {
return c.namespace, false, nil
return c.namespace, true, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we agreed this to be:

return c.namespace, len(c.namespace) > 0, nil

didn't we?

Copy link
Author

Choose a reason for hiding this comment

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

You're right.

return nil
}

func (o *CreateCronJobOptions) Run() error {
var cronjob *batchv1beta1.CronJob
cronjob = o.createCronJob()
if o.EnforceNamespace {
cronjob.Namespace = o.Namespace
cronjob.Spec.JobTemplate.Namespace = o.Namespace
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed, namespace for the created job will be always inherited from the cronjob itself.

@@ -149,18 +150,28 @@ func (o *CreateCronJobOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, a
}

func (o *CreateCronJobOptions) Validate() error {
if o.Name == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

len(o.Name) == 0 is preferred, see below.

}
}

func TestRunCreateCronJob(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is mostly duplicating TestCreateCronJob, if you look at what Run in create_cronjob.go does it's only creating the CJ itself and I don't think we need that much of testing of the printing and all the rest of the machinery. This test has more setup code than actual code, and one of the reasons we've went with *Options struct was to get rid of those additional setup steps. The high level testing should happen in test-cmd, instead.

}
}

func TestCompleteCreateCronJob(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The same argument as below, I don't think we need to test every single line of create commands, Complete method is one of those which can be easily tested in test-cmd.

if test.resourceNames != "" {
cmd.Flags().Set("resource-name", test.resourceNames)
}
cmd.Run(cmd, []string{clusterRoleName})
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer we move with the testing towards the pattern from create cronjob, where you directly inject values into *Options struct and focus testing on the "raw meat", which in here is RunCreateRole.

Copy link
Contributor

Choose a reason for hiding this comment

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

The less of the test factory magic, the easier this testing is for newcomers to understand it.

@@ -22,7 +22,7 @@ import (

rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Copy link
Contributor

Choose a reason for hiding this comment

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

metav1 is the proffered alias for this, since you've touched this.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 7, 2020
@k8s-ci-robot
Copy link
Contributor

@busser: PR needs rebase.

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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 7, 2020
@zhouya0
Copy link
Contributor

zhouya0 commented Apr 26, 2020

@busser Seems no updates from this PR? Could I take this bug and send my PR?

@busser
Copy link
Author

busser commented May 23, 2020

@busser Seems no updates from this PR? Could I take this bug and send my PR?

Sorry, I haven't taken the time to finish this. Feel free to submit your solution :)

@zhouya0
Copy link
Contributor

zhouya0 commented May 24, 2020

@busser Seems no updates from this PR? Could I take this bug and send my PR?

Sorry, I haven't taken the time to finish this. Feel free to submit your solution :)

Yes, it's already been fixed and we can close this :).
/close

@k8s-ci-robot
Copy link
Contributor

@zhouya0: Closed this PR.

In response to this:

@busser Seems no updates from this PR? Could I take this bug and send my PR?

Sorry, I haven't taken the time to finish this. Feel free to submit your solution :)

Yes, it's already been fixed and we can close this :).
/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dependency Issues or PRs related to dependency changes area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"kubectl create role" ignores --namespace flag
5 participants