# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0

http:
  http://etcd:{{ .Vars.service.ports.peer }}/version:
    status: 200
file:
  /bitnami/etcd:
    exists: true
    filetype: directory
    mode: "2775"
command:
  {{- $auth := printf "--user=root --password=%s" .Vars.auth.rbac.rootPassword }}
  {{- $svc_endpoint := printf "--endpoints=etcd:%d" .Vars.service.ports.client }}
  cluster-members:
    exec: etcdctl {{ $auth }} {{ $svc_endpoint }} member list
    exit-status: 0
    stdout:
    {{- range $e, $i := until .Vars.replicaCount }}
      - "etcd-{{ $i }}"
    {{- end }}
  {{- range $e, $i := until .Vars.replicaCount }}
  {{- $key := printf "key_%s" (randAlpha 5) }}
  {{- $value := printf "value_%s" (randAlpha 5) }}
  manage-db-{{ $i }}:
    exec: etcdctl {{ $auth }} {{ $svc_endpoint }} put {{ $key }} {{ $value }} && etcdctl {{ $auth }} --endpoints=etcd-{{ $i }}.etcd-headless:{{ $.Vars.containerPorts.client }} get {{ $key }}
    exit-status: 0
    stdout:
    - "{{ $key }}"
  {{- end }}
  {{- $uid := .Vars.containerSecurityContext.runAsUser }}
  {{- $gid := .Vars.podSecurityContext.fsGroup }}
  check-user-info:
    # The UID and GID should always be either the one specified as vars (always a bigger number that the default)
    # or the one randomly defined by openshift (larger values). Otherwise, the chart is still using the default value.
    exec: if [ $(id -u) -lt {{ $uid }} ] || [ $(id -G | awk '{print $2}') -lt {{ $gid }} ]; then exit 1; fi
    exit-status: 0
