apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: ecr-cred-updater
spec:
  schedule: "* */8 * * *"
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      backoffLimit: 4
      template:
        spec:
#          serviceAccountName: ecr-cred-updater
          terminationGracePeriodSeconds: 0
          restartPolicy: Never
          containers:
          - name: tools-cli
            image: tools:v1.0.0
            env:
              - name: AWS_ACCESS_KEY_ID
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: AWS_ACCESS_KEY_ID
              - name: AWS_SECRET_ACCESS_KEY
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: AWS_SECRET_ACCESS_KEY
              - name: AWS_REGION
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: aws-region
              - name: AWS_ACCOUNT
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: aws-account
              - name: OC_API_URL
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: oc-api-url
              - name: OC_USER
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: oc-user
              - name: OC_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: aws-account-creds
                    key: oc-password
              - name: NAMESPACE
                value: apiservice-microservicios
            command:
            - "/bin/sh"
            - "-c"
            - |
              #AWS_ACCOUNT=<AWS_ACCOUNT_ID>
              #export AWS_ACCESS_KEY_ID=<KEY_ID>
              #export AWS_SECRET_ACCESS_KEY=<ACCESS_KEY>
              #export AWS_REGION=us-east-1
              DOCKER_REGISTRY_SERVER=https://${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
              DOCKER_USER=AWS
              DOCKER_PASSWORD=`aws ecr get-login --region ${AWS_REGION} --registry-ids ${AWS_ACCOUNT} | cut -d' ' -f6`
              oc login https://${OC_API_URL} --username=$OC_USER --password=$OC_PASSWORD
              oc project ${NAMESPACE}

              oc delete secret aws-registry || true
              oc create secret docker-registry aws-registry \
                --docker-server=$DOCKER_REGISTRY_SERVER \
                --docker-username=$DOCKER_USER \
                --docker-password=$DOCKER_PASSWORD \
                --docker-email=no@email.local
              oc secrets link default aws-registry --for=pull
