feat(iot): Этап 1 — CRD IoTDevice + контроллер credentials

- iot/api/v1alpha1/device_types.go — CRD IoTDevice
- iot/api/v1alpha1/groupversion_info.go — API group iot.kube5s.ru/v1alpha1
- iot/api/v1alpha1/zz_generated.deepcopy.go — deepcopy (controller-gen)
- iot/config/crd/bases/iot.kube5s.ru_iotdevices.yaml — CRD манифест
- iot/controllers/iotdevice_controller.go — Reconcile: Secret с MQTT credentials
- main.go — регистрация IoT схемы и IoTDeviceReconciler

go build ./... — ошибок нет
This commit is contained in:
Naeel
2026-04-04 09:32:47 +03:00
parent 6dc2dc69ba
commit 716efafda8
7 changed files with 670 additions and 0 deletions
@@ -0,0 +1,123 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
name: iotdevices.iot.kube5s.ru
spec:
group: iot.kube5s.ru
names:
kind: IoTDevice
listKind: IoTDeviceList
plural: iotdevices
singular: iotdevice
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.deviceId
name: DeviceID
type: string
- jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .spec.enabled
name: Enabled
type: boolean
- jsonPath: .status.mqttUsername
name: MQTTUser
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
schema:
openAPIV3Schema:
description: |-
IoTDevice — ресурс для регистрации IoT-устройства в платформе.
Контроллер автоматически создаёт k8s Secret с MQTT-credentials.
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: IoTDeviceSpec — желаемое состояние IoT-устройства.
properties:
deviceId:
description: |-
DeviceID — уникальный идентификатор устройства внутри namespace.
Используется как часть MQTT username и имени Secret.
Разрешены только строчные буквы, цифры и дефис — для совместимости с k8s именами.
maxLength: 48
pattern: ^[a-z0-9][a-z0-9-]*[a-z0-9]$
type: string
enabled:
default: true
description: |-
Enabled — активно ли устройство (может подключаться к MQTT).
Если false — контроллер устанавливает phase=Disabled, EMQX auth отклоняет подключение.
Secret с credentials НЕ удаляется — при re-enable пароль остаётся прежним.
type: boolean
metadata:
additionalProperties:
type: string
description: |-
Metadata — произвольные метаданные устройства (модель, локация и т.д.).
Хранятся только в CRD, не влияют на логику контроллера.
type: object
required:
- deviceId
- enabled
type: object
status:
description: IoTDeviceStatus — наблюдаемое состояние IoT-устройства (заполняет
контроллер).
properties:
lastConnected:
description: |-
LastConnected — время последнего MQTT-подключения устройства.
Заполняется MQTT auth-сервисом при каждом успешном CONNECT.
format: date-time
type: string
message:
description: Message — человекочитаемое сообщение о текущем статусе
или ошибке.
type: string
mqttUsername:
description: |-
MQTTUsername — имя пользователя для подключения к MQTT-брокеру.
Формат: {namespace}_{deviceId} — глобально уникален в рамках EMQX.
type: string
phase:
description: 'Phase — текущее состояние: Active, Disabled, Pending,
Error.'
type: string
secretName:
description: SecretName — имя k8s Secret в том же namespace, содержащего
mqtt-username и mqtt-password.
type: string
topicPrefix:
description: |-
TopicPrefix — MQTT topic prefix, на который разрешена публикация.
Формат: {namespace}/ — устройство не может публиковать в чужие namespace.
type: string
type: object
type: object
served: true
storage: true
subresources:
status: {}