2019-10-23 14:06:03 -07:00
# Disallow use of default namespace
2019-11-10 15:50:18 -08:00
Kubernetes namespaces are an optional feature that provide a way to segment and isolate cluster resources across multiple applications and users. As a best practice, workloads should be isolated with namespaces. Namespaces should be required and the default (empty) namespace should not be used.
2019-10-23 14:06:03 -07:00
2020-11-11 15:55:02 -05:00
## Policy YAML
2019-10-23 14:06:03 -07:00
2020-11-11 15:55:02 -05:00
[disallow_default_namespace.yaml ](best_practices/disallow_default_namespace.yaml )
2019-10-23 14:06:03 -07:00
````yaml
2019-11-13 13:56:20 -08:00
apiVersion: kyverno.io/v1
2019-10-23 14:06:03 -07:00
kind: ClusterPolicy
metadata:
2019-11-10 15:50:18 -08:00
name: disallow-default-namespace
2020-03-04 17:40:33 -08:00
annotations:
2020-11-11 15:55:02 -05:00
pod-policies.kyverno.io/autogen-controllers: none
2020-03-04 17:40:33 -08:00
policies.kyverno.io/category: Workload Isolation
2020-11-11 15:55:02 -05:00
policies.kyverno.io/description: Kubernetes namespaces are an optional feature
that provide a way to segment and isolate cluster resources across multiple
applications and users. As a best practice, workloads should be isolated with
namespaces. Namespaces should be required and the default (empty) namespace
2020-03-04 17:40:33 -08:00
should not be used.
2019-10-23 14:06:03 -07:00
spec:
2020-08-19 14:04:58 -07:00
validationFailureAction: audit
2019-10-23 14:06:03 -07:00
rules:
2019-11-10 15:50:18 -08:00
- name: validate-namespace
2019-10-23 14:06:03 -07:00
match:
resources:
kinds:
- Pod
validate:
2019-11-10 15:50:18 -08:00
message: "Using 'default' namespace is not allowed"
2019-10-23 14:06:03 -07:00
pattern:
metadata:
namespace: "!default"
2019-11-10 15:50:18 -08:00
- name: require-namespace
2019-10-23 14:06:03 -07:00
match:
resources:
kinds:
- Pod
validate:
message: "A namespace is required"
pattern:
metadata:
namespace: "?*"
2020-03-04 17:40:33 -08:00
- name: validate-podcontroller-namespace
match:
resources:
kinds:
- DaemonSet
- Deployment
- Job
- StatefulSet
validate:
message: "Using 'default' namespace is not allowed for podcontrollers"
pattern:
metadata:
namespace: "!default"
- name: require-podcontroller-namespace
match:
resources:
kinds:
- DaemonSet
- Deployment
- Job
- StatefulSet
validate:
message: "A namespace is required for podcontrollers"
pattern:
metadata:
namespace: "?*"
2019-10-23 14:06:03 -07:00
````