---
apiVersion: v1
kind: Service
metadata:
  name: "{{ ansible_operator_meta.name }}-web-svc"
  namespace: "{{ ansible_operator_meta.namespace }}"
  labels:
    app.kubernetes.io/name: 'nginx'
    app.kubernetes.io/instance: 'nginx-{{ ansible_operator_meta.name }}'
    app.kubernetes.io/component: webserver
    app.kubernetes.io/part-of: '{{ deployment_type }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
{% if ingress_type | lower == 'loadbalancer' and loadbalancer_annotations %}
  annotations:
    {{ loadbalancer_annotations | indent(width=4) }}
{% endif %}
spec:
  selector:
    app.kubernetes.io/name: 'nginx'
    app.kubernetes.io/instance: 'nginx-{{ ansible_operator_meta.name }}'
    app.kubernetes.io/component: webserver
    app.kubernetes.io/part-of: '{{ deployment_type }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
  ports:
{% if ingress_type | lower != 'loadbalancer' and loadbalancer_protocol | lower != 'https' %}
    - port: 24880
      protocol: TCP
      targetPort: 8080
      name: web-8080
{% endif %}
{% if ingress_type | lower == 'route' and route_tls_termination_mechanism | lower == 'passthrough' %}
    - port: 443
      protocol: TCP
      targetPort: 8443
      name: web-8443
{% endif %}
{% if ingress_type | lower == 'loadbalancer' and loadbalancer_protocol | lower == 'https' %}
    - port: {{ loadbalancer_port }}
      protocol: TCP
      targetPort: 8080
      name: web-8443
{% elif ingress_type | lower == 'loadbalancer' and loadbalancer_protocol | lower != 'https' %}
    - port: {{ loadbalancer_port }}
      protocol: TCP
      targetPort: 8080
      name: web-8080
{% endif %}
{% if ingress_type | lower == "loadbalancer" %}
  type: LoadBalancer
{% elif ingress_type|lower == "nodeport" %}
{% if nodeport_port is defined %}
      nodePort: {{ nodeport_port }}
{% endif %}
  type: NodePort
{% else %}
  type: ClusterIP
{% endif %}
