apiVersion: batch/v1
kind: Job
metadata:
  name: create-observability-thanos-secret
  namespace: openshift-gitops
  annotations:
    argocd.argoproj.io/sync-wave: "201"
    argocd.argoproj.io/hook: PreSync
  labels:
    gitops.tier.group: rhacm
    gitops.tier.layer: services
spec:
  template:
    spec:
      containers:
        - image: quay.io/openshift/origin-cli:4.8
          command:
            - /bin/bash
            - -c
            - |
              echo "Create secret for thanos.yaml"
              S3_ACCESS_KEY=""
              S3_SECRET_KEY=""
              while [[ z$S3_ACCESS_KEY == z ]]; do
                 echo "Wait for rhacm-observability bucket to be created, sleeping 120 seconds"
                 S3_ACCESS_KEY=$(oc get secret rhacm-observability-obc -n open-cluster-management-observability --template={{.data.AWS_ACCESS_KEY_ID}} | base64 -d)
                 S3_SECRET_KEY=$(oc get secret rhacm-observability-obc -n open-cluster-management-observability --template={{.data.AWS_SECRET_ACCESS_KEY}} | base64 -d)
                 sleep 5
              done;
              echo "Set S3_BUCKET_NAME"
              S3_BUCKET_NAME=$(oc get ObjectBucketClaim rhacm-observability-obc -n open-cluster-management-observability --template={{.spec.bucketName}})
              echo "Create thanos object storage configuration"
              echo "---
              apiVersion: v1
              kind: Secret
              metadata:
                name: thanos-object-storage
                namespace: open-cluster-management-observability
              type: Opaque
              stringData:
                thanos.yaml: |
                  type: s3
                  config:
                    bucket: $S3_BUCKET_NAME
                    endpoint: s3.openshift-storage.svc
                    insecure: true
                    access_key: $S3_ACCESS_KEY
                    secret_key: $S3_SECRET_KEY" | oc apply -f -
          imagePullPolicy: IfNotPresent
          name: observability-setup
      dnsPolicy: ClusterFirst
      restartPolicy: OnFailure
      serviceAccountName: openshift-gitops-cntk-argocd-application-controller
      terminationGracePeriodSeconds: 30