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

kctrl: Flag to create namespace when adding new repo #1113

Merged
merged 7 commits into from
Apr 5, 2023

Conversation

ThomasVitale
Copy link
Contributor

What this PR does / why we need it:

When adding a new package repository to a cluster, it's now possible to create the installation namespace automatically by specifying the "--create-namespace" flag.

Which issue(s) this PR fixes:

Fixes gh-1001

Does this PR introduce a user-facing change?

Added a new flag `create-namespace` for creating the installation namespace when adding a new package repository to a cluster.

Additional Notes for your reviewer:

After a first review of this pull request to validate the behaviour of the new flag, I will submit a PR to update the documentation on the project website.

Review Checklist:
  • Follows the developer guidelines
  • Relevant tests are added or updated
  • [] Relevant docs in this repo added or updated
  • [] Relevant carvel.dev docs added or updated in a separate PR and there's
    a link to that PR
  • Code is at least as readable and maintainable as it was before this
    change

Additional documentation e.g., Proposal, usage docs, etc.:


@ThomasVitale ThomasVitale changed the title Option to create namespace when adding new repo kctrl: Flag to create namespace when adding new repo Feb 26, 2023

_, err = coreClient.CoreV1().Namespaces().Create(context.Background(), namespace, metav1.CreateOptions{})
if err != nil {
o.ui.PrintLinef("The namespace %s already exists", o.NamespaceFlags.Name)
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 probably return the error if it's not of type IsAlreadyExists and ignore if it already exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I changed it as suggested.

Copy link
Contributor

@100mik 100mik left a comment

Choose a reason for hiding this comment

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

Some thoughts!

In addition to these looks like there is a redundant log line being printed on line 187
(More specifically: o.ui.PrintLinef("Waiting for package repository to be added"))
That can be removed as a part of this PR but does not block the PR itself!

if errors.IsAlreadyExists(err) {
o.statusUI.PrintMessagef("The namespace '%s' already exists", o.NamespaceFlags.Name)
} else {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if errors.IsAlreadyExists(err) {
o.statusUI.PrintMessagef("The namespace '%s' already exists", o.NamespaceFlags.Name)
} else {
return err
}
if !errors.IsAlreadyExists(err) {
return err
}
o.statusUI.PrintMessagef("The namespace '%s' already exists", o.NamespaceFlags.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.

I have refactored that part to print only a message if the namespace is created as suggested in the previous comment.

@ThomasVitale ThomasVitale force-pushed the gh-1001 branch 2 times, most recently from 1d79ada to 0be3e96 Compare March 7, 2023 18:45
@@ -141,6 +145,30 @@ func (o *AddOrUpdateOptions) Run(args []string) error {
return err
}

if o.CreateNamespace {
o.statusUI.PrintMessagef("Creating namespace '%s'", o.NamespaceFlags.Name)
Copy link
Member

Choose a reason for hiding this comment

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

Should we remove this line as well? Or you already did and forgot to push the change?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I am good with the changes, once this is resolved ^

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 for catching this. You'd think after several commits I'd learn the lesson about signing off commits, but I keep missing that and had to rebase (my Git is configured with the -S parameter for signing, but the DCA sign-off is with -s). I guess while doing that, this message popped up again.

When adding a new package repository to a cluster, it's now possible
to create the installation namespace automatically
by specifying the "--create-namespace" flag.

Fixes gh-1001

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Copy link
Contributor

@100mik 100mik left a comment

Choose a reason for hiding this comment

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

Changes look good to me! Thank you so much for contributing

@praveenrewar feel free to merge this away if it looks good to you 🚀

Comment on lines 141 to 142
logger.Section("creating a repository in a namespace that already exists", func() {
kubectl.Run([]string{"create", "namespace", existingRepoNamespace})
Copy link
Member

Choose a reason for hiding this comment

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

We could probably just use env.Namespace instead of this. It would remove the overhead of creating/deleting one namespace.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry didn't take a look at the tests earlier 😅

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 doing that, but then opted for creating it explicitly to make the test case resemble the current 2-steps way of adding a repo as opposed to the changes I made in the PR (1. create the namespace, 2. add a repo). But I can change it to use the general one, if you want :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the tests here to re-use env.Namespace: 893f9e7

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
Copy link
Member

@praveenrewar praveenrewar left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you so much for the contribution 🚀

@praveenrewar praveenrewar merged commit 15c92de into carvel-dev:develop Apr 5, 2023
6 checks passed
@ThomasVitale
Copy link
Contributor Author

@100mik @praveenrewar thanks a lot for your guidance and review

@github-actions github-actions bot added the carvel-triage This issue has not yet been reviewed for validity label Apr 5, 2023
@praveenrewar praveenrewar removed the carvel-triage This issue has not yet been reviewed for validity label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

kctrl: When adding a package repo, include option to create the namespace if not existing
3 participants