2.5.14. gatekeeper ポリシーの統合
gatekeeper ポリシーの作成、適用、表示、および更新について説明します。
必要なアクセス権限:: クラスターの管理者
前提条件: Gatekeeper をインストールしておく必要があります。詳細は、open-policy-agent/gatekeeper リポジトリー を参照してください。
2.5.14.1. gatekeeper ポリシーの作成
コマンドラインインターフェース (CLI) から gatekeeper ポリシーの YAML ファイルを作成できます。Red Hat Advanced Cluster Management for Kubernetes 設定ポリシーを使用して、ハブクラスターからマネージドクラスターに gatekeeper ポリシーを伝播します。以下のセクションを参照して、受付および監査シナリオの gatekeeper ポリシーを作成します。
2.5.14.1.1. 受付シナリオの gatekeeper ポリシーの作成
Red Hat Advanced Cluster Management 設定ポリシーを使用して gatekeeper ポリシーを作成し、gatekeeper 受付 Webhook が生成するイベントを検索します。
注記: Gatekeeper は、emit-admission-events を true に設定してデプロイする必要があります。
gatekeeper ポリシーの YAML ファイルを作成します。以下のコマンドを実行します。
kubectl create -f policy-gatekeeper-admission.yaml
gatekeeper ポリシーは以下のポリシーのようになります。
apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
name: policy-gatekeeper
namespace: default
annotations:
policy.open-cluster-management.io/standards:
policy.open-cluster-management.io/categories:
policy.open-cluster-management.io/controls:
spec:
disabled: false
policy-templates:
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-gatekeeper-k8srequiredlabels
spec:
remediationAction: enforce # will be overridden by remediationAction in parent policy
severity: low
object-templates:
- complianceType: musthave
objectDefinition:
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8srequiredlabels
spec:
crd:
spec:
names:
kind: K8sRequiredLabels
validation:
# Schema for the `parameters` field
openAPIV3Schema:
properties:
labels:
type: array
items: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8srequiredlabels
violation[{"msg": msg, "details": {"missing_labels": missing}}] {
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0
msg := sprintf("you must provide labels: %v", [missing])
}
- complianceType: musthave
objectDefinition:
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: ns-must-have-gk
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Namespace"]
parameters:
labels: ["gatekeeper"]
- objectDefinition:
apiVersion: policy.open-cluster-management.io/v1
kind: ConfigurationPolicy
metadata:
name: policy-gatekeeper-admission
spec:
remediationAction: inform # will be overridden by remediationAction in parent policy
severity: low
object-templates:
- complianceType: mustnothave
objectDefinition:
apiVersion: v1
kind: Event
metadata:
namespace: gatekeeper-system
annotations:
constraint_action: deny
constraint_kind: K8sRequiredLabels
constraint_name: ns-must-have-gk
event_type: violation2.5.14.1.2. 監査シナリオの gatekeeper ポリシーの作成
製品設定ポリシーを使用して gatekeeper ポリシーを作成して、その gatekeeper ポリシーに対して既存のリソースを定期的に確認して評価します。Red Hat Advanced Cluster Management 設定ポリシーは、gatekeeper 制約の status フィールドで違反をチェックします。
gatekeeper ポリシーの YAML ファイルを作成します。以下のコマンドを実行します。
kubectl create -f policy-gatekeeper-audit.yaml
gatekeeper ポリシーは以下のポリシーのようになります。
apiVersion: policy.open-cluster-management.io/v1 kind: Policy metadata: name: policy-gatekeeper namespace: default annotations: policy.open-cluster-management.io/standards: policy.open-cluster-management.io/categories: policy.open-cluster-management.io/controls: spec: disabled: false policy-templates: - objectDefinition: apiVersion: policy.open-cluster-management.io/v1 kind: ConfigurationPolicy metadata: name: policy-gatekeeper-audit spec: remediationAction: inform # will be overridden by remediationAction in parent policy severity: low object-templates: - complianceType: musthave objectDefinition: apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sRequiredLabels metadata: name: ns-must-have-gk status: totalViolations: 0 violations: []
サードパーティーポリシーと製品の統合に関する詳細は、「サードパーティーポリシー コントローラーの統合 」を参照してください。