apiVersion: v1
kind: ConfigMap
metadata:
  name: mosquitto-config
  namespace: mqtt
data:
  mosquitto.conf: |
    listener 1883
    allow_anonymous true

    persistence true
    persistence_location /mosquitto/data/

    log_dest stdout
    log_type all

    # opzionale: retained messages
    persistence_file mosquitto.db
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mosquitto-pvc
  namespace: mqtt
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: csi-rbdfs-sc   # 👈 cambia se necessario
---  
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mosquitto
  namespace: mqtt
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mosquitto
  template:
    metadata:
      labels:
        app: mosquitto
    spec:
      containers:
        - name: mosquitto
          image: eclipse-mosquitto:2
          ports:
            - containerPort: 1883
          volumeMounts:
            - name: config
              mountPath: /mosquitto/config/mosquitto.conf
              subPath: mosquitto.conf
            - name: data
              mountPath: /mosquitto/data
          resources:
            requests:
              cpu: "100m"
              memory: "128Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"
      volumes:
        - name: config
          configMap:
            name: mosquitto-config
        - name: data
          persistentVolumeClaim:
            claimName: mosquitto-pvc
---                
apiVersion: v1
kind: Service
metadata:
  name: mqtt-broker
  namespace: mqtt
spec:
  selector:
    app: mosquitto
  ports:
    - name: mqtt
      port: 1883
      targetPort: 1883
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  name: mqtt-broker-nodeport
  namespace: mqtt
spec:
  ports:
  - appProtocol: tcp
    name: mqtt
    nodePort: 31883
    port: 1883
    protocol: TCP
    targetPort: 1883
  selector:
    app: mosquitto
  sessionAffinity: None
  type: NodePort