apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:name: policy-pod
namespace:spec:complianceType:remediationAction:namespaces:exclude:include:object-templates:-complianceType:objectDefinition:apiVersion:kind: Pod # pod must existmetadata:name:spec:containers:-image:name:ports:-containerPort:...
Copy to ClipboardCopied!Toggle word wrapToggle overflow
apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
name:
namespace:
spec:
complianceType:
remediationAction:
namespaces:
exclude:
include:
object-templates:
- complianceType:
objectDefinition:
kind: RoleBinding # role binding must exist
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: operate-pods-rolebinding
subjects:
- kind: User
name: admin # Name is case sensitive
apiGroup:
roleRef:
kind: Role #this must be Role or ClusterRole
name: operator # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io
...
apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:name:namespace:spec:complianceType:remediationAction:namespaces:exclude:include:object-templates:-complianceType:objectDefinition:kind: RoleBinding # role binding must existapiVersion: rbac.authorization.k8s.io/v1
metadata:name: operate-pods-rolebinding
subjects:-kind: User
name: admin # Name is case sensitiveapiGroup:roleRef:kind: Role #this must be Role or ClusterRolename: operator # this must match the name of the Role or ClusterRole you wish to bind toapiGroup: rbac.authorization.k8s.io
...
Copy to ClipboardCopied!Toggle word wrapToggle overflow
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: compliance-suite-e8
spec:
remediationAction: inform
severity: high
object-templates:
- complianceType: musthave # this template checks if scan has completed by checking the status field
objectDefinition:
apiVersion: compliance.openshift.io/v1alpha1
kind: ScanSettingBinding
metadata:
name: e8
namespace: openshift-compliance
profiles:
- apiGroup: compliance.openshift.io/v1alpha1
kind: Profile
name: ocp4-e8
- apiGroup: compliance.openshift.io/v1alpha1
kind: Profile
name: rhcos4-e8
settingsRef:
apiGroup: compliance.openshift.io/v1alpha1
kind: ScanSetting
name: default
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:name: compliance-suite-e8
spec:remediationAction: inform
severity: high
object-templates:-complianceType: musthave # this template checks if scan has completed by checking the status fieldobjectDefinition:apiVersion: compliance.openshift.io/v1alpha1
kind: ScanSettingBinding
metadata:name: e8
namespace: openshift-compliance
profiles:-apiGroup: compliance.openshift.io/v1alpha1
kind: Profile
name: ocp4-e8
-apiGroup: compliance.openshift.io/v1alpha1
kind: Profile
name: rhcos4-e8
settingsRef:apiGroup: compliance.openshift.io/v1alpha1
kind: ScanSetting
name: default
Copy to ClipboardCopied!Toggle word wrapToggle overflow
一个 ComplianceSuite 资源 (compliance-suite-e8),用于通过检查 status 字段来验证扫描是否已完成:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: compliance-suite-e8
spec:
remediationAction: inform
severity: high
object-templates:
- complianceType: musthave # this template checks if scan has completed by checking the status field
objectDefinition:
apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceSuite
metadata:
name: e8
namespace: openshift-compliance
status:
phase: DONE
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:name: compliance-suite-e8
spec:remediationAction: inform
severity: high
object-templates:-complianceType: musthave # this template checks if scan has completed by checking the status fieldobjectDefinition:apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceSuite
metadata:name: e8
namespace: openshift-compliance
status:phase: DONE
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Copy to ClipboardCopied!Toggle word wrapToggle overflow
一个 ComplianceSuite 资源 (compliance-suite-cis),用于通过检查 status 字段来验证扫描是否已完成:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: compliance-suite-cis
spec:
remediationAction: inform
severity: high
object-templates:
- complianceType: musthave # this template checks if scan has completed by checking the status field
objectDefinition:
apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceSuite
metadata:
name: cis
namespace: openshift-compliance
status:
phase: DONE
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:name: compliance-suite-cis
spec:remediationAction: inform
severity: high
object-templates:-complianceType: musthave # this template checks if scan has completed by checking the status fieldobjectDefinition:apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceSuite
metadata:name: cis
namespace: openshift-compliance
status:phase: DONE
Copy to ClipboardCopied!Toggle word wrapToggle overflow
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-networkpolicy
annotations:
policies.kyverno.io/title: Add Network Policy
policies.kyverno.io/category: Multi-Tenancy
policies.kyverno.io/subject: NetworkPolicy
policies.kyverno.io/description: >-
By default, Kubernetes allows communications across all Pods within a cluster.
The NetworkPolicy resource and a CNI plug-in that supports NetworkPolicy must be used to restrict
communications. A default NetworkPolicy should be configured for each Namespace to
default deny all ingress and egress traffic to the Pods in the Namespace. Application
teams can then configure additional NetworkPolicy resources to allow desired traffic
to application Pods from select sources. This policy will create a new NetworkPolicy resource
named `default-deny` which will deny all traffic anytime a new Namespace is created.
spec:
rules:
- name: default-deny
match:
resources:
kinds:
- Namespace
generate:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: default-deny
namespace: "{{request.object.metadata.name}}"
synchronize: true
data:
spec:
# select all pods in the namespace
podSelector: {}
# deny all traffic
policyTypes:
- Ingress
- Egress
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:name: add-networkpolicy
annotations:policies.kyverno.io/title: Add Network Policy
policies.kyverno.io/category: Multi-Tenancy
policies.kyverno.io/subject: NetworkPolicy
policies.kyverno.io/description:>-
By default, Kubernetes allows communications across all Pods within a cluster.
The NetworkPolicy resource and a CNI plug-in that supports NetworkPolicy must be used to restrict
communications. A default NetworkPolicy should be configured for each Namespace to
default deny all ingress and egress traffic to the Pods in the Namespace. Application
teams can then configure additional NetworkPolicy resources to allow desired traffic
to application Pods from select sources. This policy will create a new NetworkPolicy resource
named `default-deny` which will deny all traffic anytime a new Namespace is created.
spec:rules:-name: default-deny
match:resources:kinds:- Namespace
generate:apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: default-deny
namespace:"{{request.object.metadata.name}}"synchronize:truedata:spec:# select all pods in the namespacepodSelector:{}# deny all trafficpolicyTypes:- Ingress
- Egress
Copy to ClipboardCopied!Toggle word wrapToggle overflow
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-ns-quota
annotations:
policies.kyverno.io/title: Add Quota
policies.kyverno.io/category: Multi-Tenancy
policies.kyverno.io/subject: ResourceQuota, LimitRange
policies.kyverno.io/description: >-
To better control the number of resources that can be created in a given
Namespace and provide default resource consumption limits for Pods,
ResourceQuota and LimitRange resources are recommended.
This policy will generate ResourceQuota and LimitRange resources when
a new Namespace is created.
spec:
rules:
- name: generate-resourcequota
match:
resources:
kinds:
- Namespace
generate:
apiVersion: v1
kind: ResourceQuota
name: default-resourcequota
synchronize: true
namespace: "{{request.object.metadata.name}}"
data:
spec:
hard:
requests.cpu: '4'
requests.memory: '16Gi'
limits.cpu: '4'
limits.memory: '16Gi'
- name: generate-limitrange
match:
resources:
kinds:
- Namespace
generate:
apiVersion: v1
kind: LimitRange
name: default-limitrange
synchronize: true
namespace: "{{request.object.metadata.name}}"
data:
spec:
limits:
- default:
cpu: 500m
memory: 1Gi
defaultRequest:
cpu: 200m
memory: 256Mi
type: Container
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:name: add-ns-quota
annotations:policies.kyverno.io/title: Add Quota
policies.kyverno.io/category: Multi-Tenancy
policies.kyverno.io/subject: ResourceQuota, LimitRange
policies.kyverno.io/description:>-
To better control the number of resources that can be created in a given
Namespace and provide default resource consumption limits for Pods,
ResourceQuota and LimitRange resources are recommended.
This policy will generate ResourceQuota and LimitRange resources when
a new Namespace is created.
spec:rules:-name: generate-resourcequota
match:resources:kinds:- Namespace
generate:apiVersion: v1
kind: ResourceQuota
name: default-resourcequota
synchronize:truenamespace:"{{request.object.metadata.name}}"data:spec:hard:requests.cpu:'4'requests.memory:'16Gi'limits.cpu:'4'limits.memory:'16Gi'-name: generate-limitrange
match:resources:kinds:- Namespace
generate:apiVersion: v1
kind: LimitRange
name: default-limitrange
synchronize:truenamespace:"{{request.object.metadata.name}}"data:spec:limits:-default:cpu: 500m
memory: 1Gi
defaultRequest:cpu: 200m
memory: 256Mi
type: Container
Copy to ClipboardCopied!Toggle word wrapToggle overflow
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: sync-secrets
annotations:
policies.kyverno.io/title: Sync Secrets
policies.kyverno.io/category: Sample
policies.kyverno.io/subject: Secret
policies.kyverno.io/description: >-
Secrets like registry credentials often need to exist in multiple
Namespaces so Pods there have access. Manually duplicating those Secrets
is time consuming and error prone. This policy will copy a
Secret called `regcred` which exists in the `default` Namespace to
new Namespaces when they are created. It will also push updates to
the copied Secrets should the source Secret be changed.
spec:
rules:
- name: sync-image-pull-secret
match:
resources:
kinds:
- Namespace
generate:
apiVersion: v1
kind: Secret
name: regcred
namespace: "{{request.object.metadata.name}}"
synchronize: true
clone:
namespace: default
name: regcred
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:name: sync-secrets
annotations:policies.kyverno.io/title: Sync Secrets
policies.kyverno.io/category: Sample
policies.kyverno.io/subject: Secret
policies.kyverno.io/description:>-
Secrets like registry credentials often need to exist in multiple
Namespaces so Pods there have access. Manually duplicating those Secrets
is time consuming and error prone. This policy will copy a
Secret called `regcred` which exists in the `default` Namespace to
new Namespaces when they are created. It will also push updates to
the copied Secrets should the source Secret be changed.
spec:rules:-name: sync-image-pull-secret
match:resources:kinds:- Namespace
generate:apiVersion: v1
kind: Secret
name: regcred
namespace:"{{request.object.metadata.name}}"synchronize:trueclone:namespace: default
name: regcred
Copy to ClipboardCopied!Toggle word wrapToggle overflow