Skip to main content

More Info:

Workloads should live in dedicated namespaces, not the default namespace. Per-team namespaces enable namespace-scoped RBAC, NetworkPolicy, and quota, all of which are awkward to apply to default.

Risk Level

Low

Address

Compliance, Security

Compliance Standards

  • CIS OKE

Triage and Remediation

Remediation

Using Console

In OKE, namespaces are Kubernetes objects, so you remediate this by:
  1. Open OCI Console and Cloud Shell
    • Sign in to OCI Console.
    • In the top-right, click Cloud Shell (terminal icon) to open a shell already authenticated to your tenancy.
  2. Get Cluster Kubeconfig in Cloud Shell
    • In the Console, go to Developer Services → Kubernetes Clusters (OKE).
    • Select your cluster.
    • Click Access Cluster (or Cluster Access), choose Local access (for Cloud Shell it’s treated as local), and copy the kubectl setup command shown (something like oci ce cluster create-kubeconfig ...).
    • Paste that command into Cloud Shell and run it.
    • Verify access:
  3. Create a Non-Default Namespace
    • Still in Cloud Shell, create a new namespace, e.g. prod:
    • Confirm prod (or your chosen name) appears.
  4. Move Workloads Out of default Namespace For each deployment/service currently in default:
    • Export its manifest:
    • Edit the YAML files (in Cloud Shell, use nano or vi):
      • Change:
        to:
      • Remove fields under metadata that Kubernetes auto-manages (like uid, resourceVersion, creationTimestamp, managedFields) to avoid errors.
    • Apply them into the new namespace:
    • Once confirmed running in the new namespace, delete from default:
  5. Set a Default Namespace in Your Context (Optional)
    • To avoid accidentally using default:
    • Now running kubectl get pods will act in prod by default.
  6. Verify No Workloads Use default
    • Check default namespace is empty of your apps:
    • Only Kubernetes system objects (if any) should remain, or it can be empty.
This satisfies the “use non-default namespaces” requirement using the OCI Console plus Cloud Shell.
Below are concise, step‑by‑step remediation instructions to ensure your OCI OKE cluster uses non‑default namespaces, using OCI CLI (to get kubeconfig) and kubectl (for Kubernetes objects).

1. Get kubeconfig for the OKE cluster (using OCI CLI)

  1. Make sure OCI CLI is configured:
  1. Get your OKE cluster OCID (if you don’t have it already):
Copy the id of the target cluster.
  1. Generate kubeconfig for that cluster:
  1. Point kubectl to that config:

2. Create non-default namespaces

Decide the logical namespaces (e.g., prod, staging, dev).
Check:

3. Move workloads out of default namespace

3.1 Identify resources currently in default namespace

3.2 Re-deploy workloads into new namespaces

You cannot “move” namespace of an existing object; you must recreate it:
  1. Export current manifests:
  1. Edit the file:
    • Change namespace: default to the target namespace (e.g., namespace: prod) under metadata.
    • Remove status sections, and any cluster-assigned fields like resourceVersion, uid, creationTimestamp, etc.
  2. Apply to new namespace:
  1. Once you verify everything runs correctly in the new namespace(s), delete the resources from default:

4.1 Use a Namespace-level or cluster policy (Gatekeeper / OPA or admission webhook)

If using Gatekeeper (as an example):
  1. Install Gatekeeper (once per cluster).
  2. Create a ConstraintTemplate that denies default namespace usage.
  3. Create a Constraint, e.g.:
(Template details depend on your Gatekeeper setup; key point: reject manifests with metadata.namespace: default or no namespace.)

4.2 Enforce namespace usage in CI/CD

Update Helm charts/Manifests to always specify a non-default namespace and/or use --namespace <ns> in deployment scripts.

5. Validation

  1. Ensure no resources exist in default:
  1. Ensure workloads run in non-default namespaces:
  1. Test that new deployments to default are rejected (if you added an admission policy).
To remediate “OCI OKE should use non-default namespaces” with Python, you essentially need to:
  1. Create one or more custom namespaces.
  2. Migrate workloads from default to the new namespace(s).
  3. Enforce that new workloads don’t get deployed into default.
Below are concise, step‑by‑step instructions using Python and the Kubernetes Python client (works with any OKE cluster once you have kubeconfig).

1. Prereqs

  1. Ensure you have kubectl access to the OKE cluster and a valid kubeconfig:
  2. Install the Kubernetes Python client:
  3. Ensure your KUBECONFIG environment variable is set (or ~/.kube/config exists and points to OKE):

2. Create a Non-Default Namespace via Python


3. Migrate Existing Deployments from default to the New Namespace

Kubernetes does not support changing the namespace of an existing object in place. You have to:
  • Fetch the object from default
  • Remove the resourceVersion, uid, etc.
  • Re-create it in the new namespace
  • Delete it from default
Example for Deployments:
Repeat similarly for Services, ConfigMaps, Secrets, etc., as needed.

4. Ensure New Workloads Use Non-Default Namespace

You can enforce non-default namespaces in several ways. The simplest operational method:
  • Create and use context that defaults to your new namespace.
  • Optionally, use an Admission Controller (e.g., Gatekeeper/Kyverno) to block default usage.

4.1. Default to the New Namespace in kubeconfig (Operational Control)

You can script modification of your kubeconfig with Python (YAML edit) so your context defaults to the non-default namespace:
With this, kubectl apply (and tools using this context) will use prod-apps by default instead of default.

5. (Optional) Block Use of default Namespace with Policy

If you use Gatekeeper/OPA or Kyverno on OKE, you can add a policy to deny resources in default. Example (Kyverno) policy YAML (not Python, but you can apply it via Python using the same client patterns above):
You can create that via Python as a generic CustomObjectsApi call if Kyverno is installed.
Summary of Remediation
  1. Use Python/Kubernetes client to create a non-default namespace in OKE.
  2. Migrate workloads from default to that namespace (re-create in the new namespace and delete the old).
  3. Change your kubeconfig context so the new namespace is the default.
  4. Optionally enforce a policy to block new objects in default.
This satisfies the requirement that OKE “should use non-default namespaces” and provides an automated Python-based approach.
The oci_containerengine_cluster (OKE cluster) resource cannot manage Kubernetes namespaces; namespaces are runtime Kubernetes objects, not an OCI cluster property, so this finding cannot be fixed on that exact resource type via Terraform.To remediate with Terraform, you must use the Kubernetes provider against the OKE cluster and create non-default namespaces for your workloads, then move workloads to those namespaces:
This change does not replace the OKE cluster; it only creates namespaces and re-homes workloads. You must separately update or recreate any existing workloads currently in the default namespace to target the new namespaces.Verification: terraform plan should show creation of kubernetes_namespace_v1 resources and modifications (or replacements) of Kubernetes workload resources changing metadata.namespace from default to the dedicated namespaces.