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

odo create namespace #5724

Merged
merged 8 commits into from
May 12, 2022
Merged

Conversation

valaparthvi
Copy link
Contributor

@valaparthvi valaparthvi commented May 9, 2022

What type of PR is this:
/kind feature

What does this PR do / why we need it:

Which issue(s) this PR fixes:

Fixes part of #5525

PR acceptance criteria:

  • Unit test

  • Integration test

  • Documentation

How to test changes / Special notes to the reviewer:

  1. odo create namespace my-namespace
  2. odo create project my-project
  3. odo create
  4. odo create namespace --help

Note: The main logic of this PR comes from https://github.com/valaparthvi/odo/blob/main/pkg/odo/cli/project/create.go.

@openshift-ci openshift-ci bot added the kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation label May 9, 2022
@netlify
Copy link

netlify bot commented May 9, 2022

Deploy Preview for odo-docusaurus-preview ready!

Name Link
🔨 Latest commit abd914b
🔍 Latest deploy log https://app.netlify.com/sites/odo-docusaurus-preview/deploys/627d089cbe68e600088fb47c
😎 Deploy Preview https://deploy-preview-5724--odo-docusaurus-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@openshift-ci openshift-ci bot requested review from feloy and rnapoles-rh May 9, 2022 13:43
@odo-robot
Copy link

odo-robot bot commented May 9, 2022

OpenShift Tests on commit e61ce4d finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented May 9, 2022

Unit Tests on commit e61ce4d finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented May 9, 2022

Windows Tests (OCP) on commit e61ce4d finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented May 9, 2022

Kubernetes Tests on commit e61ce4d finished successfully.
View logs: TXT HTML

@valaparthvi valaparthvi force-pushed the create-namespace branch 2 times, most recently from dfabc11 to b77c98c Compare May 9, 2022 13:57
@odo-robot
Copy link

odo-robot bot commented May 9, 2022

Validate Tests on commit e61ce4d finished successfully.
View logs: TXT HTML

sidebar_position: 3
---

`odo create namespace` lets you create a namespace/project on your cluster.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please explain that it will create a Project resource when the resource type exists in the cluster (generally in OpenShift clusters), or a Namespace resource otherwise

✓ New namespace created and now using namespace: mynamespace
```

Optionally, you can also use `project` as an alias to `namespace`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please explain that using Project or Namespace won't change the resource created in the cluster: only the existence of the Project resource type in the cluster will.

})
It("should fail when an existent namespace is created again", func() {
helper.Cmd("odo", "create", "project", commonVar.Project).ShouldFail()
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add specific tests for OpenShift/Kubernetes to check the created resource? A project should have been created on OpenShift, but not on Kubernetes.

helper.CommonAfterEach(commonVar)
})
It("should successfully create the namespace", func() {
helper.Cmd("odo", "create", "namespace", "my-namespace").ShouldPass()
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need to delete these namespaces after the tests, or they will stay in the cluster and make future tests fail

@openshift-ci openshift-ci bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. Required by Prow. label May 10, 2022
@openshift-ci openshift-ci bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. Required by Prow. label May 10, 2022
AfterEach(func() {
commonVar.CliRunner.DeleteNamespaceProject(project)
})
It("should successfully create the project", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see this test failing on Windows, but I'm not sure why

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was this test passing before? I don't remember.

Copy link
Contributor

@feloy feloy May 10, 2022

Choose a reason for hiding this comment

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

I think the tests are failing because you are not using the wait flag. I don't know why (*), the Project resource is not created in an atomic way, and you have to wait for it to be created.

I can see that the specs don't talk about the wait flag, probably you could force to wait everytime we create a project

(*) after some code reading, the creation of a project is done by first creating a projectrequest, and some operator will create a project later, asynchronously

AfterEach(func() {
commonVar.CliRunner.DeleteNamespaceProject(project)
})
It("should successfully create the project", func() {
Copy link
Contributor

@feloy feloy May 10, 2022

Choose a reason for hiding this comment

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

I think the tests are failing because you are not using the wait flag. I don't know why (*), the Project resource is not created in an atomic way, and you have to wait for it to be created.

I can see that the specs don't talk about the wait flag, probably you could force to wait everytime we create a project

(*) after some code reading, the creation of a project is done by first creating a projectrequest, and some operator will create a project later, asynchronously

@valaparthvi
Copy link
Contributor Author

/retest-required

})
It("should successfully create the project", func() {
helper.Cmd("odo", "create", "project", project, "--wait").ShouldPass()
Expect(commonVar.CliRunner.GetNamespaceProject()).To(ContainSubstring(project))
Copy link
Contributor

Choose a reason for hiding this comment

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

You could try to run the command kubectl get project/namespace protjetname instead of getting the full list of projects/namespaces

@valaparthvi
Copy link
Contributor Author

/retest-required

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label May 10, 2022
Copy link
Member

@rm3l rm3l left a comment

Choose a reason for hiding this comment

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

Another question after playing with the command: what is the expected behavior of the --kubeconfig flag with this command ? I can see it mentioned in the help:

❯ odo help create namespace
Create a new namespace. This command directly performs actions on the cluster and doesn't require a push.

Usage:
  odo create namespace [flags]

Aliases:
  namespace, project

Examples:
  # Create a new namespace
  odo create namespace my-namespace

Flags:
  -h, --help   Help for namespace
  -w, --wait   Wait until the namespace is ready

Additional Flags:
      --kubeconfig string    Paths to a kubeconfig. Only required if out-of-cluster.
  -v, --v Level              Number for the log level verbosity. Level varies from 0 to 9 (default 0).
      --vmodule moduleSpec   Comma-separated list of pattern=N settings for file-filtered logging

But when running the command with this flag, it looks like it didn't read/update the specified kubeconfig:

❯ odo create namespace test1 --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt --wait
 ✓  Waiting for namespace to come up [7ms]
 ✓  Namespace "test1" is ready for use
 ✓  New namespace created and now using namespace: test1

❯ kubectl --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt config get-contexts
CURRENT   NAME    CLUSTER               AUTHINFO   NAMESPACE
*         admin   ci-ln-yqzqj1t-72292   admin      

It looks like it ignored the specified kubeconfig and used the default one:

❯ kubectl config get-contexts            
CURRENT   NAME                                     CLUSTER                  AUTHINFO                         NAMESPACE
          admin@talos-default                      talos-default            admin@talos-default              default
          asoro/api-crc-testing:6443/kubeadmin     api-crc-testing:6443     kubeadmin/api-crc-testing:6443   asoro
          crc-admin                                api-crc-testing:6443     kubeadmin                        default
          crc-developer                            api-crc-testing:6443     developer                        default
          default/api-crc-testing:6443/kubeadmin   api-crc-testing:6443     kubeadmin/api-crc-testing:6443   default
*         kind-local-k8s-cluster                   kind-local-k8s-cluster   kind-local-k8s-cluster           test1

pkg/odo/cli/create/namespace/namespace.go Show resolved Hide resolved
@rm3l
Copy link
Member

rm3l commented May 11, 2022

Another point I just stumbled upon: maybe we should also validate the input namespace name, using the same ValidateK8sResourceName function we use from the github.com/devfile/library/pkg/util package ? Just to be consistent with the current validation rules.

Otherwise, we may be affected by #5652: I successfully created an all-numeric Kubernetes namespace using odo create namespace. And now that it has been set as my current context namespace, odo dev won't start:

☸ kind-local-k8s-cluster (default) in ~/w/t/5620-devfile-with-container-component-command on  main [$!?] via ☕ v17.0.2 on ☁️   
❯ odo create namespace 5648
 ✓  Namespace "5648" is ready for use
 ✓  New namespace created and now using namespace: 5648

☸ kind-local-k8s-cluster (5648) in ~/w/t/5620-devfile-with-container-component-command on  main [$!?] via ☕ v17.0.2 on ☁️   
❯ odo dev                  
  __
 /  \__     Developing using the my-component-with-command Devfile
 \__/  \    Namespace: 5648
 /  \__/    odo version: v3.0.0-alpha1
 \__/

↪ Deploying to the cluster in developer mode
 ✗  Waiting for Kubernetes resources 
Cleaning resources, please wait
 ✗  failed to create the component: component namespace "5648" is not valid, component namespace should conform the following requirements: 
- Contain at most 63 characters
- Contain only lowercase alphanumeric characters or ‘-’
- Start with an alphanumeric character
- End with an alphanumeric character
- Must not contain all numeric values

@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. Required by Prow. label May 11, 2022
@valaparthvi valaparthvi requested review from rm3l and feloy May 11, 2022 11:01
helper.Cmd("odo", "create", "namespace", commonVar.Project).ShouldFail()
})
By("using an invalid namespace name", func() {
helper.Cmd("odo", "create", "namespace", "12345").ShouldFail()
Copy link
Member

Choose a reason for hiding this comment

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

Even if the command did not pass, would it be possible to also check that:

  1. no namespace/project with this name has been created in the cluster
  2. the active namespace we had prior to running this command has not changed

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 think the first check is unnecessary here, it's being extra cautious. The second check makes sense.

Copy link
Member

@rm3l rm3l May 11, 2022

Choose a reason for hiding this comment

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

I think the first check is unnecessary here, it's being extra cautious.

To me, the call to ShouldFail just guarantees that an error occurred with the command, but at this point, you don't necessarily know what kind of error occurred.
If tomorrow, someone removes the code that validates the namespace name, and the command fails for any reason when setting the current active namespace, then this test would still pass, I think; and in this case, I think it would be nice to detect this.
But okay, that should be fine for now.

@rm3l
Copy link
Member

rm3l commented May 11, 2022

Another question after playing with the command: what is the expected behavior of the --kubeconfig flag with this command ? I can see it mentioned in the help:

❯ odo help create namespace
Create a new namespace. This command directly performs actions on the cluster and doesn't require a push.

Usage:
  odo create namespace [flags]

Aliases:
  namespace, project

Examples:
  # Create a new namespace
  odo create namespace my-namespace

Flags:
  -h, --help   Help for namespace
  -w, --wait   Wait until the namespace is ready

Additional Flags:
      --kubeconfig string    Paths to a kubeconfig. Only required if out-of-cluster.
  -v, --v Level              Number for the log level verbosity. Level varies from 0 to 9 (default 0).
      --vmodule moduleSpec   Comma-separated list of pattern=N settings for file-filtered logging

But when running the command with this flag, it looks like it didn't read/update the specified kubeconfig:

❯ odo create namespace test1 --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt --wait
 ✓  Waiting for namespace to come up [7ms]
 ✓  Namespace "test1" is ready for use
 ✓  New namespace created and now using namespace: test1

❯ kubectl --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt config get-contexts
CURRENT   NAME    CLUSTER               AUTHINFO   NAMESPACE
*         admin   ci-ln-yqzqj1t-72292   admin      

It looks like it ignored the specified kubeconfig and used the default one:

❯ kubectl config get-contexts            
CURRENT   NAME                                     CLUSTER                  AUTHINFO                         NAMESPACE
          admin@talos-default                      talos-default            admin@talos-default              default
          asoro/api-crc-testing:6443/kubeadmin     api-crc-testing:6443     kubeadmin/api-crc-testing:6443   asoro
          crc-admin                                api-crc-testing:6443     kubeadmin                        default
          crc-developer                            api-crc-testing:6443     developer                        default
          default/api-crc-testing:6443/kubeadmin   api-crc-testing:6443     kubeadmin/api-crc-testing:6443   default
*         kind-local-k8s-cluster                   kind-local-k8s-cluster   kind-local-k8s-cluster           test1

The fact that the --kubeconfig flag is not honored seems to affect almost all commands that expose it. As such, it is not necessarily related to this PR. So I created a separate issue: #5732

@valaparthvi
Copy link
Contributor Author

Another question after playing with the command: what is the expected behavior of the --kubeconfig flag with this command ? I can see it mentioned in the help:

❯ odo help create namespace
Create a new namespace. This command directly performs actions on the cluster and doesn't require a push.

Usage:
  odo create namespace [flags]

Aliases:
  namespace, project

Examples:
  # Create a new namespace
  odo create namespace my-namespace

Flags:
  -h, --help   Help for namespace
  -w, --wait   Wait until the namespace is ready

Additional Flags:
      --kubeconfig string    Paths to a kubeconfig. Only required if out-of-cluster.
  -v, --v Level              Number for the log level verbosity. Level varies from 0 to 9 (default 0).
      --vmodule moduleSpec   Comma-separated list of pattern=N settings for file-filtered logging

But when running the command with this flag, it looks like it didn't read/update the specified kubeconfig:

❯ odo create namespace test1 --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt --wait
 ✓  Waiting for namespace to come up [7ms]
 ✓  Namespace "test1" is ready for use
 ✓  New namespace created and now using namespace: test1

❯ kubectl --kubeconfig /home/asoro/work/tmp/cluster-bot/cluster-bot.kubeconfig.txt config get-contexts
CURRENT   NAME    CLUSTER               AUTHINFO   NAMESPACE
*         admin   ci-ln-yqzqj1t-72292   admin      

It looks like it ignored the specified kubeconfig and used the default one:

❯ kubectl config get-contexts            
CURRENT   NAME                                     CLUSTER                  AUTHINFO                         NAMESPACE
          admin@talos-default                      talos-default            admin@talos-default              default
          asoro/api-crc-testing:6443/kubeadmin     api-crc-testing:6443     kubeadmin/api-crc-testing:6443   asoro
          crc-admin                                api-crc-testing:6443     kubeadmin                        default
          crc-developer                            api-crc-testing:6443     developer                        default
          default/api-crc-testing:6443/kubeadmin   api-crc-testing:6443     kubeadmin/api-crc-testing:6443   default
*         kind-local-k8s-cluster                   kind-local-k8s-cluster   kind-local-k8s-cluster           test1

The fact that the --kubeconfig flag is not honored seems to affect almost all commands that expose it. As such, it is not necessarily related to this PR. So I created a separate issue: #5732

Ohhhh. I didn't see this was happening. The logic of this code is simply copy pasted from v2, I didn't take a look at it 🙄. Thanks for creating the issue!

@valaparthvi
Copy link
Contributor Author

/retest-required
Infra errors.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.6% 0.6% Duplication

@feloy
Copy link
Contributor

feloy commented May 12, 2022

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label May 12, 2022
@valaparthvi
Copy link
Contributor Author

/retest-required

@valaparthvi valaparthvi requested a review from rm3l May 12, 2022 14:21
Copy link
Member

@rm3l rm3l left a comment

Choose a reason for hiding this comment

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

/approve

@openshift-ci
Copy link

openshift-ci bot commented May 12, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rm3l

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

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label May 12, 2022
@openshift-ci openshift-ci bot merged commit e9c0579 into redhat-developer:main May 12, 2022
cdrage pushed a commit to cdrage/odo that referenced this pull request Aug 31, 2022
* odo create namespace

* Add documentation

Signed-off-by: Parthvi Vala <[email protected]>

* Philippe's review

Signed-off-by: Parthvi Vala <[email protected]>

* Rename doc file

* Attempt at fixing integration tests

Signed-off-by: Parthvi Vala <[email protected]>

* Second Attempt at fixing integration tests

Signed-off-by: Parthvi Vala <[email protected]>

* Arm3l review

* Arm3l review part 2
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. Required by Prow. kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants