close
Skip to content

fix: adopt pre-existing namespaces with no owner annotation - #331

Open
rckr2710 wants to merge 1 commit into
kubeflow:mainfrom
rckr2710:fix/profile-namespace-adoption
Open

fix: adopt pre-existing namespaces with no owner annotation#331
rckr2710 wants to merge 1 commit into
kubeflow:mainfrom
rckr2710:fix/profile-namespace-adoption

Conversation

@rckr2710

Copy link
Copy Markdown

Problem

The Profile controller rejects a namespace that already exists when its
Profile CR is applied, unless the namespace already has a matching
owner annotation. A namespace created via kubectl create ns (no
annotation at all) was always rejected, even with no other claimant.

Fix

Adopt the namespace when it has no owner annotation yet (nothing has
claimed it), and continue to reject only when it's owned by a different
user. On adoption, also set a controller OwnerReference so the
namespace is garbage-collected with its Profile, matching the behavior
of namespaces this controller creates itself.

Testing

Unit Tests

Added unit tests in profile_controller_test.go covering the following scenarios:

  • Adoption of an existing namespace without an owner annotation.
  • Verification that an OwnerReference is added during namespace adoption.
  • Existing namespace already owned by the same user.
  • Existing namespace owned by a different user (Profile reconciliation is rejected).
  • Preservation of an existing custom istio-injection label during adoption.

All unit tests pass successfully.

make test

Manual Validation

The changes were verified on a local Kind cluster by:

  1. Building the updated Dashboard image.
  2. Deploying the custom image.
  3. Creating a namespace using:
    kubectl create namespace test-profile
  4. Applying a corresponding Profile resource.
  5. Verifying that:
    • the existing namespace is successfully adopted;
    • the owner annotation is added;
    • the Profile is set as the controller OwnerReference;
    • default namespace labels are applied;
    • existing custom labels (including istio-injection) are preserved.
  6. Verifying that a namespace already owned by another user continues to be rejected.

related: #330
closes: #330

Signed-off-by: rckr2710 <rckr2710@gmail.com>
@google-oss-prow google-oss-prow Bot added the area/profiles area - related to profile-controller label Jun 26, 2026
@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign thesuperzapper for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details 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

@rckr2710 rckr2710 changed the title fix(profile-controller): adopt pre-existing namespaces with no owner annotation fix: adopt pre-existing namespaces with no owner annotation Jun 26, 2026
@thesuperzapper

Copy link
Copy Markdown
Member

This would need significant discussions about the security implications before we can make any changes like this.

It would also likely be classified as a "breaking change" and so only be able to be done on a major version.

Can you share what your motivation was here so we can understand if there is a better solution?

/hold

@vamshirajagiri

Copy link
Copy Markdown

Yes, this is a feature we're looking for because of one of our enterprise onboarding workflows @thesuperzapper

A couple of use cases:

  • Pre-provisioned namespaces: In our environment, the platform team creates the namespace first and configures everything (NetworkPolicies, ResourceQuotas, LimitRanges, storage, etc.). Kubeflow is onboarded later, so Profile creation fails simply because the namespace already exists.

  • GitOps-managed clusters: Namespaces are created and managed by GitOps/platform automation, not by applications. It would be useful if Kubeflow could integrate with these existing namespaces instead of requiring a new one.

I completely agree that automatically adopting any existing namespace would be a security concern.

Instead, what if adoption was explicitly allowed by an administrator?

For example, only namespaces with a label like:

kubeflow.org/adoption: "true"

would be eligible for adoption.

Label present → Kubeflow can adopt the namespace.

Label absent → Keep the current behavior and reject adoption.

This way, the decision stays with the Kubernetes/Kubeflow administrators, and teams can enable it only for namespaces that are intentionally prepared for Kubeflow. The default behavior remains secure while still supporting enterprise onboarding workflows @rckr2710 , I would help you with the changes.

@thesuperzapper

Copy link
Copy Markdown
Member

Was thinking more about this, and the one of the most serious question this feature opens is:

Could someone with Profile access take over the kube-system namespace, or other critical namespaces?
And how would we prevent this.

@vamshirajagiri

Copy link
Copy Markdown

That's a valid concern, and I agree Profile users should never be able to adopt arbitrary namespaces. My thought was that the decision should be entirely in the hands of the Kubernetes/Kubeflow administrator.

For example, Kubeflow would only consider adopting a namespace if:

  • the namespace is explicitly marked as adoptable by an admin (e.g. kubeflow.org/adoption: "true"),
  • it isn't already managed by another Profile,
  • and the requesting user is authorized for that namespace.

Namespaces like kube-system, argocd, cert-manager, monitoring, etc. would never be labeled as adoptable in the first place, so they would continue to be rejected.

The intention isn't to let users take over existing namespaces, but to support enterprise environments where platform teams pre-provision dedicated namespaces for Kubeflow users and explicitly allow Kubeflow to manage them.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Enables the Profile controller to adopt pre-existing, unclaimed namespaces while retaining ownership-conflict protection.

Changes:

  • Adds namespace adoption, labels, Istio injection, and controller ownership.
  • Adds unit tests for adoption, conflicts, and label preservation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
profile_controller.go Implements namespace adoption and ownership handling.
profile_controller_test.go Tests namespace adoption scenarios.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +207 to +210
if err := controllerutil.SetControllerReference(instance, foundNs, r.Scheme); err != nil {
logger.Info("warning: could not set ControllerReference on adopted namespace",
"namespace", foundNs.Name, "error", err.Error())
}
Comment on lines +174 to +178
// Check existing namespace ownership before moving forward.
//
// A namespace that has no "owner" annotation at all has never been claimed
// by any Profile (for example, it was created directly via
// `kubectl create ns <name>` before the Profile CR existed). Such a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/profiles area - related to profile-controller do-not-merge/hold size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profile Controller: Allow Creating Kubeflow Profiles for Pre-existing Kubernetes Namespaces

4 participants