mode: 0644
path: "/etc/kubernetes/manifests/haproxy.yaml"
contents:
  inline: |
    kind: Pod
    apiVersion: v1
    metadata:
      name: haproxy
      namespace: openshift-openstack-infra
      creationTimestamp:
      deletionGracePeriodSeconds: 65
      labels:
        app: openstack-infra-api-lb
    spec:
      volumes:
      - name: resource-dir
        hostPath:
          path: "/etc/kubernetes/static-pod-resources/haproxy"
      - name: kubeconfigvarlib
        hostPath:
          path: "/var/lib/kubelet"
      - name: run-dir
        empty-dir: {}
      - name: conf-dir
        hostPath:
          path: "/etc/haproxy"
      - name: chroot-host
        hostPath:
          path: "/"
      containers:
      - name: haproxy
        image: {{.Images.haproxyImage}}
        env:
          - name: OLD_HAPROXY_PS_FORCE_DEL_TIMEOUT
            value: "120"
        command:
        - "/bin/bash"
        - "-c"
        - |
          #/bin/bash
          verify_old_haproxy_ps_being_deleted()
          {
            local prev_pids
            prev_pids="$1"
            sleep $OLD_HAPROXY_PS_FORCE_DEL_TIMEOUT
            cur_pids=$(pidof haproxy)
            for val in $prev_pids; do
                if [[ $cur_pids =~ (^|[[:space:]])"$val"($|[[:space:]]) ]] ; then
                   kill $val
                fi
            done
          }

          reload_haproxy()
          {
            old_pids=$(pidof haproxy)
            if [ -n "$old_pids" ]; then
                /usr/sbin/haproxy -W -db -f /etc/haproxy/haproxy.cfg  -p /var/lib/haproxy/run/haproxy.pid -x /var/lib/haproxy/run/haproxy.sock -sf $old_pids &
                #There seems to be some cases where HAProxy doesn't drain properly.
                #To handle that case, SIGTERM signal being sent to old HAProxy processes which haven't terminated.
                verify_old_haproxy_ps_being_deleted "$old_pids"  &
            else
                /usr/sbin/haproxy -W -db -f /etc/haproxy/haproxy.cfg  -p /var/lib/haproxy/run/haproxy.pid &
            fi
          }

          msg_handler()
          {
            while read -r line; do
              echo "The client send: $line"  >&2
              # currently only 'reload' msg is supported
              if [ "$line" = reload ]; then
                  reload_haproxy
              fi
            done
          }
          set -ex
          declare -r haproxy_sock="/var/run/haproxy/haproxy-master.sock"
          declare -r haproxy_log_sock="/var/run/haproxy/haproxy-log.sock"
          export -f msg_handler
          export -f reload_haproxy
          export -f verify_old_haproxy_ps_being_deleted
          rm -f "$haproxy_sock" "$haproxy_log_sock"
          socat UNIX-RECV:${haproxy_log_sock} STDOUT &
          if [ -s "/etc/haproxy/haproxy.cfg" ]; then
              /usr/sbin/haproxy -W -db -f /etc/haproxy/haproxy.cfg  -p /var/lib/haproxy/run/haproxy.pid &
          fi
          socat UNIX-LISTEN:${haproxy_sock},fork system:'bash -c msg_handler'
        resources:
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: conf-dir
          mountPath: "/etc/haproxy"
        - name: run-dir
          mountPath: "/var/run/haproxy"
        livenessProbe:
          initialDelaySeconds: 50
          httpGet:
            path: /haproxy_ready
            port: 50936
        terminationMessagePolicy: FallbackToLogsOnError
        imagePullPolicy: IfNotPresent
      - name: haproxy-monitor
        securityContext:
          privileged: true
        image: {{ .Images.baremetalRuntimeCfgImage }}
        command:
          - "/bin/bash"
          - "-c"
          - |            
            cp /host/etc/resolv.conf /etc/resolv.conf
            monitor /var/lib/kubelet/kubeconfig  /config/haproxy.cfg.tmpl  /etc/haproxy/haproxy.cfg  --api-vip {{ .Infra.Status.PlatformStatus.OpenStack.APIServerInternalIP }}
        resources:
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: conf-dir
          mountPath: "/etc/haproxy"
        - name: run-dir
          mountPath: "/var/run/haproxy"
        - name: resource-dir
          mountPath: "/config"
        - name: chroot-host
          mountPath: "/host"
        - name: kubeconfigvarlib
          mountPath: "/var/lib/kubelet"
        livenessProbe:
          initialDelaySeconds: 10
          exec:
            command:
              - /bin/bash
              - -c
              - |
                cmp /host/etc/resolv.conf /etc/resolv.conf
        terminationMessagePolicy: FallbackToLogsOnError
        imagePullPolicy: IfNotPresent
      hostNetwork: true
      tolerations:
      - operator: Exists
      priorityClassName: system-node-critical
    status: {}
