الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

الفصل 5الاسم في الدورة: Policy as code

Write policies as code with OPA Gatekeeper

Pod Security covers pods. For your own rules — "every namespace needs an owner label", "images only from our registry", "no Services of type LoadBalancer" — use an admission controller with policy as code: OPA Gatekeeper (Rego language) or Kyverno (YAML).

  • 4 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

المهارات في هذا الفصل

  • السياسات ككود باستخدام OPA Gatekeeper

5.1

Know how Gatekeeper works

Gatekeeper is a validating admission webhook. A ConstraintTemplate holds the Rego logic; a Constraint applies it to certain objects with parameters.

kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/v3.18.0/deploy/gatekeeper.yaml
kubectl get pods -n gatekeeper-system
kubectl get constrainttemplates
kubectl get constraints

الأوامر في هذا الدرس

5.2

Write a template and a constraint

Example: images must come from an allowed registry.

template-allowed-repos.yaml
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata: { name: k8sallowedrepos }
spec:
  crd:
    spec:
      names: { kind: K8sAllowedRepos }
      validation:
        openAPIV3Schema:
          type: object
          properties:
            repos: { type: array, items: { type: string } }
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8sallowedrepos
        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          not startswith_any(container.image, input.parameters.repos)
          msg := sprintf("image %v is not from an allowed registry", [container.image])
        }
        startswith_any(img, repos) { startswith(img, repos[_]) }
constraint-allowed-repos.yaml
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sAllowedRepos
metadata: { name: only-our-registry }
spec:
  enforcementAction: deny
  match:
    kinds: [{ apiGroups: [""], kinds: ["Pod"] }]
    namespaces: ["shop"]
  parameters:
    repos: ["ghcr.io/s0sta/", "acrshop2026.azurecr.io/"]
kubectl apply -f template-allowed-repos.yaml -f constraint-allowed-repos.yaml
kubectl run x --image=nginx -n shop
Error from server (Forbidden): admission webhook "validation.gatekeeper.sh" denied the request: [only-our-registry] image nginx is not from an allowed registry

الأوامر في هذا الدرس

5.3

Audit existing objects

Gatekeeper also checks objects that existed before the rule. Results are in the constraint's status.

kubectl get k8sallowedrepos only-our-registry -o jsonpath='{.status.totalViolations}'
kubectl describe k8sallowedrepos only-our-registry | grep -A5 Violations
enforcementActionEffect
denyReject the request
dryrunAllow, only report in audit
warnAllow, show a warning

في العمل

Roll out new rules with dryrun, fix the violations, then switch to deny.

5.4

Compare with Kyverno and ValidatingAdmissionPolicy

Same goal, other tools.

ToolPolicy languageNotes
OPA GatekeeperRegoVery flexible; a CKS topic
KyvernoYAMLEasy to read; can also mutate and generate objects, verify image signatures
ValidatingAdmissionPolicyCEL (built into Kubernetes)No extra controller needed

كل الأوامر في هذا الفصل