apiVersion: batch/v1beta1
kind: CronJob
metadata:
  labels:
    app: aws-telemetry
  name: aws-kubeflow-telemetry
  namespace: kubeflow
spec:
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 0
  jobTemplate:
    metadata:
      labels:
        app: aws-telemetry
    spec:
      backoffLimit: 3
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: "false"
          labels:
            app: aws-telemetry
        spec:
          containers:
          - command:
            - /bin/sh
            - -c
            - "# Following code uses IMDS service. See: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html\n\nget_instance_id()
              {\n  # use IMDSv2 if enabled else fallback to IMDSv1\n  local _token\n
              \ _token=$(curl -s --retry 3 --max-time 3 -X PUT http://169.254.169.254/latest/api/token
              -H \"X-aws-ec2-metadata-token-ttl-seconds: 21600\")\n  if [[ -n ${_token+x}
              ]]; then\n    IMDSV2_HEADER=(-H \"X-aws-ec2-metadata-token: ${_token}\")\n
              \ fi\n  \n  INSTANCE_ID=$(curl -s --retry 3 \"${IMDSV2_HEADER[@]}\"
              http://169.254.169.254/latest/meta-data/instance-id)\n\n  local _instance_id_regex=\"^(i-\\S{17})\"\n
              \ if [[ -z ${INSTANCE_ID+x} || ! ${INSTANCE_ID} =~ ${_instance_id_regex}
              ]]; then\n    exit 0\n  fi\n}\n\nget_region() {\n  # regions where S3
              buckets have been created\n  local _valid_regions=(\n    \"us-east-1\"\n
              \   \"us-east-2\"\n    \"us-west-1\"\n    \"us-west-2\"\n    \"af-south-1\"\n
              \   \"ap-east-1\"\n    \"ap-southeast-1\"\n    \"ap-southeast-2\"\n
              \   \"ap-southeast-3\"\n    \"ap-south-1\"\n    \"ap-northeast-1\"\n
              \   \"ap-northeast-2\"\n    \"ap-northeast-3\"\n    \"ca-central-1\"\n
              \   \"eu-central-1\"\n    \"eu-north-1\"\n    \"eu-west-1\"\n    \"eu-west-2\"\n
              \   \"eu-west-3\"\n    \"eu-south-1\"\n    \"me-south-1\"\n    \"sa-east-1\"\n
              \ )\n  REGION=$(curl -s --retry 3 \"${IMDSV2_HEADER[@]}\" http://169.254.169.254/latest/meta-data/placement/availability-zone
              | awk '{print substr($1, 1, length($1)-1)}')\n\n  if [[ -z ${REGION+x}
              || ! ${_valid_regions[${REGION}]+x} ]]; then\n    exit 0\n  fi\n}\n\nsleep
              $((1 + $RANDOM % 300))\nget_instance_id\nget_region\n\n# send a GET
              request to S3 access point\ncurl -s -o /dev/null \"https://kubeflow-on-aws-usage-tracking-${REGION}.s3.${REGION}.amazonaws.com/instance-${INSTANCE_ID}.log?x-instance-id=${INSTANCE_ID}\"\n"
            image: public.ecr.aws/amazonlinux/amazonlinux:2
            name: amazonlinux
          restartPolicy: Never
      ttlSecondsAfterFinished: 0
  schedule: 0 0 * * *
  successfulJobsHistoryLimit: 0
