Files
2026-04-08 20:22:14 +07:00

132 lines
3.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Урок 3.3 — Продвинутые техники мутации и переменные
## Файлы
| Файл | Описание |
|------|----------|
| `kyverno-global-config.yaml` | ConfigMap с централизованными настройками |
| `set-dynamic-resource-limits.yaml` | Лимиты из ConfigMap по типу сервиса |
| `add-creator-audit-annotation.yaml` | Автоматический audit trail |
## Подготовка
```bash
# Сначала создать ConfigMap с конфигурацией
kubectl apply -f kyverno-global-config.yaml
# Затем политики
kubectl apply -f set-dynamic-resource-limits.yaml
kubectl apply -f add-creator-audit-annotation.yaml
```
## Тест динамических лимитов
```bash
# Pod типа "api" — получит 512Mi/500m
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: api-pod
namespace: default
labels:
service-type: api
spec:
containers:
- name: app
image: nginx:1.25.3
EOF
kubectl get pod api-pod -o jsonpath='{.spec.containers[0].resources}' | jq .
# Ожидаем: limits.memory=512Mi, limits.cpu=500m
# Pod типа "worker" — получит 1Gi/250m
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: worker-pod
namespace: default
labels:
service-type: worker
spec:
containers:
- name: app
image: nginx:1.25.3
EOF
kubectl get pod worker-pod -o jsonpath='{.spec.containers[0].resources}' | jq .
# Ожидаем: limits.memory=1Gi, limits.cpu=250m
# Изменить дефолты без изменения политики
kubectl edit configmap kyverno-global-config -n kyverno
# Измените default_memory: "512Mi", сохраните
# Новые поды получат новые дефолты (~30-60 сек на обновление кэша)
# Удалить тесты
kubectl delete pod api-pod worker-pod
```
## JMESPath функции — шпаргалка
```yaml
# Строки
{{ to_upper('hello') }} # HELLO
{{ to_lower('WORLD') }} # world
{{ trim(' spaces ') }} # spaces
{{ split('a,b,c', ',') }} # ["a","b","c"]
{{ join(['a','b','c'], '-') }} # a-b-c
{{ replace_all('foo/bar', '/', '-') }} # foo-bar
{{ truncate('long string', 5) }} # long
# Числа
{{ to_number('42') }} # 42
{{ divide(`10`, `3`) }} # 3.333
# Условия (Elvis operator)
{{ request.object.metadata.labels.env || 'default' }}
# Массивы
{{ length(request.object.spec.containers) }}
{{ request.object.spec.containers[0].name }}
{{ request.object.spec.containers[].name }} # все имена
# Время
{{ time_now_utc() }} # текущее UTC время
```
## Тест audit аннотаций
```bash
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-audit
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: app
image: nginx:1.25.3
resources:
limits:
memory: "128Mi"
cpu: "100m"
EOF
kubectl get deployment test-audit \
-o jsonpath='{.metadata.annotations}' | jq .
# Увидите: created-by, created-at, user-groups
kubectl delete deployment test-audit
```