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
LowAddress
Compliance, SecurityCompliance Standards
- CIS OKE
Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
In OKE, namespaces are Kubernetes objects, so you remediate this by:
-
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.
-
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
kubectlsetup command shown (something likeoci ce cluster create-kubeconfig ...). - Paste that command into Cloud Shell and run it.
- Verify access:
-
Create a Non-Default Namespace
- Still in Cloud Shell, create a new namespace, e.g.
prod: - Confirm
prod(or your chosen name) appears.
- Still in Cloud Shell, create a new namespace, e.g.
-
Move Workloads Out of
defaultNamespace For each deployment/service currently indefault:-
Export its manifest:
-
Edit the YAML files (in Cloud Shell, use
nanoorvi):- Change:
to:
- Remove fields under
metadatathat Kubernetes auto-manages (likeuid,resourceVersion,creationTimestamp,managedFields) to avoid errors.
- Change:
-
Apply them into the new namespace:
-
Once confirmed running in the new namespace, delete from
default:
-
Export its manifest:
-
Set a Default Namespace in Your Context (Optional)
- To avoid accidentally using
default: - Now running
kubectl get podswill act inprodby default.
- To avoid accidentally using
-
Verify No Workloads Use
default- Check
defaultnamespace is empty of your apps: - Only Kubernetes system objects (if any) should remain, or it can be empty.
- Check
Using CLI
Using CLI
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).
Copy the
Check:
3. Move workloads out of
3.1 Identify resources currently in
(Template details depend on your Gatekeeper setup; key point: reject manifests with
1. Get kubeconfig for the OKE cluster (using OCI CLI)
- Make sure OCI CLI is configured:
- Get your OKE cluster OCID (if you don’t have it already):
id of the target cluster.- Generate kubeconfig for that cluster:
- Point kubectl to that config:
2. Create non-default namespaces
Decide the logical namespaces (e.g.,prod, staging, dev).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:- Export current manifests:
-
Edit the file:
- Change
namespace: defaultto the target namespace (e.g.,namespace: prod) undermetadata. - Remove
statussections, and any cluster-assigned fields likeresourceVersion,uid,creationTimestamp, etc.
- Change
- Apply to new namespace:
- Once you verify everything runs correctly in the new namespace(s), delete the resources from
default:
4. Enforce “no workloads in default namespace” (optional but recommended)
4.1 Use a Namespace-level or cluster policy (Gatekeeper / OPA or admission webhook)
If using Gatekeeper (as an example):- Install Gatekeeper (once per cluster).
- Create a ConstraintTemplate that denies
defaultnamespace usage. - Create a Constraint, e.g.:
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
- Ensure no resources exist in
default:
- Ensure workloads run in non-default namespaces:
- Test that new deployments to
defaultare rejected (if you added an admission policy).
Using Python
Using Python
To remediate “OCI OKE should use non-default namespaces” with Python, you essentially need to:
3. Migrate Existing Deployments from
Kubernetes does not support changing the namespace of an existing object in place. You have to:Repeat similarly for Services, ConfigMaps, Secrets, etc., as needed.
4.1. Default to the New Namespace in
You can script modification of your With this,
5. (Optional) Block Use of
If you use Gatekeeper/OPA or Kyverno on OKE, you can add a policy to deny resources in You can create that via Python as a generic
Summary of Remediation
- Create one or more custom namespaces.
- Migrate workloads from
defaultto the new namespace(s). - Enforce that new workloads don’t get deployed into
default.
kubeconfig).1. Prereqs
-
Ensure you have
kubectlaccess to the OKE cluster and a validkubeconfig: -
Install the Kubernetes Python client:
-
Ensure your
KUBECONFIGenvironment variable is set (or~/.kube/configexists 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
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
defaultusage.
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: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):CustomObjectsApi call if Kyverno is installed.Summary of Remediation
- Use Python/Kubernetes client to create a non-default namespace in OKE.
- Migrate workloads from
defaultto that namespace (re-create in the new namespace and delete the old). - Change your kubeconfig context so the new namespace is the default.
- Optionally enforce a policy to block new objects in
default.
Using Terraform
Using Terraform
The 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
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: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.
