init: learning-service skeleton

Микросервис обучения портала: тесты, курсы, видео-уроки, доступы,
public-ссылки для кандидатов с email-валидацией.

В этой итерации:
- Skeleton (config, migrate, main, health) по паттерну tasks/candidates
- Migration 001_init: 10 таблиц (tests/questions/answers/attempts/
  attempt_answers + courses/lessons/lesson_progress + access_grants +
  public_tokens) с подробными комментариями why
- Tests: полный CRUD + вопросы/ответы; non-owner'у is_correct и
  explanation скрываются в выдаче
- Заглушки 501 для attempts / courses / lessons / video-stream /
  access / public-tokens — следующие итерации
- k8s: namespace, configmap, secrets, postgres, deployment с HPA,
  service с portal-discovery annotations
- Dockerfile, Makefile, .gitignore

См. README.md для полного списка отложенного и инструкций запуска.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Ilya
2026-05-25 22:43:37 +03:00
commit 62519081e7
24 changed files with 1915 additions and 0 deletions

22
k8s/configmap.yaml Normal file
View File

@@ -0,0 +1,22 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: learning-config
namespace: learning
data:
DATABASE_URL: "postgres://learning:learning@postgres.learning.svc.cluster.local:5432/learning?sslmode=disable"
SERVER_PORT: "3001"
# PORTAL_URL — для in-app уведомлений HR'у когда кандидат проходит тест,
# сотруднику когда ему назначили курс. Пустой = notify-вызовы no-op'ятся.
PORTAL_URL: "http://portal-server.portal.svc.cluster.local"
# PUBLIC_BASE_URL — внешний origin для public-ссылок кандидатам.
# Сервис не делает редиректов сам — он только подставляет origin
# в URL'и при создании public_token (для копи-пасты в email-шаблон).
PUBLIC_BASE_URL: "https://portal.estateliga.work"
# MinIO для видео-уроков. Bucket'ы создаются вручную (как для telephony-
# records). UseSSL=false внутри кластера.
MINIO_ENDPOINT: "minio.minio.svc.cluster.local:9000"
MINIO_BUCKET: "learning-videos"
MINIO_USE_SSL: "false"
REDIS_ADDR: "redis.redis.svc.cluster.local:6379"
REDIS_DB: "0"

12
k8s/kustomization.yaml Normal file
View File

@@ -0,0 +1,12 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: learning
resources:
- namespace.yaml
- configmap.yaml
- secrets.yaml
- postgres.yaml
- server-deployment.yaml
- server-service.yaml

4
k8s/namespace.yaml Normal file
View File

@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: learning

65
k8s/postgres.yaml Normal file
View File

@@ -0,0 +1,65 @@
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: learning
spec:
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: learning
spec:
serviceName: postgres
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:16-alpine
ports:
- containerPort: 5432
envFrom:
- secretRef:
name: postgres-secret
volumeMounts:
- name: pgdata
mountPath: /var/lib/postgresql/data
resources:
requests:
cpu: 50m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
exec:
command: ["pg_isready", "-U", "learning", "-d", "learning"]
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
exec:
command: ["pg_isready", "-U", "learning", "-d", "learning"]
initialDelaySeconds: 5
periodSeconds: 5
volumeClaimTemplates:
- metadata:
name: pgdata
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: local-path
resources:
requests:
storage: 5Gi

26
k8s/secrets.yaml Normal file
View File

@@ -0,0 +1,26 @@
# SECRETS — в открытом виде намеренно, репо приватное в gitea.estateliga.work.
# INTERNAL_API_KEY — общий ключ с порталом (X-Internal-Key, constant-time
# сравнение). MINIO_ACCESS_KEY/SECRET_KEY — учётка с правом на bucket
# learning-videos (создать через MinIO console, scope = read/write).
apiVersion: v1
kind: Secret
metadata:
name: learning-secrets
namespace: learning
type: Opaque
stringData:
PORTAL_INTERNAL_API_KEY: "36fe89ed40c01fdc54d3cf4e3fcacc8751dc456a4a1acd394e9fed48257c5734"
INTERNAL_API_KEY: "36fe89ed40c01fdc54d3cf4e3fcacc8751dc456a4a1acd394e9fed48257c5734"
MINIO_ACCESS_KEY: "learning-svc"
MINIO_SECRET_KEY: "REPLACE_AFTER_FIRST_DEPLOY"
---
apiVersion: v1
kind: Secret
metadata:
name: postgres-secret
namespace: learning
type: Opaque
stringData:
POSTGRES_USER: learning
POSTGRES_PASSWORD: learning
POSTGRES_DB: learning

View File

@@ -0,0 +1,87 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: learning-server
namespace: learning
spec:
replicas: 2
selector:
matchLabels:
app: learning-server
template:
metadata:
labels:
app: learning-server
spec:
terminationGracePeriodSeconds: 15
securityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
fsGroup: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: learning-server
image: localhost:30300/admin/learning-server:latest
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
ports:
- containerPort: 3001
envFrom:
- configMapRef:
name: learning-config
- secretRef:
name: learning-secrets
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
startupProbe:
httpGet:
path: /healthz
port: 3001
periodSeconds: 5
failureThreshold: 30
livenessProbe:
httpGet:
path: /healthz
port: 3001
periodSeconds: 10
readinessProbe:
httpGet:
path: /readyz
port: 3001
periodSeconds: 5
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
# Видео-стрим может локально жевать память, потому чуть выше.
cpu: 300m
memory: 384Mi
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: learning-server
namespace: learning
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: learning-server
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70

18
k8s/server-service.yaml Normal file
View File

@@ -0,0 +1,18 @@
apiVersion: v1
kind: Service
metadata:
name: learning-server
namespace: learning
annotations:
portal.estateliga.work/enabled: "true"
portal.estateliga.work/name: "Обучение"
portal.estateliga.work/description: "Тесты, курсы, видео-уроки, аттестация"
portal.estateliga.work/icon: "book"
portal.estateliga.work/path: "/api/learning"
portal.estateliga.work/code: "learning"
spec:
selector:
app: learning-server
ports:
- port: 80
targetPort: 3001