---
apiVersion: v1
kind: Service
metadata:
  name: asterisk
  annotations:
    external-dns.alpha.kubernetes.io/hostname: voip.k-space.ee
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  selector:
    app: asterisk
  ports:
    - name: asterisk
      protocol: UDP
      port: 5060
    - name: sip-data-10000
      protocol: UDP
      port: 10000
    - name: sip-data-10001
      protocol: UDP
      port: 10001
    - name: sip-data-10002
      protocol: UDP
      port: 10002
    - name: sip-data-10003
      protocol: UDP
      port: 10003
    - name: sip-data-10004
      protocol: UDP
      port: 10004
    - name: sip-data-10005
      protocol: UDP
      port: 10005
    - name: sip-data-10006
      protocol: UDP
      port: 10006
    - name: sip-data-10007
      protocol: UDP
      port: 10007
    - name: sip-data-10008
      protocol: UDP
      port: 10008
    - name: sip-data-10009
      protocol: UDP
      port: 10009
    - name: sip-data-10010
      protocol: UDP
      port: 10010
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: asterisk
  labels:
    app: asterisk
spec:
  selector:
    matchLabels:
      app: asterisk
  replicas: 1
  template:
    metadata:
      labels:
        app: asterisk
    spec:
      containers:
        - name: asterisk
          image: harbor.k-space.ee/k-space/asterisk
          command:
            - /usr/sbin/asterisk
          args:
            - -TWBpvvvdddf
          volumeMounts:
            - name: config
              mountPath: /etc/asterisk
          ports:
            - containerPort: 8088
              name: metrics
      volumes:
        - name: config
          secret:
            secretName: asterisk-secrets
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: asterisk
spec:
  selector:
    matchLabels:
      app: asterisk
  podMetricsEndpoints:
    - port: metrics
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: asterisk
spec:
  groups:
    - name: asterisk
      rules:
        - alert: AsteriskPhoneNotRegistered
          expr: asterisk_endpoints_state{resource=~"1.*"} < 2
          for: 5m
          labels:
            severity: critical
          annotations:
            summary: "{{ $labels.resource }} is not registered."
        - alert: AsteriskOutboundNumberNotRegistered
          expr: asterisk_pjsip_outbound_registration_status == 0
          for: 5m
          labels:
            severity: critical
          annotations:
            summary: "{{ $labels.username }} is not registered with provider."
        - alert: AsteriskCallsPerMinuteLimitExceed
          expr: asterisk_channels_duration_seconds > 10*60
          for: 20m
          labels:
            severity: warning
          annotations:
            summary: "Call at channel {{ $labels.name }} is taking longer than 10m."